5
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ 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? $\endgroup$
    – Szabolcs
    Oct 21, 2015 at 11:49

1 Answer 1

3
$\begingroup$

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) *)
$\endgroup$
3
  • 1
    $\begingroup$ Can you imagine a systematic way of telling Mathematica to do this? I am aware that I may be asking for too much. $\endgroup$
    – user191919
    Oct 20, 2015 at 17:02
  • $\begingroup$ You need a way to differentiate the arguments of the function and the factorizing parenthesis. I guess that is why the space matters. $\endgroup$
    – rhermans
    Oct 20, 2015 at 17:12
  • $\begingroup$ @user191919. You could do StringReplace["A - \\frac{B (2+F)}{(1+C)} D", "(" -> "\\,("] before converting the expression. $\endgroup$
    – march
    Oct 20, 2015 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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