1
$\begingroup$

I've got a String variable defined as b="4.0 \cdot 10^{-5}", which is clearly written as LaTeX input.

The problem is that when I want to obtain the expression from it using c=ToExpression[b,TeXForm], I get the following output:

4.\[CenterDot]1/100000.

To avoid that problem, I tried first doing b=StringReplace[b," \cdot " -> "*"] but, if I do that, then the output of the other line of code gets senseless:

9.76563*10^(-9)

Is there another way to deal with this problem with \cdot?

$\endgroup$
3
  • $\begingroup$ It's a matter of precedence. TeXForm is interpreting the input as (4.0*10)^{-5}. $\endgroup$
    – Carl Woll
    Sep 21 at 20:31
  • $\begingroup$ Is there another way to deal with this problem with \cdot? it is better to go back to the source to fix the problem there. What sympy expression did you have that generated \cdot as its Latex translation? $\endgroup$
    – Nasser
    Sep 21 at 20:49
  • $\begingroup$ The number was 4e-5 $\endgroup$ Sep 21 at 20:51
3
$\begingroup$

You should not really use * in Latex. No one does. This is simply bad latex. How did you generate "4.0 * 10^{-5}" as latex to start with?

Try b = "$4.0 \\times 10^{-5}$" which when compiled in Latex, gives $$ 4.0 \times 10^{-5} $$

Then now b = ToExpression[b, TeXForm] gives 0.00004 But if you want to keep the form without evaluation, then you can use

b = ToExpression[b, TeXForm, HoldForm]

Mathematica graphics

$\endgroup$
6
  • $\begingroup$ Oh you are right! I already made some changes actually. I will give all the information about how did I get that variable actually. Maybe there is another way to solve the problem in a straightforward way. $\endgroup$ Sep 21 at 20:37
  • $\begingroup$ I edited the question. Do you have any suggestion? \times is not working for me. If I replace \cdot by \times, the \t gets understood as a tabulation by Mathematica. $\endgroup$ Sep 21 at 20:49
  • 1
    $\begingroup$ @edgardeitor Use \\times instead of \times. $\endgroup$
    – Carl Woll
    Sep 21 at 20:54
  • 1
    $\begingroup$ Yes it should be \\times. bad typing on my part. Any latex command in string should be escaped. $\endgroup$
    – Nasser
    Sep 21 at 20:55
  • $\begingroup$ That works perfect! $\endgroup$ Sep 21 at 20:55
2
$\begingroup$

It's a bug, but ToExpression["4.0 * 10^{-5}"] works fine. So does ToExpression["4.0 \, 10^{-5}", TeXForm]. Here's what's happening:

Trace@ToExpression["4.0 * 10^{-5}", TeXForm]
(* {ToExpression[4.0 * 10^{-5},TeXForm],1/(4. 10)^5,{4. 10,40.},1/40.^5,9.76563*10^-9} *)
$\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.