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$
7
  • $\begingroup$ z = 99; StringForm["z = ``", z] // TraditionalForm $\endgroup$
    – Bob Hanlon
    Commented Oct 21, 2015 at 1:19
  • $\begingroup$ z = 99; DisplayForm[FormBox[RowBox[{"z", "=", z}], TraditionalForm]]? $\endgroup$ Commented Oct 21, 2015 at 1:21
  • 1
    $\begingroup$ TraditionalForm@Row[{HoldForm[z], " = ", z }] $\endgroup$ Commented Oct 21, 2015 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
    Commented Oct 21, 2015 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
    Commented Oct 21, 2015 at 16:48

3 Answers 3

1
$\begingroup$

I usually do something like this:

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

Mathematica graphics

$\endgroup$
1
  • 1
    $\begingroup$ This typesets OK -- provided that you change == to =. $\endgroup$
    – murray
    Commented Oct 23, 2015 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$
4
  • $\begingroup$ The equal sign from label[z] comes out too long, not like a proper equal sign in mathematical typography. $\endgroup$
    – murray
    Commented Oct 21, 2015 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
    Commented Oct 21, 2015 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
    Commented Oct 23, 2015 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
    Commented Oct 23, 2015 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$
1
  • $\begingroup$ In my situation, the variable z already has a value, so using a Rule would require, I think, awkward multiple replacements. $\endgroup$
    – murray
    Commented Oct 24, 2015 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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