# Notebook formatting - easier descriptions for equations and results?

When I do computations in mathematica, I generally try to make it readable for myself-in-the-future. This can be a cumbersome task in mathematica, where it often requires me to switch betwee a text cell, then back to an equation cell (or two), over and over again, to be able to label what a given equation/result is.

e.g.

(text:) This is the equation for ... .... ...
In[517]:= 2 + 2
Out[517]= 4


over and over again. Is there anyway to make mathematica produce output like the following?

In[517]:= 2 + 2  This is the ... Equation
Out[517]= 4      The resulting value ...

• You can add comments to input lines using (* ... *), but doctoring output will turn the formatting into input style. Commented Oct 12, 2012 at 13:53
• You may want to look at this question and the answers provided there. Basically, what you want seems to be possible by using inline cells. You can use cell options Evaluatable->True and CellEvaluationFunction to tweak the evaluation so that it only evaluates the input code part of your larger cell. Commented Oct 12, 2012 at 14:47
• @LeonidShifrin -- What a great idea. I missed it first time around. Commented Oct 12, 2012 at 18:15
• Thanks @LeonidShifrin, that does provide the resulting look I wanted, but its again far too laborious to be convenient Commented Oct 12, 2012 at 18:47
• Tangentially related: mathematica.stackexchange.com/q/1047/121 Commented Mar 8, 2013 at 17:08

An interesting problem.

Trivially one could use Print like this:

Print[2 + 2, Spacer[50], "this is a note"]

4      this is a note


But that is hardly a usable syntax. Looking deeper into the system one observes that (* comments *) are stripped during parsing so those are out of reach without prohibitive contortions. Strings however are inert objects that could serve as comments. I propose this:

### Version 2

$note = Null;$PreRead =
Replace[#,
RowBox[{body__, ";", note_String?(StringMatchQ[#, "\"*\""] &)}] :>
($note = Style[ToExpression@note, Italic, Red]; RowBox[{body}]) ] &;$PrePrint =
If[$note =!= Null, # &[Row[{Pane@#, Spacer[50],$note}], $note = Null], # ] &;  (This code uses a new Global symbol $note -- in practice it may be better to place this in a different context to prevent collisions and allow for e.g. ClearAll["Global*"] without issue.)

This has at least the limitation of the way that Mathematica reflows text in standard Input cells. Here the last comment, "Graphics", wraps to the next line:

When using this implement it may be preferable to use a cell style that does not reflow, such as Code:

Pane in the $PrePrint function is to handle a case such as the last line above; without it Graphics objects are incorrectly downsized. Note that in version 2, because labeling is handled with $PrePrint, the functionality of Out remains intact allowing %^2 in the example and maintaining Output cell labels.

• Mr. Wizard is still clearly THE Wizard +1 Commented Oct 12, 2012 at 23:17
• @Jagra Thanks for the kind words. If this method is put into use it would probably be better to use a method besides Print as this doesn't create any actual "Output" cells. Consider this answer, at present, a proof of concept. Better output formatting and integration into the In[] / Out[] system are yet to be done. Commented Oct 12, 2012 at 23:52
• This is quite nice! Thanks @Mr.Wizard. As you point out -- creating actual "Output" is pretty important, so I haven't accepted this as 'the answer' just yet. Commented Oct 13, 2012 at 22:44
• @zhermes okay, but you like the input syntax? If you do I'll see what I can do about proper output, otherwise I'll go back to the drawing board. Commented Oct 13, 2012 at 22:52
• This is really great for my homework! I changed it to print before by changing to Row[{ Pane@#, $note, Spacer[5]}] because I usually note things as "This$quantity is ". I also fixed the reflow problem by changing LineBreakAdjustments for my Input style in my stylesheet to {1,2,7,0,1} The first and third parameters come out as the most important parameters and if you've ever been dissatisfied with Mathematica reflowing this may make you happy. Setting c from 7 to 4 won't break the string, and if you want to allow breaking in the string set it to 3 to 0. [fixed] Commented Feb 23, 2013 at 0:09

The best I can think of gets a bit cumbersome, but if you did everything this way one could deal with it consistently:

In[517]:= {2 + 2, "The resulting value ...'} (* This is the ... Equation *)

Out[517]= {4, The resulting value ...}

In[518]:= %[[1]] (* To use just the value *)

Out[518]= 4


Again quite cumbersome.

• Yeah, still cumbersome --- but it's definitely a step in the right direction! Thanks @Jagra Commented Oct 12, 2012 at 18:48