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.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityEdit
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:
\\, d
. Only with the additional space will it recognize the d
as the differential part of the integral.\\partial
symbol ($\partial$): ToExpression["\\frac{\\partial^2 f(x)}{\\partial x^2}", TeXForm, Hold]
Hold
prevents evaluation so you can copy the expression from the output cell if desired (you can later apply ReleaseHold
to evaluate the expression)\\{x,y,f (z)\\}
. The conversion of array environments or commands such as \pmatrix
to Mathematica produces wrong formatting.\\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.
$failed
mistake and i tried all you said. Thank you anyways :)
$\endgroup$
$\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]
$\endgroup$
Syntax::stresc: Unknown string escape \s.
. $\endgroup$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"
). $\endgroup$