2
$\begingroup$

I like the formatting for Code and would like to use it for all my input. However, I want to be able to have both Initialization and non-initialization cells and I'd like them to be clearly distinguishable.

Therefore I want to create a second non-initialization code style. Also I want to do everything programmatically rather having to find my way through menu's (and repeating this after having forgotten how whenever I need to change / re-install something etc...).

I would also like to add the style to the menu and I would like to be able to turn cells into this non-initialization-code with a key binding, for example Shift+Alt+8.

How do I do this?


My attempt:

I thought I could create a style that is at least accessible locally by running:

nb = EvaluationNotebook[];
NotebookWrite[nb, 
  Cell[StyleData["Test", StyleDefinitions -> StyleData["Code"]], 
   InitializationCell -> False, Background -> RGBColor[1, 1, 0.8], 
   FontFamily -> "Courier New", CellMargins -> {{50, 0}, {0, 0}} ]];

I thought this would inherit style options from "Code" but it seems not to inherit everything. The most obvious of which are the very weird CellMargins putting the cell in the margin if we don't explicitly put them here.

Now how do I go about changing a cell into this style?

(I tried using https://mathematica.stackexchange.com/a/241025/45020 with

testCell = EvaluationCell[];
1 + 1

followed by ChangeStyle[testCell, Code] or ChangeStyle[testCell, Test]. The first worked. Changing the same cell again did nothing probably due to this issue Why can't I change the style of a cell repeatedly?. The second gave a random weird style (The same one it gives if you just make up a non-existant style name.).

$\endgroup$

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.