Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
2  
Mr. Wizard is still clearly THE Wizard +1 –  Jagra Oct 12 '12 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. –  Mr.Wizard Oct 12 '12 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. –  zhermes Oct 13 '12 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. –  Mr.Wizard Oct 13 '12 at 22:52
1  
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] –  dmikalova Feb 23 '13 at 0:09
show 6 more comments

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.

share|improve this answer
    
Yeah, still cumbersome --- but it's definitely a step in the right direction! Thanks @Jagra –  zhermes Oct 12 '12 at 18:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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