# Write commands in a texteditor as a low-level notebook expression

I want to write a notebook file by myself. I use a text editor (such as Vim) to do that, for the purpose of having full control of my code I am writing, and to really understand what is in my .nb file when I read it later on my version control repository.

So, I headed over to the following Wolfram Tutorial, Manipulating Notebooks, and learned that it is possible, and I am rather enthusiastic about it!

But I got stuck in a quite basic stage:

For example, how do I create a button, that when clicked prints the number $$4\cdot5$$, for example?

In the front-end, one just does

(*20*)

Using the "Cell"->"Show Expression" tool (or pressing ++e, I get the following text:

Cell[BoxData[
RowBox[{"Button", "[",
RowBox[{"Print", "[",
RowBox[{"4", "*", "5"}], "]"}]}], "]"}]], "Input",
CellChangeTimes->{{3.835353874007769*^9, 3.835353879618236*^9}},
CellLabel->"In[10]:="]

Ok so I guess I can delete the CellLabel and CellChangeTimes options for Cell, but is there any possibility to make this Click here! button shorter when typed by a human in a text editor?

Do I have to take care for all those ", } and RowBoxs?

Even a simple task such as assigning the symbol x a numerical value seems too long:

Cell[
BoxData[
RowBox[{"x", "=", "2"}]], "Input"]

Is it possible to shorten it?

My .nb now is this:

(*Low level notebook*)
Notebook[{
Cell[
"Some text","Text"
]
}
,WindowFrameElements->{"MinimizeBox","CloseBox","DocumentIcon","ResizeArea"}
,Saveable->False
,WindowTitle->"My Awesome Notebook"
]
• I would seriously reconsider this. As you have seen, the box structure of notebook files is extremely verbose, even if technically readable. If version control is your main goal, I would suggest you look into package (.wl) files. And a side node: Your button example is not really correct: What you are showing is the cell expression of the input cell that will create the button, instead of the output cell that contains the button (that one is at least a bit simpler than what you show) Jul 15, 2021 at 16:41
• @Lukas Lang I will certainly check the .wl files you mentioned, but let me ask again: there is no shortcut I can make, at least regarding the two examples I have presented above? Moreover, can you show me the "more" correct command for the button cell?
– tush
Jul 15, 2021 at 17:35
• @tush - when I evaluate your Button expression, the output cell expression is Cell[BoxData[ButtonBox["\<\"Click here!\"\>", Appearance->Automatic, ButtonFunction:>Print[4 5], Evaluator->Automatic, Method->"Preemptive"]], "Output", CellChangeTimes-> ..., CellLabel->....] Jul 15, 2021 at 17:49
• @tush One of the main reasons why .nb files do not play nice with version control systems, is that each time you evaluate one (even if nothing changes), Mathematica will still modify quite some cell properties such as ExpressionUUID, CellChangeTimes etc. And in the more recent MMa versions there is no way to turn it off, cf. link. So for all book keeping purposes, .wl/.m files are a much better choice.
– vsht
Jul 16, 2021 at 9:06
– tush
Jul 16, 2021 at 9:17

As noted in the comments, I would advise against doing this. Package files (.wl) are designed to be plaintext and are as such way more compatible with version control and text editors.

That being said, there are a few things you could do to make your code more readable:

• Omit input cells if you are interested only in the output: For your button example, you can just include the output cell containing the button:

Cell[BoxData[
Appearance -> Automatic,
ButtonFunction :> Print[4 5],
Evaluator->Automatic,
Method->"Preemptive"
]],"Output"]

This gets you around one layer of box structure. (In this particular example, you need to write quite a bit of stuff, but this can help in many cases)

• Strings can often be simplified from "\"\<String\>\"" to "\"String\"", see this answer for more details.

• Identify and omit options and boxes that are unnecessary: Grid@{{a}} for example produces the following output cell structure:

Cell[BoxData[
TagBox[GridBox[{
{"a"}
},
AutoDelete->False
],
"Grid"]], "Output",
CellContext->"Cell11425",
CellLabel->"Out[8]=",
CellID->531179843]

You already noticed that CellLabel is not really needed. In fact, the following will be virtually identical in most cases:

Cell[BoxData[
GridBox[{
{"a"}
}]], "Output"]

• Note: Thanks to @ihojnicki we know that the following is undocumented, and that the behavior described should not be counted on. Use at your own risk!

Apparently, the following works: