# How can I set the style of one Output cell so it doesn't change after re-evaluating its Input cell?

I have created a notebook for my classes which is full of inputs that need to be evaluated by my students. All these inputs produce standard outputs, which are fine as they are. But there is one of them, and just that one, which needs to produce a special style of output (other similar outputs must remain as they are).

Specifics:

• The left margin of this particular output must be set to zero.

• This cell is the result of evaluating a Manipulate[...] function, which contains graphics and text.

• This change must be permanent: I don't want to manually assign a style each time the input is evaluated, but I don't mind including any code in the input that generates this cell.

Right now, this is the solution I have implemented:

• I have created a duplicate of the Output style (which I called Output2 and inherits from Output's style: Cell[StyleData["Output2",StyleDefinitions->StyleData["Output"]]]).

• Then I changed its margins in the Stylesheet editor (CellMargins->{{5,10},{5,10}}).

• I write SetOptions[EvaluationNotebook[], "GeneratedCellStyles" -> {"Output" -> "Output2"}]; in the input cell, right before the code that will generate this special output.

• I write SetOptions[EvaluationNotebook[], "GeneratedCellStyles" -> {"Output" -> "Output"}]; after the code of the input cell, to restore future outputs back to normal.

This solution seems too complicated to me and lacking elegance. So, I was hopping that any of you know a better solution. Any help would be very appreciated.

• The interaction with Manipulate may be important. When you say "the output of" I am assuming you mean the interactive Manipulate object itself, created by evaluating a line of code with Manipulate[ . . . ]. – Mr.Wizard Apr 28 '18 at 14:37
• Yes, that is what I mean. I have a Manipulate[...] which produces an interactive output. This interactive cell must have a left margin of 5 or less. (I've changed my description to use your words, hopefully this will make things clearer.) – RoberRM Apr 28 '18 at 14:56

For a one-off use I believe this should do what you want, using the existing Style Sheet definition.

CellPrint[
ExpressionCell[
Manipulate[Plot[Sin[x (1 + a x)], {x, 0, 6}], {a, 0, 2}],
"Output2"
]
]


There may be a more direct way using Style but my skills are quite rusty at the moment, and I'd need to refresh and experiment.

• This is exactly what I needed. And it has a very welcome bonus: it removes the Out[#] label of the output. Thank you very, very much!!! Maybe your skills are rusty, but surpass mine by a lot! Thank you again. – RoberRM Apr 28 '18 at 15:02
• By the way, if you ever find that "direct way using Style" or any other that is simpler, please do share it (to be honest, I was trying to use Style yesterday but couldn't get it to work). Again, thank you! – RoberRM Apr 28 '18 at 15:21
• @RoberRM You're welcome, and I will. – Mr.Wizard Apr 28 '18 at 15:34

I think you're on the right track with using a style sheet. I would just add a new "Input" type style as well (I will call it "Input2"), and give it your GeneratedCellStyles option. To make it easy to switch between the "Input" and the "Input2" styles, I would also add a StyleKeyMapping option. So, the style sheet would look like:

SetOptions[
EvaluationNotebook[],
Notebook[
{
Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Output2", StyleDefinitions -> StyleData["Output"]],
CellMargins->{{5,10},{5,10}},
ShowCellLabel->False
],
Cell[StyleData["Input2", StyleDefinitions -> StyleData["Input"]],
StyleKeyMapping -> {"Tab" -> "Input"},
Background -> GrayLevel[.9],
GeneratedCellStyles -> {"Output" -> "Output2"}
],
Cell[StyleData["Input"],
StyleKeyMapping -> {"Tab" -> "Input2"}
]
},
StyleDefinitions->"PrivateStylesheetFormatting.nb"
]
]


To show the difference between the "Input" and "Input2" cell styles, I gave the "Input2" cell style a gray background. Just remove the Background option if it's not needed.

In the following animation, I use Tab at the start of the "Input" cell to toggle back and forth between the "Input" and the "Input2" cell styles. I also toggle the cell styles, and reevaluate the input with both cell styles:

• Wow! This option is also beautiful!! If I use this solution I can keep the visible code cleaner for my students (although they might wonder why they cannot achieve the same result that I do). I'll have to think long and hard on which of the two I'll finally implement. But thank you sooo much for this solution! (Too bad I can't mark both of your posts as the final answer.) Oh! And thank you also because I didn't know I could use GeneratedCellStyles -> {"Output" -> "Output2"} in the definition of an input cell. – RoberRM Apr 28 '18 at 15:43
• @Carl Somehow you dropped StyleDefinitions -> before the Notebook expression. Also your solution works for me only if GeneratedCellStyles is replaced with "GeneratedCellStyles". Thank you for sharing! – Alexey Popkov May 24 '18 at 2:57

Another approach I always use is to make the input cell that really created the interactive output Closed. When a cell is Closed it is very small and has a barely noticeable cell bracket. What you do it select the cell bracket on the right side of the notebook for the Input you don't want people to mess with. Then make the menu selection [Cell, Cell Properties, Open]. Once "Open" is not checked, the selected cell(s) will be Closed (barely noticeable).

Below the Input with Manipulate is Closed.

You can also have an Input cell just before the closed Input cell and make sure they are almost the same. Then you use the same menu to ensure the first Input cell is not Evaluatable.

• I don't think your answer addresses the OP question. He was concerned about the appearance of the output cell, not the input cell. – Carl Woll May 23 '18 at 2:23
• I added a part about another Input cell that is not Evaluatable, This might be an easier way to give the look and feel the OP is looking for. – Ted Ersek May 24 '18 at 0:20
• @TedErsek I'm afraid Carl is right: I was asking for a way to permanently change the style of a single Output cell. Nevertheless, thank you for your answer, Ted; you have taught me a new way to easily hide Input cells (even though that was not what I asked). – RoberRM Jun 7 '18 at 0:27