3
$\begingroup$

The following:

a="FormBox[RowBox[{SuperscriptBox["\:f74d", RowBox[{"\:f74e", " ", "\:03b8"}]], "=", RowBox[{RowBox[{"x", " ", SuperscriptBox["sin", "2"]}], "+", RowBox[{"x", " ", SuperscriptBox["cos", "2"]}]}]}], TraditionalForm]";
Style[a,FontSize->20]

displays with a leading " but no trailing ". I would expect the output to be "expression" or expression but getting "expression is obviously a bug. How can I fix this?

As you can see, I am storing a math expression with typesetting that may have a collision with the = assignment operator. What is the preferred method for assigning an expression that contains an unevaluated = operator? Is using a string the only way?

using mathematica 12 cloud based IDE.

$\endgroup$
3
  • $\begingroup$ What are those special characters at the beginning of your string? Would it work for you to just wrap your expression in Hold? $\endgroup$
    – MarcoB
    Commented Dec 27, 2020 at 22:03
  • $\begingroup$ the special characters were put there by mathematica after escaping to type exp e. after I posted I found out HoldForm works. This has a new side affect of turning the expression into an atomic that can not be edited after it is converted. $\endgroup$
    – acacia
    Commented Dec 27, 2020 at 22:08
  • $\begingroup$ I get this (Mac, V12.2), which looks fine, I think: i.sstatic.net/QQQri.png $\endgroup$
    – Michael E2
    Commented Dec 27, 2020 at 22:48

1 Answer 1

6
$\begingroup$

When I copied and pasted your code into a V12.1.1 notebook on my system (MacOS 10.13.4), I got the following:

code_image

which doesn't display the problem you experienced. So I don't think there is any bug involved, just something corrupted in your notebook. You might try evaluating your expression in a clean notebook to see if that makes the problem go away.

But you also ask for recommendations of better practice. I recommend the following.

a = HoldForm[E^(I θ) == Cos[θ] + I Sin[θ]]

out.1

With this definition of a, you can still style the held expression.

Style[TraditionalForm @ a, 20, "TR"]

out.2

You can also do all kinds of other things with the expression held by a by using ReleaseHold. Here are a couple of examples:

ReleaseHold[a] // Simplify
True
{x, y} = Cases[ReleaseHold[a], u : (Sin | Cos)[θ] :> u, All]; {x, y}
{Cos[θ], Sin[θ]}

Update

It occured to me that some people might prefer

{x, y} = Simplify[ReIm[ReleaseHold[a][[2]]], θ ∈ Reals]

to the 2nd example given above; it gives the same result. Of course, this works, too:

{x, y} = FullSimplify[ReIm[ReleaseHold[a][[1]]], θ ∈ Reals]

After all, equality is equality 😀 (see 1st example above for for confirmation of equality).

$\endgroup$

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.