14
  • Let's create a stylesheet:

NotebookPut@
 Notebook[{
   Cell[StyleData[StyleDefinitions -> "Default.nb"]], 
   Cell[StyleData["myStyle"]], 
   Cell[StyleData["myStyle2", StyleDefinitions -> StyleData["myStyle"]]]
}]

enter image description here

  • Now let's add a FontSize -> 30 spec for our first style and FontSize ->Inherited for the last one.

enter image description here

  • This FontSize->Inherited is not necessary, the FontSize of myStyle2 will be 30 anyway, since there is StyleDefinitions -> StyleData["myStyle"] ( Ref)

enter image description here

  • But let's replace Inherited with (Inherited + 0):

enter image description here

It immediately changed the size! That means, it inherits from somewhere else now...

Q1: Can this be explained with documentation?


Of course 0 makes no sense but my goal is to have e.g. Inherited + 5 so when I change myStyle I won't have to change anything in dependent styles.

Q2: So I would gladly upvote any answer that will tell me how to achieve this behaviour within one stylesheet.

I tried FontSize :> (CurrentValue[{StyleDefinitions, "myStyle", FontSize}] + 1) but to no avail.


key words: cascading styles style inheritance


loose thoughs:

I'm afraid it may be tough since e.g. SubitemParagraph isn't inheriting CellMargins to align with SubItem but has it hardcoded to be the same. :-(

One possible solution would be to be able to create FrontEnd`CurrentValue["myStyleFontSize"] = 15 so that I can set myStyle and myStyle2 FontSizes with reference to it like FontSize :> CurrentValue["myStyleFontSize"]. There are similar solutions in e.g. Core.nb with "MenuFontSize" but it's not the solution "within one stylesheet".

Kuba
  • 136,707
  • 13
  • 279
  • 740

2 Answers2

6

TLDR: The point of this answer is that we can move styles merging from stylesheet to the expression/boxes level. But sill have all definitions kept in the stylesheet.

My last conclusions are that what I was after was not meant to work and an example with plain Inherited was a coincidence.


We can create main styles and complementary styles.

So the main is:

Cell[StyleData["myStyle"],
 FontSize  -> 35,
 FontColor -> RGBColor[1,0,0]
]

And complementary style is e.g. "biggerFont":

Cell[
  StyleData["biggerFont"], 
  FontSize -> (Inherited+10)
]

The preview of the stylesheet won't be very informative:

enter image description here

But we can use it in very useful way:

Framed[
 Dynamic@AbsoluteCurrentValue[FontSize],
 BaseStyle -> "myStyle"
 ]

Framed[
 Dynamic@AbsoluteCurrentValue[FontSize],
 BaseStyle -> {"myStyle", "biggerFont"}
 ]

enter image description here

Neat!

We can combine styles and inheritance will fire at the end.

The order in {"myStyle", "biggerFont"} is important.

Kuba
  • 136,707
  • 13
  • 279
  • 740
3

You may think the following is a cheat, but it accomplishes what you ask for. I tested the code with V10.3 on OS X 10.10.2.

With[{size = 24}, 
  CreateDocument[{
    Cell[StyleData[StyleDefinitions -> "Default.nb"]], 
    Cell[StyleData["myStyle1"], Background -> LightBlue, FontSize -> size], 
    Cell[StyleData["myStyle2", StyleDefinitions -> StyleData["myStyle1"]], 
      FontSize -> size + 12]},
    WindowTitle -> "MyStyleSheet"]]

stylesheet

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • I think it is good approach to deploy a stylesheet that is not meant to be adjusted by the end-user, which is often the case for me. – Kuba Dec 10 '15 at 16:20