How create text label of form “var = ”, var properly formatted

If I use

   z = 99;


then as output I get a property formatted math expression $z = 199$.

How do I accomplish the same thing without having that 1 after ==?

In other words, I want to be able to crate a Row expression that will display a properly formatted math expression of the form $\text{varName} = \text{varValue}$?

The issue is subtler than it may first seem. The real difficulty is getting the equal sign to appear as normal size, not longer than it should be (as using == might cause) and to have proper, typographically correct, spacing before and after the equal sign.

Of course, a good solution should generalize to more complicated expressions, such as producing typographically correct expressions as $\sin(2 \pi z) = 1/\sqrt{2}$ after setting z = 1/8.

• z = 99; StringForm["z = ", z] // TraditionalForm – Bob Hanlon Oct 21 '15 at 1:19
• z = 99; DisplayForm[FormBox[RowBox[{"z", "=", z}], TraditionalForm]]? – J. M.'s technical difficulties Oct 21 '15 at 1:21
• TraditionalForm@Row[{HoldForm[z], " = ", z }] – Mike Honeychurch Oct 21 '15 at 2:01
• The trouble with things like TraditionalForm@Row[{HoldForm[z], " = ", z }] is that the " = " part is pure text and doesn't produce reasonable mathematical typography -- too much space around the equal sign. – murray Oct 21 '15 at 16:38
• @J.M.: your solution does give proper spacing appropriate to good mathematical typography. Alas, it doesn't seem to generalize to typesetting things like $\sin(2 \pi z) = 1/\sqrt{2}$ after setting z=1/8. – murray Oct 21 '15 at 16:48

I usually do something like this:

z = 99;


• This typesets OK -- provided that you change == to =. – murray Oct 23 '15 at 20:31

You might try this.

SetAttributes[label, HoldFirst];
label[var_Symbol] :=
Module[{textForm},
textForm = First @ StringSplit[SymbolName[Unevaluated[var]], "\$"];
ToExpression[textForm <> "==" <> ToString[var], StandardForm, HoldForm]]


Then

z = 42; label[z]


gives

It also handles this

z = Sin[2 π t]; label[z]


and this

Module[{z = 42}, label[z]]


• The equal sign from label[z] comes out too long, not like a proper equal sign in mathematical typography. – murray Oct 21 '15 at 16:40
• @murray. You specified "==" in your question, and that's the glyph you get from Mathematica in traditional form to represent "==". Try specifying "=" instead. You may like the results better. – m_goldberg Oct 21 '15 at 22:32
• Yes, that will work with = instead of ==. I had not tried the former. But the answer by Michael E2 seems a lot simpler. – murray Oct 23 '15 at 20:32
• @murray. Michael's answer is simpler and if it meets your needs, use it. My answer is more general; it works inside Module. – m_goldberg Oct 23 '15 at 22:09

Why not just use HoldForm in concert with Set/= in order to get the typesetting you want, and use HoldFirst to capture the expression, while With substitutes in the value?

Attributes[EquationForm] = {HoldFirst};
EquationForm[expr_] :=
With[{value = expr},

z = 1/8;

• In my situation, the variable z already has a value, so using a Rule would require, I think, awkward multiple replacements. – murray Oct 24 '15 at 15:12