3
$\begingroup$

I have a code in Python that makes some calculations for different reaction-diffusion equations. I am trying to let the user define the variables and parameters of the system.

The problem is that, for instance, I have tried needed to define variables with weird names like munormalform, because I don't want to use the variable mu to avoid problems when a user needs to define that variable as a parameter of the system. What happens then is that I need to get some of these variables into Mathematica so I am saving them into .txt files in a LaTeX form.

After this, I read the file in Mathematica with no problems but, when I have a variable defined as a = "D^{4} \\times munormalform" and I try to translate it into Mathematica input using b=ToExpression[a,TeXForm], I get the following output:

D^4 f l m^3 n o^2 r^2 u

$\endgroup$
0

1 Answer 1

6
$\begingroup$

As I mentioned in your deleted question (please do not delete this again), that latex has no concept of multi letter variables. Spaces are not important in this context. So the following Latex

\documentclass[12pt]{book}
\begin{document}

\[
   D^{4} \times x y z
\]

\[
   D^{4} \times xyz
\]
\end{document}

When compiled, generates the same thing

enter image description here

So Mathematica's TeXForm when it sees xyz in the Latex expression, treats these are x multiplied with y multiplied with z.

To solve this, use either \text{xyz} or \textit{xyz} in math mode in your latex. Depending if you want it to remain italic or not.

Default math font in computer modern is italic. So you will get this

\documentclass[12pt]{book}
\usepackage{amsmath}
\begin{document}
\[
   D^{4} \times \text{xyz}
\]

\[
   D^{4} \times \textit{xyz}
\]
\end{document}

enter image description here

And now Mathematica TeXForm will also treat it as single variable.

expr = "D^{4} \\times \\textit{munormalform}";
ToExpression[expr, TeXForm]

Mathematica graphics

compared to

Mathematica graphics

So to fix this problem, you need to make sure your source Latex is formatted correctly by adding \text{...} or \textit{....} around those multi-letter variables meant to be single variable, as Mathematica is just following the rules of Latex when translating it. The Python/sympy latex translator should have automatcially added \text{...} around that variable. I am surprised it did not.

Notice how Mathematica's TeXForm adds \text{} automatically:

ClearAll[d, munormalform];
expr = d^4 * munormalform;
TeXForm[expr]

Mathematica graphics

For additional information you can see

multiple-letters-without-spacing-in-math

typesetting-multi-letter-variable-names-in-math-mode

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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