# How to import $\LaTeX$ matrices into Mathematica?

I have a lot of $$\LaTeX$$ matrices, and I want to find their eigenvalues and eigenvectors.

I tried to import them to Mathematica with:

ToExpression["[MATRIX PASTED FROM LATEX]",TeXForm]


This method worked for everything I tried until now, as long as it wasn't a matrix. For some reason, Mathematica thinks of the matrix as some kind of "ExpressionCell" That I know nothing of.

Here is an example of this problem:

Input:

ToExpression["\\begin{matrix}{0} & {0} & {0}\\\\
{\\sqrt{2}} & {0} & {0}\\\\
{0} & {\\sqrt{2}} & {0}
\\end{matrix}", TeXForm]


Output: (I converted it to InputForm so you can see the problem)

ExpressionCell[TraditionalForm[{{TextCell[Row[{ , TextCell[0, InlineFormula],  }]],
TextCell[Row[{ , TextCell[0, InlineFormula],  }]],
TextCell[Row[{ , TextCell[0, InlineFormula],  }]]},
{TextCell[Row[{ , ExpressionCell[2, InlineFormula],  }]],
TextCell[Row[{ , TextCell[0, InlineFormula],  }]],
TextCell[Row[{ , TextCell[0, InlineFormula],  }]]},
{TextCell[Row[{ , TextCell[0, InlineFormula],  }]],
TextCell[Row[{ , ExpressionCell[2, InlineFormula],  }]],
TextCell[Row[{ , TextCell[0, InlineFormula],  }]]}}]]


When I ran the code the matrix looks fine - like a Matrix. But Mathematica just doesn't think of it as a matrix, and therefor can't find it's eigenvalues (or do any kind of calculations).

I'm stuck here. Does anyone how to input matrices into Mathemtica?

Any help would be appreciated!

P.S. I found that I'm not the only one stuck with this problem:

Another poor user from Quora

• My WinEdt fails with compiling your \\ begin{matrix}{0} & {0} & {0}\\ \\ {\\ sqrt{2}} & {0} & {0}\\ \\ {0} & {\\ sqrt{2}} & {0} \\ end{matrix}. Is it OK? Jun 29, 2019 at 19:41

I'm assuming the OP wants "\\sqrt{2}" to turn into the equivalent of $\sqrt{2}$ or $$\sqrt{2}$$, which would Sqrt[2] in Mathematica. The command ToExpression[s, TeXForm] does not seem to handle the square roots, when they are embedded in a matrix, as in the OP's string. The matrix format of TeX is so straightforward that one can split it up into a list of lists almost on the fly:

stripLaTeXCommand[cmd_] := StringDelete[cmd ~~ "{" ~~ Except["}"] ... ~~ "}"];
split[pat_] := StringSplit[#, pat] &;
texToExp = Function[s, ToExpression[s, TeXForm], Listable];

"\\begin{matrix}{0} & {0} & {0}\\\\
{\\sqrt{2}} & {0} & {0}\\\\
{0} & {\\sqrt{2}} & {0}
\\end{matrix}"   //
stripLaTeXCommand["\\begin" | "\\end"] //
StringTrim    //
split["\\\\"] //
split["&"]    //
texToExp
(*  {{0, 0, 0}, {Sqrt[2], 0, 0}, {0, Sqrt[2], 0}}  *)


Remarks: (1) I'd say mishandling the square roots in a matrix is a bug. (2) Caveat: ToExpression["\\begin{matrix}{0}", TeXForm] and texToExp["\\begin{matrix}{0}"] would sometimes return \$Failed (good) and sometimes seemingly get stuck in an infinite loop (bad). I had to kill the kernel (but not the front end) to get back control of Mathematica.

• As far as I can see, this works for all of my matrices. Thanks! Jun 30, 2019 at 20:50
• @DeadlosZ You're welcome! Jun 30, 2019 at 20:55

From your InputForm example, we can see that each row has the same structure. Namely each row has the form

TextCell[Row[List[" ", TextCell[element_, "InlineFormula"], " "]]]


or

TextCell[Row[List[" ", ExpressionCell[element_, "InlineFormula"], " "]]]


where element is the number in that entry of the matrix.

Then we can take advantage of this and simply replace each outermost TextCell with the non-string element itself with

TextCell[Row[List[" ", (TextCell | ExpressionCell)[element_, "InlineFormula"], " "]]] :> ToExpression@element


Then we still have the pesky ExpressionCell[TraditionalForm[...]] to take care of with a simple part specification.

In the case of your example, this turns out to be

mat = ToExpression["\\begin{matrix}{0} & {0} & {0}\\\\
{\\sqrt{2}} & {0} & {0}\\\\
{0} & {\\sqrt{2}} & {0}
\\end{matrix}", TeXForm]

mat /. TextCell[Row[List[" ", (TextCell | ExpressionCell)[element_, "InlineFormula"], " "]]] :> ToExpression@element
%[[1, 1]]
FullForm@%


{{0,0,0},{2,0,0},{0,2,0}}

List[List[0,0,0],List[2,0,0],List[0,2,0]]

And the beauty of this approach is that it readily generalizes (as far as I can tell). For example

ToExpression["\\begin{matrix}{0} & {0} & {0} & {2} \\\\
{\\sqrt{2}} & {0} & {0} & {0} \\\\
{0} & {\\sqrt{2}} & {0} & {7}
\\end{matrix}", TeXForm]

% /. TextCell[Row[List[" ", (TextCell | ExpressionCell)[element_, "InlineFormula"], " "]]] :> ToExpression@element

%[[1, 1]]//FullForm


List[List[0,0,0,2],List[2,0,0,0],List[0,2,0,7]]

• What happened to the "\\sqrt"? Jun 30, 2019 at 14:31
• This solution looks perfect, but it does looks like the sqrt disappeared. Jun 30, 2019 at 20:45

Here's my solution:

SetAttributes[toexpr, Listable];
toexpr[str_] := ToExpression[str, TeXForm]

importlatexmat[str_] :=
toexpr@ImportString[str, "Table", "FieldSeparators" -> {"\\\\", "&"},
"Numeric" -> False]

importlatexmat@"{0} & {0} & {0}\\\\
{\\sqrt{2}} & {0} & {0}\\\\
{0} & {\\sqrt{2}} & {0}"