8

2

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?

1The extra lines are likely to to be

`\r\n`

, in which case:`string = ReadString[f]; If[string === EndOfFile, string = ""]; StringReplace[string, "\r\n" -> "\n"]`

– Andrew Norton – 2019-08-02T10:55:33.477