5
$\begingroup$

If I use

   z = 99;
   Row[{TraditionalForm@HoldForm[z == 1], z}]

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.

$\endgroup$
  • $\begingroup$ z = 99; StringForm["z = ``", z] // TraditionalForm $\endgroup$ – Bob Hanlon Oct 21 '15 at 1:19
  • $\begingroup$ z = 99; DisplayForm[FormBox[RowBox[{"z", "=", z}], TraditionalForm]]? $\endgroup$ – J. M.'s technical difficulties Oct 21 '15 at 1:21
  • 1
    $\begingroup$ TraditionalForm@Row[{HoldForm[z], " = ", z }] $\endgroup$ – Mike Honeychurch Oct 21 '15 at 2:01
  • $\begingroup$ 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. $\endgroup$ – murray Oct 21 '15 at 16:38
  • $\begingroup$ @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. $\endgroup$ – murray Oct 21 '15 at 16:48
1
$\begingroup$

I usually do something like this:

z = 99;
Row[{TraditionalForm@HoldForm[z == ""], z}]

Mathematica graphics

$\endgroup$
  • 1
    $\begingroup$ This typesets OK -- provided that you change == to =. $\endgroup$ – murray Oct 23 '15 at 20:31
6
$\begingroup$

You might try this.

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

Then

z = 42; label[z]

gives

z-out

It also handles this

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

sin-out

and this

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

z-out

$\endgroup$
  • $\begingroup$ The equal sign from label[z] comes out too long, not like a proper equal sign in mathematical typography. $\endgroup$ – murray Oct 21 '15 at 16:40
  • $\begingroup$ @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. $\endgroup$ – m_goldberg Oct 21 '15 at 22:32
  • $\begingroup$ Yes, that will work with = instead of ==. I had not tried the former. But the answer by Michael E2 seems a lot simpler. $\endgroup$ – murray Oct 23 '15 at 20:32
  • $\begingroup$ @murray. Michael's answer is simpler and if it meets your needs, use it. My answer is more general; it works inside Module. $\endgroup$ – m_goldberg Oct 23 '15 at 22:09
2
$\begingroup$

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},
  TraditionalForm[HoldForm[expr = value]];

Then you can enter:

z = 1/8;
EquationForm[Sin[2*Pi*z]] 

and get

typeset equation

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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