# How to type a capital 'E' in Mathematica 9 using 'ToExpression' and 'TeXForm'? [duplicate]

This question already has an answer here:

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:

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

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


Irritatingly, 'E' is converted to the bold-faced natural exponential and the order of the variables is changed in the output:

Does anyone know how to fix these two problems? Thanks!

-

## migrated from stackoverflow.comMay 19 '13 at 19:05

This question came from our site for professional and enthusiast programmers.

## marked as duplicate by Jens, Artes, Szabolcs, m_goldberg, rm -rf♦May 20 '13 at 2:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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 May 19 '13 at 20:27
In addition to the above link, I just remembered an extended version of the same answer that I posted later. –  Jens May 19 '13 at 20:40
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 May 19 '13 at 20:56

## 3 Answers

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.

-
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 May 20 '13 at 11:46
Thanks - I was actually wrong about requiring \text{} wrappers. It works without that, so it's a little simpler than I wrote at first. –  Jens May 21 '13 at 5:31

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.

-
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 May 19 '13 at 0:06
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 May 19 '13 at 14:11

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

-
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 May 18 '13 at 23:53