Skip to main content

notebooks - How to set the output be generated in a cell different from "Output"


I would have some functions, for instance Plot, to generate their output using a different Cell's style rather than "Output". The reason is that I'm producing some documents with many images and, for editing reasons, I need to adjust some options of their cell's style.


Unfortunately, I cannot change the "Output" style, because other kind of outputs don't have the same formatting as images. Just to make and example, suppose I want all graphics centered in the cell, while other output left aligned. So, I would assign a custom style to the output generate by Plot. Is there any suggestion?



Answer




For Graphics output specifically there is a nice approach using $DisplayFunction:


(* create a new output style -- overwrites existing custom style sheet *)
SetOptions[EvaluationNotebook[],
StyleDefinitions ->
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["altOutput"], TextAlignment -> Center]},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"]
]

(* define a display function *)

altOutput[expr_] := CellPrint @ ExpressionCell[expr, "altOutput"]

$DisplayFunction = altOutput;

Graphics output in this Notebook will now be centered. You could also set the DisplayFunction option for individual plot types independently rather than using the global $DisplayFunction parameter.


Comments