# Import Tex's aligned multiline equation into Mathematica

I know there have been answers to similar questions, using either ImportString or ToExpression, however I haven't been able to succeed in importing this.

I'm using version 10 Mathematica on Linux by the way. What I want to import is a multi-line aligned equation. In this link, Workaround for Import not supporting the \leqslant TeX macro, I see some more advanced work but as a Mathematica novice I am unable to extend it to deal with my problem.

Here is the attached, long equation I'm trying to import.

Thanks for any help at all!

P.S. I really only care about the right-hand side.

P.P.S. I know it is ridiculous and if I see how to do, say, three or four lines, then I imagine it is the same for the rest. One of the problems I had is that the author used things like having a space between the symbol and the underscore for subscripts...

\begin{aligned} V&{}_{2,3}(\alpha_1,\beta_1,\alpha_2,\beta_2;Q)=\\ &24 (Q-4) \alpha _1^2 \alpha _2^3 \beta _2^9 \beta _1^9-6 Q \left(4 Q^2-23 Q+25\right) \alpha _1 \alpha _2^3 \beta _2^9 \beta _1^9-2 \left(61 Q^3-447 Q^2+975 Q-625\right) \alpha _1^2 \alpha _2^2 \beta _2^9 \beta _1^9\\ &+36 Q^5 \alpha _1 \alpha _2^2 \beta _2^9 \beta _1^9+18 (Q-1) Q^4 \alpha _1 \alpha _2^2 \beta _2^8 \beta _1^9-90 (Q-1) Q \alpha _1 \alpha _2^3 \beta _2^7 \beta _1^9+36 Q^3 \left(Q^2-6 Q+5\right) \alpha _1 \alpha _2^2 \beta _2^7 \beta _1^9\\ &+2 Q^3 \left(38 Q^2-253 Q+305\right) \beta _2+\left(-16 Q^3+315 Q^2-888 Q+625\right) \alpha _2 \beta _2-36 \alpha _1 \alpha _2 \beta _2 \ . \end{aligned}

• Please reduce your example to a minimal one. Maybe single term from this huge LaTeX expression is enough to illustrate the problem? – jkuczm Jan 13 '16 at 1:49
• Sure, I thought it was clear that I suggested that in my "P.P.S.", but I'll edit the equation. – nate Jan 13 '16 at 22:15

If you're only interested in extracting expression from $\TeX$ code, and you don't care about formatting, then easiest thing to do is to remove unnecessary formatting from string with $\TeX$ code. I can see four formatting sub-string types to delete: opening and closing of align environment, newlines, and column separators. There's also full stop at the and. All occurrences of these strings can be matched using pattern: "\\begin{aligned}" | "\\end{aligned}" | "\\\\" | "&" | "\\ ."

Second problem is that things like Q \left(...\right) are interpreted as function calls, and I guess you want them to be interpreted as multiplication. They will be interpreted as multiplication if we add, for example, a "control space" (\ ) between letter and opening bracket.

There's also a semicolon ; on the left hand side that Mathematica interprets as CompoundExpression. If we want it to be interpreted as separator between function arguments we can replace it with a comma ,.

This preprocessing can be done using StringReplace function.

Full code looks like this:

texString = "\\begin{aligned}
V&{}_{2,3}(\\alpha_1,\\beta_1,\\alpha_2,\\beta_2;Q)=\\\\
&24 (Q-4) \\alpha _1^2 \\alpha _2^3 \\beta _2^9 \\beta _1^9-6 Q \
\\left(4 Q^2-23 Q+25\\right) \\alpha _1 \\alpha _2^3 \\beta _2^9 \
\\beta _1^9-2 \\left(61 Q^3-447 Q^2+975 Q-625\\right) \\alpha _1^2 \
\\alpha _2^2 \\beta _2^9 \\beta _1^9\\\\
&+36 Q^5
\\alpha _1 \\alpha _2^2 \\beta _2^9 \\beta _1^9+18 (Q-1) Q^4 \
\\alpha _1 \\alpha _2^2 \\beta _2^8 \\beta _1^9-90 (Q-1) Q \\alpha _1 \
\\alpha _2^3 \\beta _2^7 \\beta _1^9+36 Q^3 \\left(Q^2-6 Q+5\\right) \
\\alpha _1 \\alpha _2^2 \\beta _2^7
\\beta _1^9\\\\
&+2 Q^3 \\left(38 Q^2-253 Q+305\\right) \\beta _2+\\left(-16 \
Q^3+315 Q^2-888 Q+625\\right) \\alpha _2 \\beta _2-36 \\alpha _1 \
\\alpha _2 \\beta _2 \\ .
\\end{aligned}";

texStringPreprocessed =
StringReplace[texString, {
"\\begin{aligned}" | "\\end{aligned}" | "\\\\" | "&" | "\\ ." -> "",
";" -> ",",
"\\left(" -> "\\ \\left("
}];

ToExpression[texStringPreprocessed, TeXForm, HoldForm] • Wow, that's exactly what I was looking for. To know what strings are interpreted by Mathematica differently, etc. Clearly explained! – nate Jan 14 '16 at 0:32