Skip to main content

front end - FormatType with respect to Cell's content


Minimal example:


Let's establish a notebook's stylesheet with additional TestStyle which has a FormatType->"CellExpression":


SetOptions[
EvaluationNotebook[],
StyleDefinitions -> Notebook[{

Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Notebook"], Background -> LightBlue],
Cell[StyleData["TestStyle"], FormatType -> "CellExpression"]
}]
]

and let's create couple of examples:


CellPrint[Cell[BoxData["TestStyle"], "TestStyle"]]

CellPrint[Cell[TextData["TestStyle"], "TestStyle"]]


CellPrint[Cell["TestStyle", "TestStyle"]]

Problem:


The first one doesn't respect FormatType (Background should be White). I can imagine it is to be expected but I failed to explain it to myself.


enter image description here


Question:


What exactly do I have to keep in mind about interference between Cell contents and FormatType or other options. Background inherited from CellExpression shouldn't care about the content so there is probably something more general to remember.



Answer



Cell[TextData["string"]] is completely equivalent to Cell["string"]. There's no reason to expect the two to behave differently. TextData is a required wrapper when the contents are more complex than a string, but when it's a string, it changes nothing. And TextData cells have only a single way of determining the format type...from the FormatType option.



While TextData must rely on the FormatType option for its notion of format type, BoxData relies instead on the FormBox wrapper. You can see evidence of this by evaluating...


Dynamic[CurrentValue[FormatType]]

If you convert the output cell to TraditionalForm, you'll see the dynamic update. But note that nowhere in the underlying cell expression is FormatType->TraditionalForm set. Nor is it set in the "TraditionalForm" style in Core.nb. This is the FE trying to keep the FormBox and option forms of FormatType in sync.


When a BoxData cell has no FormBox, then it's StandardForm. Always. This is hard-coded and cannot be overridden.


Comments