4
$\begingroup$

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.

Thank you for reading this and for your time.

$\endgroup$
2
  • $\begingroup$ 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[ . . . ]. $\endgroup$
    – Mr.Wizard
    Apr 28, 2018 at 14:37
  • $\begingroup$ 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.) $\endgroup$
    – RoberRM
    Apr 28, 2018 at 14:56

4 Answers 4

3
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – RoberRM
    Apr 28, 2018 at 15:02
  • $\begingroup$ 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! $\endgroup$
    – RoberRM
    Apr 28, 2018 at 15:21
  • $\begingroup$ @RoberRM You're welcome, and I will. $\endgroup$
    – Mr.Wizard
    Apr 28, 2018 at 15:34
6
$\begingroup$

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:

enter image description here

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – RoberRM
    Apr 28, 2018 at 15:43
  • $\begingroup$ @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! $\endgroup$ May 24, 2018 at 2:57
2
$\begingroup$

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).

Closing a Cell

Below the Input with Manipulate is Closed.

A Closed Cell

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.

$\endgroup$
3
  • 2
    $\begingroup$ I don't think your answer addresses the OP question. He was concerned about the appearance of the output cell, not the input cell. $\endgroup$
    – Carl Woll
    May 23, 2018 at 2:23
  • $\begingroup$ 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. $\endgroup$
    – Ted Ersek
    May 24, 2018 at 0:20
  • $\begingroup$ @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). $\endgroup$
    – RoberRM
    Jun 7, 2018 at 0:27
2
$\begingroup$

It sounds like your use-case was similar to mine, where you just need certain settings to be always applied to a single output cell, and be re-applied e.g. if that cell is deleted and re-created.

Note that GeneratedCellStyles can be applied directly to a single cell programatically, without touching a single stylesheet:

SetOptions[EvaluationCell[], 
 "GeneratedCellStyles" -> {"Output" -> {"Output", Bold, Red, CellMargins -> {{5, 10}, {5, 10}}}}]
abc

I'm surprised this isn't a more-discussed feature!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.