8
$\begingroup$

I am trying to write a function which takes Maple Latex generated, feed it to Mathematica, use ToExpression then convert it back to Latex.

This is because Mathematica Latex generated is a much better quality than Maple's.

For some latex, it works with no problem.

For some other I tried, it did not work. Mathematica gives lots of ErrorBox and Null in the translated Latex and I do not know why, since the Maple Latex is valid and can be compiled using tex with no problem.

Here is an example of a Latex string (I include this Latex in a plain text file with link at the bottom to make it easy to reproduce the problem)

\begin{align*}
p &=  \frac{  \partial G}{\partial x}+ \frac{  \partial G}{\partial \
p} \frac{  \mathop{\mathrm{d}p}}{\mathop{\mathrm{d}x}}\\ 
p -  \frac{  \partial G}{\partial x}&= \frac{  \partial G}{\partial \
p} \frac{  \mathop{\mathrm{d}p}}{\mathop{\mathrm{d}x}}\tag{3A}
\end{align*}

\begin{align*} p &= \frac{ \partial G}{\partial x}+ \frac{ \partial G}{\partial \ p} \frac{ \mathop{\mathrm{d}p}}{\mathop{\mathrm{d}x}}\\ p - \frac{ \partial G}{\partial x}&= \frac{ \partial G}{\partial \ p} \frac{ \mathop{\mathrm{d}p}}{\mathop{\mathrm{d}x}}\tag{3A} \end{align*}

Now when using the command

 TeXForm[ToExpression[str , TeXForm, HoldForm]] 

Mathematica gives this

$$ \left( \begin{array}{cc} \text{TI}(p) & \left\{\text{ErrorBox}[\text{ErrorBox}[=]],\text{Null},\frac{\partial G}{\partial x},\text{Null},\text{ErrorBox}[\text{ErrorBox}[+]],\text{Null},\frac{\partial G}{\partial p},\text{Null},\frac{d p}{d x}\right\} \\ \left\{p,\text{Null},\text{ErrorBox}[\text{ErrorBox}[-]],\text{Null},\frac{\partial G}{\partial x}\right\} & \left\{\text{ErrorBox}[\text{ErrorBox}[=]],\text{Null},\frac{\partial G}{\partial p},\text{Null},\frac{d p}{d x},\text{Null},3 A\right\} \\ \end{array} \right) $$

I will show the code below on how to reproduce the above.

Getting the Latex from Maple to Mathematica was a little tricky. Maple saves the latex to a plain text file called "input.txt" and Mathematica reads the Latex from this file.

I found that using ReadString["input.txt"] does not work, as Mathematica for some reason adds an extra new line between each line in the file and this break the latex.

So I am now using the following method instead:

SetDirectory[NotebookDirectory[]] (*where input.txt is located*)
str = "";
line = ReadLine["input.txt"];
If[line =!= EndOfFile,
  While[line =!= EndOfFile,
   str = str <> line;
   line = ReadLine["input.txt"];
   If[ line =!= EndOfFile, str = str <> "\n"]
   ]];

Now str above has the latex code as string. Next

 TeXForm[ToExpression[str , TeXForm, HoldForm]]

And that is where the problem shows up.

This is the file input.txt

Any idea why Mathematica does not produce good Latex for this and if there is a workaround? I am not going to use the Expression generated from Maple latex in any way, other than to convert it to Latex again, so I can use the Mathematica Latex instead of the Maple Latex.

The other thing I am not sure about is this: If the Latex input is valid, is Mathematica expected to always handle ToExpression[str , TeXForm, HoldForm] with no issue at all? Or it not necessarily so? Are there limitations on what the Latex can contain then?

$\endgroup$
1
  • 1
    $\begingroup$ The extra lines are likely to to be \r\n, in which case: string = ReadString[f]; If[string === EndOfFile, string = ""]; StringReplace[string, "\r\n" -> "\n"] $\endgroup$ Aug 2, 2019 at 10:55

1 Answer 1

2
$\begingroup$

In your example, Mathematica seems to parse the equations fine (aside from trying to multiply the lines together), but doesn't understand the \begin{align*}. I would guess that since it's called TeXForm, it can parse TeX primitives but not all LaTeX macros (see here for the difference), unless they are macros generated ass the output of TeXForm. Indeed, \def does work, but not any of the non-math LaTeX commands I tried.

s1 = "p &=  \\frac{  \\partial G}{\\partial x}+ \\frac{  \\partial G}{\\partial p} \\frac{  \\mathop{\\mathrm{d}p}}{\\mathop{\\mathrm{d}x}}\\\\ 
p -  \\frac{  \\partial G}{\\partial x}&= \\frac{  \\partial G}{\\partial p} \\frac{  \\mathop{\\mathrm{d}p}}{\\mathop{\\mathrm{d}x}}\\tag{3A}"

s2 = "\\begin{align*}
1+1
\\end{align*}"

s3 = "\\def \\surround [#1][#2]{#1 + #2 + #1}
\\surround[1][2]"

Echo[ToExpression[#, TeXForm, HoldForm]]& /@ {s1, s2, s3}

(*
>> HoldForm[(p == D[G, {x}] + D[G, {p}]*(d*p)/(d*x))*(p - D[G, {x}] == D[G, {p}]*(d*p)/(d*x)*3*A)]
>> HoldForm[ExpressionCell[TraditionalForm[{{TextCell[Row[{ , ExpressionCell[{1, Null, ErrorBox[ErrorBox[+]], Null, 1}, InlineFormula],  }]]}}]]]
>> HoldForm[1 + 2 + 1]
*)

Try it online!

The documentation also hints through omission that ToExpression with TeXForm is mainly useful in inverting ToString[#, TeXForm]& in the first place, as the only two examples dealing with ToExpression.

Use ToExpression to convert TeX back to Wolfram Language syntax: [...]

Use ToExpression to convert from TeX to the Wolfram Language: [...]

Even the ExportString example, which generates a full LaTeX document, cannot be inverted by ToExpression.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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