2
$\begingroup$

I don't like how initialization (or "Code") cells are formatted in notebooks, namely the grey background color and the spacing/wrapping settings. I want to make them look exactly as if they were normal input cells (other than the Dingbat). However, I need to use them to later export my notebooks as .wls files.

My best attempt so far is to set new cells to be Initialization Cells by default, using the Options inspector in the Format menu, and setting InitializationCellStyle to something different than the default "InitializationCell" value.

This is:

Format ► Options Inspector ► Global Options ► Cell Options ► Evaluation Options ► IntitializationCell = True

Format ► Options Inspector ► Global Options ► Cell Options ► Display Options ► Private Cell Options ► InitializationCellStyle = ""

This successfully makes new cells be created as Initialization Cells with the usual "Input" format. However, cells that are already formatted as "Code" cells will not fully revert to the original format (only the background color seems to revert to white).

I have also tried other values for InitializationCellStyle such as "Input", but most seem to have the same behavior. I have not been able to find documented possible values for that setting.

I have many different files that I would like to convert to the normal format without needing to copy the cells to new ones.

Is there any way to revert cells already formatted as "Code" to the original "Input" format?

$\endgroup$

2 Answers 2

2
$\begingroup$

Probably the simplest solution:

SetOptions[EvaluationNotebook[], 
 StyleDefinitions -> Notebook[{
    Cell[StyleData[StyleDefinitions -> "Default.nb"]], 
    Cell[StyleData["InitializationCell", StyleDefinitions -> StyleData["Input"]]]}]]

Strongly related:

$\endgroup$
4
  • $\begingroup$ Are there any other Style settings (other than the transparent background) that are also characteristic of Input Cells vs Code Cells? In appearance setting the background to transparent makes them look identical to the sight, but is there something hidden that still differs? $\endgroup$
    – abcd
    Mar 5, 2022 at 12:16
  • 1
    $\begingroup$ @abcd If you are speaking about "Input" vs "Code" cells, yes. But "InitializationCell" is something special. $\endgroup$ Mar 5, 2022 at 12:24
  • $\begingroup$ Is it correct to say that ctrl+8 just makes cells InitializationCell, while cmd+8 makes them additionally Code, which has further differences such as the wrapping setting? $\endgroup$
    – abcd
    Mar 5, 2022 at 12:40
  • 1
    $\begingroup$ @abcd I hadn't investigated the differences, but CurrentValue[{StyleDefinitions, "InitializationCell"}] returns information only about cell background what may be wrong. $\endgroup$ Mar 5, 2022 at 12:47
2
$\begingroup$

Does:

SetOptions[
    EvaluationNotebook[],
    StyleDefinitions -> Notebook[{
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[StyleData["InitializationCell"], Background->None]
    }]
]

do what you want?

$\endgroup$
1
  • $\begingroup$ Hi Carl, thanks for the answer. It does indeed, but also I realised I need to convert them to Initialization Cells by pressing ctrl+8, instead of cmd+8 (in MacOS). I realised it was the latter what was making the spacing and wrapping settings be different (and also the font size). By setting the background to transparent and using ctrl+8 (not cmd+8), they are (in appearance) formatted exactly as normal cells. $\endgroup$
    – abcd
    Mar 5, 2022 at 12:12

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.