# Want to return name of last defined variable without explicitly naming it

Suppose I have this code:

dec = 7.1 + 6.6 + 27.2 + 66.5
jan = 7.1 + 6.6 + 27.2 + 66.5 + 65.7
feb = 7.1 + 6.6 + 27.2 + 66.5 + 65.7 + 69.7


And I want MMA to return:

dec: 107.4
jan: 173.1
feb: 242.8


I could accomplish this as follows:

dec = 7.1 + 6.6 + 27.2 + 66.5;
Print["dec: ", %]
jan = 7.1 + 6.6 + 27.2 + 66.5 + 65.7;
Print["jan: ", %]
feb = 7.1 + 6.6 + 27.2 + 66.5 + 65.7 + 69.7;
Print["feb: ", %]


But I have a long list of variables, and don't want to retype all their names. So I'd instead prefer to just copy and paste the same code in after each line, e.g., something like this:

dec = 7.1 + 6.6 + 27.2 + 66.5;
Print[SymbolName[%],": ", %]
jan = 7.1 + 6.6 + 27.2 + 66.5 + 65.7;
Print[SymbolName[%],": ", %]
feb = 7.1 + 6.6 + 27.2 + 66.5 + 65.7 + 69.7;
Print[SymbolName[%],": ", %]


Of course, the above doesn't work. But is there simple code that would?

• Not quite right, but maybe a start: foo = 10; HoldForm[In[#]] &[$Line - 1] /. DownValues[In] – Michael E2 Dec 11 '16 at 3:03 • You may have more success by posting your follow-up questions as separate questions. It seems to me that formatting the output in this new way is a separate question from the original question. – Michael E2 Dec 18 '16 at 18:08 • @MichaelE2: Good suggestion, I'll do that. That was in fact what I thought made most sense to do, but I wasn't sure if it would violate site etiquette, so thanks for letting me know this is an acceptable, indeed preferred, option. – theorist Dec 23 '16 at 17:59 ## 2 Answers I think there must be a better way to accomplish the OP's ultimate goal than the general approach outlined above. But be that as it may, the following culls all the Set[] commands from the last input line (so it has a little broader scope than what was asked for). lastInputSets[] := Block[{Set}, Grid@Cases[Hold[In[#]] &[$Line - 1] /. DownValues[In],
Set[lv_, v_] :> {HoldForm[lv], ": ", lv}, Infinity]
];


Examples:

foo = 10; murf = 20 + 30;
lastInputSets[]


The OP's example:

dec = 7.1 + 6.6 + 27.2 + 66.5;
lastInputSets[]
jan = 7.1 + 6.6 + 27.2 + 66.5 + 65.7;
lastInputSets[]
feb = 7.1 + 6.6 + 27.2 + 66.5 + 65.7 + 69.7;
lastInputSets[]


Or all at once:

(dec = 7.1 + 6.6 + 27.2 + 66.5;
jan = 7.1 + 6.6 + 27.2 + 66.5 + 65.7;
feb = 7.1 + 6.6 + 27.2 + 66.5 + 65.7 + 69.7;)
lastInputSets[]


• Thanks, this has the advantage that it works even if the symbol wasn't newly defined. – theorist Dec 11 '16 at 3:57

If these symbols are newly defined, you could do

$NewSymbol = (lastsymbol = #) &; dec = 7.1 + 6.6 + 27.2 + 66.5; Print[lastsymbol, ": ", %] jan = 7.1 + 6.6 + 27.2 + 66.5 + 65.7; Print[lastsymbol, ": ", %] feb = 7.1 + 6.6 + 27.2 + 66.5 + 65.7 + 69.7; Print[lastsymbol, ": ", %]  dec: 107.4 jan: 173.1 feb: 242.8 In response to the comment -- as a more general purpose tool, one could use $PrePrint to automatically print the input and output from each evaluation. This uses Downvalues[In] as in @MichaelE2's answer:

$PrePrint = ( Print[TraditionalForm@HoldForm[In[line] = #] /. line ->$Line /. DownValues[In]]; #
) &;


Now you have, for example:

Integrate[x^2, x]


$\int x^2 \, dx=\frac{x^3}{3}$

x^3/3

a = 2


$(a=2)=2$

2


Or you could just set the output to appear this way, without printing on a separate line, using

$PrePrint = (TraditionalForm@HoldForm[In[line] = #] /. line ->$Line /. DownValues[In]) &;


Use $PrePrint =. to turn this off. I agree with MichaelE2, though, that depending on why you want to do this, there's probably a better way to go about it. • Thanks, that works. Is there a way to generalize this to show whatever commands or symbols you've entered, followed by the result? For instance, suppose I enter: nov16/nov*100 which returns: 136.919 Is there a way to get MMA to instead return: nov16/nov*100: 136.919 ....without explicitly naming what I entered? Or, alternately, suppose I enter: Int[x^2, x] ...can I get MMA to return: [Integral]x^2 [DifferentialD]x = x^3/3 – theorist Dec 11 '16 at 3:36 • ...since I can't edit last comment any more: The last one was supposed to ask if I could get MMA to display the indefinite integral of x^2, in traditional form, on the LHS, and the result on the RHS, again without having to explicitly re-specify what I'd entered. Or should I post these as separate questions? – theorist Dec 11 '16 at 3:44 • @theorist One could adapt my history-based approach to show the whole input like this: showMe[n0_Integer: -1] := Row[With[{n = If[n0 < 0,$Line + n0, n0]}, {HoldForm[In[n]] /. DownValues[In], ":>", Out[n]}]] Then showMe[] or showMe[-1] shows the previous input and output. – Michael E2 Dec 11 '16 at 15:03