# Order/Dependency of Styles in a Stylesheet

How exactly is a custom stylesheet processed? Which style depends on (inherits from) which other style.

Consider e.g. setting a font. I would expect that setting the font for "Section" to YY after setting the font for "All" to XX would result in the font being YY for Sections and XX for everything else, but the "All" overrides it. For "Text" and "Section" its works as expected. "All" seems to be a global override (at least for the FontFamily).

Can someone clarify this?

• All styles ultimately inherit from Core.nb. The style applied will be the most "inner" one. For stylesheets the styles applied will be the private styles, then the styles from the stylesheet that your private styles inherit from -- usually Default.nb, and then from Core.nb – Mike Honeychurch Oct 28 '12 at 21:56

[Edit: I updated this question to fix a couple of mistakes and to reflect the behavior in 10.3. -jf]

The question I'll answer here is "How is the cell style of a given cell resolved." There are several other questions one could ask about option resolution, and I won't attempt to answer all of them here, but I will show in some exhaustive detail, at the cell level, what's happening.

First, a parent set of options is calculated at the notebook level and passed down to cells. I won't discuss how those are computed (feel free to ask another question about that), but the options passed down from the notebook level are identical to all cells within that notebook (except inline cells, which inherit many properties from their parent cells...once again, that goes a bit beyond the scope of my answer).

Starting with those options, further options are merged in from the following locations:

1. The fully resolved style corresponding to the primary stylename, if one exists.
2. The cell's FormatType is determined, and the named style corresponding to the FormatType is applied.
3. If the cell is an inline cell, the named style "InlineCell" is applied.
4. If the cell is an inline cell which is presently being edited, the named style "InlineCellEditing" is applied.
5. For each cell group (i.e., at different nesting levels) the cell is in, the setting of the PrivateCellOptions->{"CellGroupBaseStyle"} option is applied, beginning with the innermost group.
6. If the cell is a generated cell (i.e., created by action of Shift+Return from a parent cell in the cell group), and that cell has been created since the notebook was last opened, then the parent cell is checked to see if it has "changed" in a way which would affect its evaluation. If so, then the value of PrivateCellOptions->{"EvaluationUnmatchedStyle"} is applied. This is the source of the "dimming" behavior.
7. If the cell is a docked cell, then stylenames/options from the DefaultDockedCellStyle option are applied.
8. If the cell is an attached cell, then stylenames/options from the DefaultAttachedCellStyle
9. If the style has multiple stylenames applied to it, then the further stylenames are resolved and applied in order of appearance.
10. Finally, the options set specifically on the cell are applied.

More should be said about how stylenames are resolved into styles (which is to say, a collection of option settings). Without touching on issues of style or stylesheet inheritance, here is what happens every time a named style is resolved for use by a cell:

1. Begin with the "All" style.
2. Merge in the named style definition.
3. If one exists, merge in the environment-specific version of the style definition.

So, back to the initial question. Why does setting a font on the "All" style affect the style resolution of a "Section" cell, which one would expect to override the "Section" cell? The issue here comes up in the resolution of the FormatType. Section cells have a FormatType of TextForm, and Mathematica is making an attempt to resolve the named cell style "TextForm". In doing so, it's pulling in "All" yet again.

I, for one, don't really find the "All" style all that useful. I much prefer to work with either generic environment styles (e.g., StyleData[All, "environment name"]) or the "Notebook" style. Those styles are applied exclusively at the notebook level, and so become part of the background default that all cells inherit from, as I describe above.

• That's very useful post. Is is possible that the full style stack and the resolving scheme will documented/published somewhere? I'm a little tired of experimenting :) – Kuba Feb 23 '16 at 15:34
• @Kuba, that would probably happen after the box language gets fully documented. Which is to say someday, but don't hold your breath. Or you can keep luring me into answering SE questions which involve reading FE source code. :) – John Fultz Feb 23 '16 at 18:04
• Ok, I will. p.s. it seems that one should discourage the usage of "All". It can become really messy for multiple layers of stylesheets and non trivial inheritance. Still it all seems logical, the problem is, one cannot predict that, basing only on documentation. It would be really useful if WRI agrees to publish something like "documentation updates sketches" with e.g this scheme. Users would be glad because less things will be unpredictable and WRI too since you can say "hey, don't blame us for whatever is written there, that's just a sketch". – Kuba Feb 24 '16 at 8:08
• Let's say I've updated Input in private stylesheet. It won't be reflected in styles defined in Default that are basing on Input. But If I restate those styles and in private Stylesheet then I have to put/copy there really everything because StyleData[style, definitions -> input] alone will just define style as input without previous settings. Does this issue deserve "guidelines question"? – Kuba Feb 24 '16 at 8:18
• What worked though was to use: Cell[StyleData["WolframAlphaLong"], FormatType->Input] but I don't know if that's safe/generic way. – Kuba Feb 24 '16 at 8:28