19
$\begingroup$

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 ...
$\endgroup$
5
  • $\begingroup$ You can add comments to input lines using (* ... *), but doctoring output will turn the formatting into input style. $\endgroup$ Oct 12, 2012 at 13:53
  • 1
    $\begingroup$ 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. $\endgroup$ Oct 12, 2012 at 14:47
  • $\begingroup$ @LeonidShifrin -- What a great idea. I missed it first time around. $\endgroup$
    – Jagra
    Oct 12, 2012 at 18:15
  • $\begingroup$ Thanks @LeonidShifrin, that does provide the resulting look I wanted, but its again far too laborious to be convenient $\endgroup$ Oct 12, 2012 at 18:47
  • $\begingroup$ Tangentially related: mathematica.stackexchange.com/q/1047/121 $\endgroup$
    – Mr.Wizard
    Mar 8, 2013 at 17:08

2 Answers 2

14
$\begingroup$

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.)

Comments are made as Strings ending a line and following semicolon.

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:

Mathematica graphics

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

Mathematica graphics

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.

$\endgroup$
13
  • 2
    $\begingroup$ Mr. Wizard is still clearly THE Wizard +1 $\endgroup$
    – Jagra
    Oct 12, 2012 at 23:17
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Oct 12, 2012 at 23:52
  • $\begingroup$ 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. $\endgroup$ Oct 13, 2012 at 22:44
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Oct 13, 2012 at 22:52
  • 1
    $\begingroup$ 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] $\endgroup$
    – dmikalova
    Feb 23, 2013 at 0:09
3
$\begingroup$

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.

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

Your Answer

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

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