3
$\begingroup$

I am trying to import a $\LaTeX$ equation into Mathematica. I called ToExpression[expression,TeXForm], which works for the most part, but sometimes it interprets products as functions, and I don't see how to prevent this.

For instance, I have the following equation:

c_{1,2} (b c_{1,2} - a s_{1,2}) t_{1,2}

which Mathematica interprets as $c_{1,2}[b\,c_{1,2} - a\,s_{1,2}]\,t_{1,2}$, where the square brackets mean that Mathematica sees $c_{1,2}$ as a function instead of a variable. Rewriting the expression as:

(b c_{1,2} - a s_{1,2}) c_{1,2} t_{1,2}

gives the result I want, but I have a lot of quite long equations already written, and it would be very tedious to check and shuffle them by hand.

Am I missing something obvious? How can I avoid this behavior? Thanks!

Edit: thanks for everyone's input! It wasn't simple to decide which answer to accept, in the end I accepted Domen's answer because it didn't require to change any of the latex. But I'm sure your suggestions will come in handy in the future :)

$\endgroup$

3 Answers 3

2
$\begingroup$

If you use Insert > Inline TeX Input, your expression gets parsed as a multiplication. So you may try using the same functionality, which is contained in InputAssistant`TeX... functions:

tex = "c_{1,2} (b c_{1,2} - a s_{1,2}) t_{1,2}";

ToExpression[tex, TeXForm]
(* Subscript[t, 1, 2] Subscript[c, 1, 2][b Subscript[c, 1, 2] - 
    a Subscript[s, 1, 2]] *)

ToExpression[InputAssistant`TeXStringToBoxes[tex]]
(* Subscript[c, 1, 2] (b Subscript[c, 1, 2] - 
    a Subscript[s, 1, 2]) Subscript[t, 1, 2] *)
$\endgroup$
3
$\begingroup$

Sematics of Latex is fussy. One option I see now is to change you latex and add explicit \times

expr = "c_{1,2}\\times(b c_{1,2} - a s_{1,2}) t_{1,2}"
ToExpression[expr, TeXForm]

Gives

enter image description here

And the actual Latex compiled is not too bad

enter image description here

If you do not want to do this, another option is to add a hidden character in Latex, like this

expr = "c_{1,2}\\phantom{}(b c_{1,2} - a s_{1,2}) t_{1,2}"
ToExpression[expr, TeXForm]

enter image description here

The advantage of this, is that you Latex remain the same as before:

enter image description here

This means when you write your Latex, just add \phantom{} any where it is meant to be multiplication. (If you plan to copy it to Mathematica later on).

This will not affect your Latex PDF output but will make Mathematica happy and will not take the input as function call.

$\endgroup$
1
$\begingroup$

Multiplication sign helps:

ToExpression["c_{1,2} *(b c_{1,2}-a s_{1,2}) t_{1,2}", TeXForm]

Subscript[c, 1, 2] (b Subscript[c, 1, 2] - a Subscript[s, 1, 2]) Subscript[t, 1, 2]

$\endgroup$

Your Answer

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

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