Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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 Mar 10 '12 at 23:20
    
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 Mar 10 '12 at 23:20
    
What version of Mathematica are you using on what platform? –  Szabolcs Mar 11 '12 at 6:49
    
Mathematica 8.04 on Windows XP with a pentium 4 –  Diego S. Mar 11 '12 at 15:19
    
I get Sqrt[xy] too. Win7/mma 8.04 –  Sjoerd C. de Vries Mar 12 '12 at 0:17
add comment

1 Answer

Edit

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

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

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$.

Edit

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.

share|improve this answer
    
I still get the $failed mistake and i tried all you said. Thank you anyways :) –  Diego S. Mar 11 '12 at 0:02
    
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 Mar 11 '12 at 0:18
    
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 Mar 11 '12 at 4:04
2  
You'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 Mar 11 '12 at 4:14
    
@Jens I think \mathrm keeps the uniform math-spacing, see here. \text is fine though (with amsmath). –  Szabolcs Mar 11 '12 at 9:57
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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