Unable to convert $\TeX$ input into mathematica



The input

ToExpression["\\sqrt{x y}", TeXForm]

gives me the $Failed output.

Maybe my very old computer is the problem, but that's the only command that has caused me problems, and that input is from the tutorials.


Posted 2012-03-10T22:46:24.873

Reputation: 697

Works for me on version 8.0.4, Mac OS X. But make sure you do have doubled backslashes - cutting one out gives the error Syntax::stresc: Unknown string escape \s.. – Verbeia – 2012-03-10T23:20:22.500

I get Sqrt[xy] — not quite correct, but better than just $Failed. However when entered directly to the kernel, for some strange reason it complains if there's no X display ... (Can't open display ":0.0"). – celtschk – 2012-03-10T23:20:38.973

What version of Mathematica are you using on what platform? – Szabolcs – 2012-03-11T06:49:18.220

Mathematica 8.04 on Windows XP with a pentium 4 – Zero – 2012-03-11T15:19:14.123

I get Sqrt[xy] too. Win7/mma 8.04 – Sjoerd C. de Vries – 2012-03-12T00:17:45.920




An updated version of this answer is here. I forgot that there was some duplication when I posted the linked answer, but the material in that answer is more recent so I'll update that one and leave this post unchanged.

End edit

The conversion from $\LaTeX$ in general is rather quirky, but I don't see a failure of the kind you see.

However, there is in fact something wrong: The $\LaTeX$ syntax you're entering is completely valid and it should be interpreted as the square root of the product of two variables, x and y. However, Mathematica (my version is 8.0.4) incorrectly outputs a single variable xy instead of a product.

So there is really a bug in how products are interpreted. The work-around is to put thin spaces in your $\LaTeX$ code explicitly, as in


Now the question still is why your conversion failed. My only explanation for that would be that before executing the ToExpression, you might have had a global variable by the name xy defined in Mathematica, for which it turned out to be impossible to take the square root. You could test this by first typing Clear[xy,x,y] and then re-trying the conversion from $\LaTeX$.


To expand a little on the quirks of $\LaTeX$-to-Mathematica conversion, here are some more points to watch out for:

  • To translate integrals properly, Mathematica expects the integration variable in the $\LaTeX$ code to be preceded by \\, d. Only with the additional space will it recognize the d as the differential part of the integral.
  • Express derivatives using the \\partial symbol ($\partial$): ToExpression["\\frac{\\partial^2 f(x)}{\\partial x^2}", TeXForm, Hold]
  • In the above code, the argument Hold prevents evaluation so you can copy the expression from the output cell if desired (you can later apply ReleaseHold to evaluate the expression)
  • All matrices and vectors must be written in list form using escaped curly brackets, as in \\{x,y,f (z)\\}. The conversion of array environments or commands such as \pmatrix to Mathematica produces wrong formatting.
  • To group expressions, use only round parentheses, i.e., \\left( and \\right), not square [...] or curly {...} brackets. The latter are interpreted differently by Mathematica.

I copied this list from this my web page where I also collected some notes on the reverse process of getting equations out of Mathematica.

Edit 2

A comment by RM below brings to mind another point that is related more to $\LaTeX$: some novice authors write lazy expressions like $volume = length \times area$ and wonder why the typeset equation has unnatural-looking character spacing inside the "words" used here as variables. The reason is that $\TeX$ considers each single character in a sequence such as volume to be a single variable and applies the corresponding horizontal spacing rules to them.

Not knowing this feature, you may think that Mathematica has every right to interpret x y and likewise xy as the name of a single variable "xy". However, that goes against the rules of the $\TeX$ language and is therefore an incorrect translation.


Posted 2012-03-10T22:46:24.873

Reputation: 93 191

I still get the $failed mistake and i tried all you said. Thank you anyways :) – Zero – 2012-03-11T00:02:52.087

Sorry - then it may really have something to do with your old computer... perhaps Mathematica needs to invoke some functionality in the FrontEnd for the conversion and isn't able to do so. – Jens – 2012-03-11T00:18:32.443

I disagree with parts of your answer related to a "bug in how products are interpreted". In latex, spaces are ignored in math mode, so even if you write $\sqrt{x y}$ and $\sqrt{xy}$ and intend for them to mean different things (as how Mathematica interprets spaces), they mean the same in latex. So what Mathematica returns (i.e. Sqrt[xy]) is indeed correct. If, instead, you wanted the output to be the product of two separate variables, you'll have to make that explicit in your latex expression as either "\\sqrt{x*y}" or "\\sqrt{x\\times y}". This will give you Sqrt[x y] – rm -rf – 2012-03-11T04:04:16.593

2You're confusing me. First off: LaTeX doesn't care about the spaces between x and y, that's true. But I actually didn't say that! In contrast to what you may believe, LaTeX interprets the individual letters as single variables! So I'm right in saying that it's a bug. In LaTeX, to get a variable to have more than one character you would usually put it into a \text{} ot a \mathrm{} or a variety of other constructs. – Jens – 2012-03-11T04:14:08.297

@Jens I think \mathrm keeps the uniform math-spacing, see here. \text is fine though (with amsmath).

– Szabolcs – 2012-03-11T09:57:55.957

@Szabolcs: See how confused I was? You're right, of course. I use \text all the time, for subscripts, too. \mathrm could still be OK because at least you don't see the extra italic spacing so much. – Jens – 2012-03-11T16:39:52.437