Mathematica Output Display

  • Mathematica
  • Thread starter NeoDevin
  • Start date
  • #1
299
1

Main Question or Discussion Point

Is there any way to make Mathematica display the variable name in the output?

For example, if I have the following:
Code:
In[1]:= A = 1 + 2 + 3
By default Mathematica outputs this:
Code:
Out[1]:= 6
I would like it to instead output something like this:
Code:
Out[1]:= A = 6
Or in some other way indicate what variable the output value corresponds to. Often times I have large cells with may lines that I want printed out, so I can copy the values into other documents. With the default output, I have to keep track of which line of output corresponds to which variable, which is a nuisance when there are 20-30 lines of output.
 

Answers and Replies

  • #2
1,069
29
SetAttributes[nameAndValue, HoldFirst];
nameAndValue[x_] := Print[Unevaluated[x], "=", x];
a = 4;
nameAndValue[a]
 
  • #3
299
1
Thanks for the reply, that will do the trick.

I don't suppose there's an option anywhere to make this (or something similar) the default behavior? Like it is in Matlab.
 
  • #4
1,069
29
There is $Pre and $Post which can be expressions that process expressions before and after MMA handles them.

That is not without some risk, but MMA help and a search in the right places can find you a few examples of how others have used this.

If you have lots of examples where you have dozens of lines of output for every variable then you might consider whether there might be different ways of coding that do not have this.
 
Last edited:
  • #5
299
1
If you have lots of examples where you have dozens of lines of output for every variable then you might consider whether there might be different ways of coding that do not have this.
Mostly it's just one output for any given variable. For example, in a homework assignment, where they ask for a bunch of values along the way.

Thanks for the help, I'll look into those.
 

Related Threads on Mathematica Output Display

  • Last Post
Replies
3
Views
2K
Replies
2
Views
3K
Replies
2
Views
724
Replies
1
Views
2K
Replies
5
Views
8K
Replies
2
Views
3K
Replies
5
Views
7K
Replies
1
Views
3K
Replies
2
Views
2K
Top