From: Jean-Marc Gulliet on
Murray Eisenberg wrote:
> I tried something similar, but using ToExpression[...,"TeXForm"],
> likewise removing the trailing "\\" from the original.
>
> (I was surprised that Mathematica just ignored the "&", which is
> evidently in the original expression because that expression is part of
> a larger LaTeX construct such as a \begin{align}...\end{align).
>
> However, I was unable to see how to let Mathematica itself convert the
> single backslashes, which of course are interpreted as escape symbols,
> to the needed double-backslashes. The obvious thing,
>
> expr = "\ln{H}^{*} &= {X}_{1} {\beta}_{1} + {\alpha}_{1}lnE+ \
> {\varepsilon}_{1}";
>
> expr/. "\"->"\\"
>
> does not work, of course.

<snip>

However, *StringReplace[expr, "\\" -> "\\\\"]* works.

.. Note, that expr needs some additional editing before being converted
because "\b" is an ASCII escape sequence for "Bell" and it is converted
-- more or less -- to its numeric value. Therefore "\beta" becomes
"\.08eta" :)

.. Also, depending on your settings, Mathematica can automatically
convert the escape character "\" into "\\" when pasting a string.

In[2]:= expr = "\ln{H}^{*} &= {X}_{1} {\beta}_{1} + \
{\alpha}_{1}lnE+ {\varepsilon}_{1}"

Out[2]= "\\ln{H}^{*} &= {X}_{1} {\.08eta}_{1} + {\\alpha}_{1}lnE+ {\
\\varepsilon}_{1}"

.. Finally, the string must start and end with a dollar sign "$" that
denotes math mode in LaTeX.

StringReplace[
"$" <> "ln{H}^{*} &= {X}_{1} {\\beta}_{1} + {\alpha}_{1}lnE+ \
{\varepsilon}_{1}\\" <> "$", "\\" -> "\\\\"]

ToExpression[%, TeXForm]

(Note that TeXForm is entered without double quotes.)

Regards,
-- Jean-Marc

From: Jean-Marc Gulliet on
snowrain wrote:

> Thanks for your reply !
> I tried "Import" to import the Latex file but it shows error info as below:
> Import::texerr: Error: Math character '\alpha ' in non-math mode.
>
> Can you please let me know what is the problem?
> thanks a lot !

The following message strongly suggests that your input file is not
correctly written in LaTeX. Apparently you have some regular (plain)
text that contains special math character without being enclosed within
dollar sign "$". For instance,

.. "The parameter \alpha is of crucial importance."

will generate an error, while

.. "The parameter $\alpha$ is of crucial importance."

will be parse correctly.

Having said that, I would strongly suggest that you latexify (i.e.
compile with whatever installation of LaTeX you have) your source file
to be sure to check and correct any error and also warning messages.

Note that it is very likely that LaTeX, when processing the first
sentence will generate a warning about math mode missing, yet add the
missing math mode characters in the intermediate file that is producing,
leaving the original source file untouched.

Therefore, you must fix these warnings/errors by yourself within the
source file you are going to give to Mathematica.

Regards,
-- Jean-Marc