ToExpression to Latex Parenthesis

5

1

Everytime I use ToExpression to transform a TeX expression into mathematica input form, Mathematica always interprets parenthesis ('(' and ')') as square brackets, which has a differente meaning for expressions. I always have to manually change the expression. Also, it doesn't happen all the time. Does anybody know what is going on and what can I do to fix this? An Example:

ToExpression["A - \\frac{B (2+F)}{(1+C)} D", TeXForm]

This yields:

A - (D B[2 + F])/(1 + C)

I would like:

A - (D B(2 + F))/(1 + C)

Thanks.

user191919

Posted 2015-10-20T16:16:18.277

Reputation: 527

In practice it may be easier to just fix the resulting Mathematica expression rather than get Mathematica to use the desired interpretation ... i.e. do a replacement similar to s_[a_] :> s*a. Where do your TeX snippets come from? Can you get MathML instead of TeX? – Szabolcs – 2015-10-21T11:49:54.830

Answers

3

You can use a LaTeX thin space \, as a multiplication symbol to enforce what you want:

ToExpression["A - \\frac{B\\,(2+F)}{(1+C)} D", TeXForm]

(* A-(B D (2+F))/(1+C) *)

As noted by rhermans, standard mathematical notation is ambiguous -- do the parentheses signify multiplication or function arguments? -- so the thin space is needed to remove the ambiguity.

As noted by march, if you want to consistently use parentheses for multiplication, you could define a function like

fromTeX[str_] := ToExpression[StringReplace[str, "(" -> "\\,("], TeXForm]

Now you can do

fromTeX["A - \\frac{B(2+F)}{(1+C)} D"]

(* A - (B D (2 + F))/(1 + C) *)

Simon Rochester

Posted 2015-10-20T16:16:18.277

Reputation: 5 901

1Can you imagine a systematic way of telling Mathematica to do this? I am aware that I may be asking for too much. – user191919 – 2015-10-20T17:02:14.647

You need a way to differentiate the arguments of the function and the factorizing parenthesis. I guess that is why the space matters. – rhermans – 2015-10-20T17:12:35.890

@user191919. You could do StringReplace["A - \\frac{B (2+F)}{(1+C)} D", "(" -> "\\,("] before converting the expression. – march – 2015-10-20T22:48:17.500