How to type a capital 'E' in Mathematica 9 using 'ToExpression' and 'TeXForm'?



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


  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?


Posted 2013-05-18T12:28:08.220

Reputation: 183

Question was closed 2013-05-20T02:07:41.777

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.

– Jens – 2013-05-19T20:27:21.643

In addition to the above link, I just remembered an extended version of the same answer that I posted later.

– Jens – 2013-05-19T20:40:17.370

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. – Jens – 2013-05-19T20:56:37.190



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.


Posted 2013-05-18T12:28:08.220

Reputation: 93 191

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. – Calleniah – 2013-05-20T11:46:17.067

1Thanks - I was actually wrong about requiring \text{} wrappers. It works without that, so it's a little simpler than I wrote at first. – Jens – 2013-05-21T05:31:36.787


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.


works as expected.


Posted 2013-05-18T12:28:08.220

Reputation: 907

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! – Calleniah – 2013-05-19T00:06:59.393

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. – ogerard – 2013-05-19T14:11:12.490


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)])"]


Posted 2013-05-18T12:28:08.220

Reputation: 666


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.

– Calleniah – 2013-05-18T23:53:46.630