4
$\begingroup$

I looked at similar questions, but could not find an answer.

I need to convert Latex that represent pure mathematical expressions generated by Maple to Mathematica. I do not want to actually use these expressions in Mathematica. But just to convert them to Mathematica syntax, then save them back as plain strings for other purposes.

Some of the Maple expressions (in Latex) do not map to same Mathematical functions as defined by Mathematica.

But that is OK, I do not need to use them any way in Mathematica. But the problem is when I use r=ToExpression[s, TeXForm], where s is the Latex string generated by Maple, depending on s, Mathematica can complain if the generated expression is not understood by Mathematica.

Even though I just want to do ToString[InpuForm[r]] right after that, and save r as string for other purposes.

But to go from Latex to string, I have to go through ToExpression first.

I do not know how to use Hold or Inactive or such to do this.

Is there a way to do this conversion without this error being generated? Here is an example

s="-{{\\rm e}^{2}}{\\it Ei} \\left( 1,-2t+2 \\right)";(*generated by Maple*)
r=ToExpression[s,TeXForm];

Mathematica graphics

ToString[InputForm[r]]

Mathematica graphics

The above is all what I need. Even if it is not valid Mathematica command, it is ok.

Most of the Maple latex output is converted OK, since these are just pure mathematical expressions (do not contain actual Maple commands or such), so about 90% of them cause no problem (they are all antiderivatives results from integration test).

Version 11.1

ps., I looked at converting-expression-into-string-without-evaluating-it but could not make it work for the above example. I still get the error message.

ps. Currently I do the following, as temporary (may be permanent?) solution

s="-{{\\rm e}^{2}}{\\it Ei} \\left( 1,-2t+2 \\right)";
Quiet[r=ToExpression[s,TeXForm]]; (*suppress error message*)
ToString[InputForm[r]]

Mathematica graphics

$\endgroup$
  • 2
    $\begingroup$ Try MakeExpression or ToHeldExpression. Latter is deprecated. $\endgroup$ – b3m2a1 Mar 18 '17 at 0:51
  • $\begingroup$ @MB1965 thanks. Actually I ended up just ignoring the message using Quiet for now. Since as I said, I am not going to use the result. I tried your method, but I must did something wrong, and could not understand how to use it. I can close this question if it is not useful for others. $\endgroup$ – Nasser Mar 18 '17 at 2:29
  • $\begingroup$ they'll interpret like ToExpression but wrap it in a Hold so it doesn't evaluate. Then you'd do like: Replace[MakeExpression[s_],HoldComplete[r_]:>ToString[Unevaluated[r],InputForm. Up to you over whether you think it's worth leaving open or not, I think. $\endgroup$ – b3m2a1 Mar 18 '17 at 2:41
  • 1
    $\begingroup$ @MB1965 MakeExpression and ToHeldExpression do not accept TeXForm as the second argument. $\endgroup$ – Alexey Popkov Mar 18 '17 at 2:56
4
$\begingroup$

Here are several methods (the first two are preferred) which completely avoid evaluation of the intermediate expression:

ToString[#, InputForm] &@ToExpression[s, TeXForm, Unevaluated]
ToExpression[s, TeXForm, HoldComplete] /. 
    HoldComplete[x__] :> ToString[Unevaluated[x], InputForm]
ToExpression[s, TeXForm, HoldComplete] /. 
    HoldComplete[x__] :> ToString[Unevaluated@InputForm[x]]
ToExpression[s, TeXForm, HoldComplete] /. 
    HoldComplete[x__] :> ToString[HoldForm@InputForm[x]]    
"(-e^2)*ExpIntegralEi[1, -2*t + 2]"

"(-e^2)*ExpIntegralEi[1, -2*t + 2]"

"(-e^2)*ExpIntegralEi[1, -2*t + 2]"

"(-e^2)*ExpIntegralEi[1, -2*t + 2]"

The above solutions will work correctly even for unlikely (but theoretically possible) expressions like the following:

ss = {"\\text{Sequence}\\left[x^2,x^3\\right]", "\\text{Evaluate}\\left[x^2,x^3\\right]" };
ToString[#, InputForm] &@ToExpression[# , TeXForm, Unevaluated] & /@ ss
{"Sequence[x^2, x^3]", "Evaluate[x^2, x^3]"}
$\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.