8
$\begingroup$

I am trying to use Mathematica's ToExpression[] combined with TexForm to type the Einstein's famous result: E = mc^2. Now this is dead easy in LaTeX, the code is:

E = m c^2

and it should generate something like:

enter image description here

However, when I try to generate this in Mathematica using the following syntax:

ToExpression["E = m c^2 ", TeXForm]

enter image description here

Irritatingly:

  1. 'E' is converted to the bold-faced natural exponential;
  2. The order of the variables is changed in the output.

Does anyone know how to fix these two problems?

$\endgroup$
3
  • $\begingroup$ The general topic of your question is addressed in this other question: Unable to convert TeX input into mathematica. The specific case of the letter E is addressed in ogerard's answer. $\endgroup$
    – Jens
    May 19, 2013 at 20:27
  • $\begingroup$ In addition to the above link, I just remembered an extended version of the same answer that I posted later. $\endgroup$
    – Jens
    May 19, 2013 at 20:40
  • $\begingroup$ As to the reversed order: use the third argument to TexForm as I mention in the second link above: ToExpression["U = mc^2", TeXForm, HoldForm]. I used a letter U that doesn't have a built-in meaning. $\endgroup$
    – Jens
    May 19, 2013 at 20:56

3 Answers 3

8
$\begingroup$

The variable name E isn't available in Mathematica because it's a built-in symbol, but you can define a new name for the energy and use Format to make it display as E. The compromise is that you then have to enter the $\LaTeX$ code using the new name - here I call it energy:

Format[energy, StandardForm] := "E"

ToExpression["energy = m c^2", TeXForm, HoldForm]

$\text{E} = \text{m} \text{c}^2$

Edit: In the $\LaTeX$ code, you can enter energy without wrapping it in \\text{} because Mathematica interpets it as a single word by default. I initially put in a wrapper \\text{energy} because that's how one defines a multi-letter word in $\LaTeX$ equations. This is a confusing discrepancy between $\LaTeX$ and Mathematica that works to our advantage here because it allows lazier $\LaTeX$ input.

If your ultimate goal is to display the formulas as TraditionalForm (e.g., as labels in Graphics), then change the last argument in Format from StandardForm to TraditionalForm.

$\endgroup$
2
  • $\begingroup$ Many thanks @Jens, this is good enough for what I want to achieve! Also appreciate the little 'bonus track' on the amenities of TranditionalForm over StandardForm. $\endgroup$
    – Joshua T
    May 20, 2013 at 11:46
  • 1
    $\begingroup$ Thanks - I was actually wrong about requiring \text{} wrappers. It works without that, so it's a little simpler than I wrote at first. $\endgroup$
    – Jens
    May 21, 2013 at 5:31
5
$\begingroup$

The reason of your troubles is the fact that E is not available as a variable in Mathematica but is assigned for the exp(1). A similar problem occurs when trying to use D (differential operator), I (imaginary unit), N (numerical evaluation), Pi (famous constant) as variables. This can often be frustrating, especially when using Mathematica for basic physics. Others here may have suggestions as how to accomodate this design decision. Here are my 2 cents.

Your call:

ToExpression[" stuff ", TeXForm]

is used to produce a Mathematica expression from a TeX expression. E is automatically converted to the e constant (although E is a Mathematica convention, not a TeX one) and displayed accordingly.

ToExpression["Energy = m c^2", TeXForm]

should work as expected, but I do not know an efficient way to have a variable which looks correctly like a capital E without resorting to special characters. And in this case there is no much point to using ToExpression to convert it from TeXForm.

You can enter a special E, usable as a variable if you type ( is the Escape key at the upper left of your keyboard):

<esc>E<esc> == m c^2

for instance (after typing the second esc, you will see a capital E, slightly less bold than the normal font) or

<esc>E<esc> = 10

to assign an immediate value.

Then each time you will enter E in an equation it will be treated as a variable or as its value if you give it one.

TraditionalForm["<esc>E<esc>"]

works as expected.

$\endgroup$
2
  • $\begingroup$ Thanks for your insightful diagnosis of the problem and your suggestions on how to type E without going through Latex and making sure its a usable variable! However, the reason I'm asking this question is because I want to type LaTeX code into Mathematica because I don't like typesetting of equations in Mathematica (using palette or the shortcuts associated with them). Gave you an upvote but no green tick! $\endgroup$
    – Joshua T
    May 19, 2013 at 0:06
  • $\begingroup$ Thanks and fair enough. Something you can do if you don't like Mathematica's output is to use TeXForm or "Notebook to TeX" conversion to convert a Mathematica Expression or a whole Notebook to something (or close to something) you can use in a TeX source file after you are done with your computations. $\endgroup$
    – ogerard
    May 19, 2013 at 14:11
1
$\begingroup$

I assume what "ToExpression" does is, replacing characters of a string with their Mathematica interpretation. Why don't you just try: TeXForm["E = m !(*SuperscriptBox[(c), (2)])"]

$\endgroup$
1
  • 1
    $\begingroup$ Thanks for your suggestion! TexForm is a way of getting the Latex code corresponding to some Mathematica input: see here. But what I want is sort of the opposite way, I want to type Latex in Mathematica by me typing the LaTeX code - I hate using Mathematica's equation typesetting system and prefer typing LaTeX code directly! If I adapt your code though, I get the useful information that 'e' is protected. $\endgroup$
    – Joshua T
    May 18, 2013 at 23:53

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