2
$\begingroup$

This question relates to Mathematica 12.3 (and possibly many other versions).

Mathematica's automatic indentation does not conform to my workflow. From the very old days of Mathematica 1.2, i.e. before the introduction of the notebook frontends, I am still used to writing in indentation notation with one brace or bracket below the matching one. At the end of a construct, I insert additional line breaks so that I can align the closing parenthesis to match the beginning of the construct.

However, the frontend prevents me from doing this when I use Input cells. I could circumvent this by using Code cells, but they have other drawbacks.

I found out that you can disable Mathematica's indentation in Input cells by changing the Input cell in your own Default.nb like this (it is an example of my favorite indentation style):

Cell[StyleData["Input"]
, AutoIndent -> False    (* 2020-12-08 *)
, AutoSpacing -> False   (* 2020-12-08 *)
, FontSize -> 13
, CellMargins->{{66, 10}, {5, 8}}
, StyleKeyMapping->{"=" -> "WolframAlphaShort"
  , "*" -> "Item"
  , ">" -> "ExternalLanguageDefault"
  }
]

But this only works on new notebooks. For an existing one, you do this:

  1. call Format>OptionInspector: Show Option Values>Selected Notebook and set Lookup: AutoIndent to False, then click Apply,
  2. set Lookup: AutoSpacing to False, then click on Apply.

(If I do this with Show Option Values>Global, it still doesn't work on old notebooks after they have been loaded).

  • How can I combine steps 1. and 2. programmatically?
  • Can I make it happen automatically after I open an existing notebook?

My favorite indentation scheme:

Because function names, which precede a square bracket, practically belong to the bracket following them, I treat the function name regarding the indentation like an opening bracket. Then the closing square bracket is in the same indentation depth as the function name before the opening square bracket (and of course the closing bracket is not in-column with its particular opening one).

Mathematica syntax separates parameters and list items by commas. If you want to see where the next parameter or list element begins in an indented text, you have to keep looking back and forth between the beginning of the indented text and the end of the line. This is exhausting and tiring, and it is easy to miss a comma.

You can also consider the commas as parameter separators or between list elements as belonging to the same indentation order as the corresponding opening and closing brackets, braces or parentheses. In fact, the coloring of syntax elements during editing in the frontend, it highlights the associated brackets together with the keyword before them, e.g. if you click before a comma. My personal view on the syntax elements seems to be present among at least some developers of Mathematica).

My preferred indentation takes up more space in the vertical direction than absolutely necessary, but it makes it easier to see the relationships.

$\endgroup$
4
  • 2
    $\begingroup$ That is why I use code cells to write code in and not input cells. This way I have full control of formatting myself without the frontend formatting getting in the way. I $\endgroup$
    – Nasser
    Jun 14 at 8:24
  • 1
    $\begingroup$ The disadvantage of code cells is that they do not automatically wrap lines. If the window is too narrow, you can miss something at the end of the line, which disappears behind the boundary of the window. Fortunately, the light green markings on brackets also work in code cells in my version. $\endgroup$ Jun 14 at 11:51
  • $\begingroup$ @AdalbertHanßen I recommend that you remove or condense the discussion of your preferred indentation scheme at the end of the question. You already provided a clear example in code, and your question is not about the indentation scheme itself anyway, but about how to enforce any such scheme in existing notebooks. $\endgroup$
    – MarcoB
    Jun 14 at 14:42
  • $\begingroup$ You can enter a pair of quotes in Mathematica's notebook, then paste the code in, finally delete the quotes outside. $\endgroup$
    – chyanog
    Jun 15 at 5:43

1 Answer 1

3
$\begingroup$

If I understand the question, I think you have to create a stylesheet. Using a document created with the Default stylesheet, this should work:

  1. Format > Edit Stylesheet...
  2. Base definitions will be taken from Default.nb. Select the Input style.
  3. Cell > Show expression...
  4. Edit the expression as you suggested and then Cell > Show expression
  5. Click Export and save the style sheet
  6. Select an existing Notebook and use Format>Stylesheet>Other... and select your saved stylesheet to apply it to the notebook.

Note: saving the style sheet in the directory returned by FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "StyleSheets"}] will result in the stylesheet name appearing in the Format>Stylesheet submenu.

$\endgroup$
1
  • $\begingroup$ Jean-Pierre, thank you for your hint. I already had copies of Core.nb, Default.nb and Package.nb in that place. - I wanted to call the file Default.nb in order to act that way. However, Mma would not let me store it in this place under this name: The file's datestamp just did not update. I had to store it under another name and change the file name later in my file browser to become Default.nb. I guess, some setting in OptionsInspector prevented me from modifying this file. $\endgroup$ Jun 22 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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