Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.4K Views
|
14 Replies
|
1 Total Like
View groups...
Share
Share this post:

ToExpress to TeXForm sometimes gives wrong results

Posted 2 years ago

Hello,

Use this to output TeX form,

ToExpression["\\text{H}_2\\text{O}", TeXForm]

But it outputs:
enter image description here

Is it a bug or did I write wrong code?

POSTED BY: Zhenyu Zeng
14 Replies
Posted 2 years ago
POSTED BY: Eric Rimbey
Posted 2 years ago

Thanks a lot. You patient and detailed answer makes clear of that question. Learnt a lot.

POSTED BY: Zhenyu Zeng

The function ToExpression takes a string and tries to output a valid Wolfram Language expression. Unfortunately, the system is not smart enough to guess that your string is meant to be a chemical formula. It assumes that it is a mathematical formula, which includes multiplication, and then the reordering of factors.

POSTED BY: Gianluca Gorni
Posted 2 years ago

But ToString[ ,TeXForm] should convert strings to TeXForm, so it shouldn't consider whether it is related to Mathe or Chemical. The fact that it consider the strings to something is weird.

POSTED BY: Zhenyu Zeng

Try with dimethyl ether:

ToString[C Subscript[H, 3] O C Subscript[H, 3], TeXForm]

The expression is treated as a mathematical formula, not as a chemical formula.

POSTED BY: Gianluca Gorni
Posted 2 years ago

Sorry. I made a mistake.

But ToString[ ,TeXForm] should convert strings to TeXForm, so it shouldn't consider whether it is related to Mathe or Chemical. The fact that it consider the strings to something is weird.

should be

But ToExpression[ " " ,TeXForm] should convert strings to TeXForm, so it shouldn't consider whether it is related to Mathe or Chemical. The fact that it consider the strings to something is weird.

As there are two " inside the ToExpression[ " " ,TeXForm], the strings should not be treated as mathe or chemical, but just strings.

POSTED BY: Zhenyu Zeng

Both Mathematica and TeX were born to handle mathematical formulas as their first mission. When you write "\\text{H}_2\\text{O}" you are using a math construct to represent a chemical formula. Instead, you should use either \usepackage{mhchem} or \usepackage{chemformula}.

ToExpression["string",TeXForm] was designed to read the string as a math formula written in TeX. It was not designed to guess whether you actually meant to fake a chemical formula as a math formula.

TeX does not reorder the factors of a product because it is visually oriented. Mathematica rearranges the factors because it is meant to make calculations.

POSTED BY: Gianluca Gorni
Posted 2 years ago

But, why "5 6" will output "5 6", not 30 in Mathematica. So, sometimes Mathematica wouldn't calculate anything in " ", sometimes it will. I can't tell when it will or won't.

POSTED BY: Zhenyu Zeng

"5 6" is a string, not an expression. If you convert it to an expression with ToExpression, it will become 30:

In[15]:= ToExpression["5 6"]

Out[15]= 30

Mathematica does not calculate anything inside a string.

In your original question, "\\text{H}_2\\text{O}" is a string. but the output is not a string:

In[16]:= ToExpression["\\text{H}_2\\text{O}", TeXForm] // StringQ

Out[16]= False

To my great fury, some years ago the standard output view stopped showing the "" quotes, and this leads to amiguity and misunderstanding.

POSTED BY: Gianluca Gorni
Posted 2 years ago

Okay. Thanks for your detailed explaination. But, it still doesn't have " " in the output. So, what did you mean of

some years ago the standard output view stopped showing the "" quotes,

Thanks.

POSTED BY: Zhenyu Zeng

POSTED BY: Ahmed Elbanna
Posted 2 years ago
POSTED BY: Zhenyu Zeng

This is fully expected behaviour. You are writing the expression H_2 followed immediately by O. In Wolfram Language this is interpreted as a multiplication, and the factors of a product are arranged in standard order

ToExpression["\\text{H}_2\\text{O}", TeXForm] // InputForm
O*Subscript[H, 2]

and it just happens that "O" comes before Subscript[H, 2].

Try this

Row[{Subscript["H", 2], "O"}]
POSTED BY: Gianluca Gorni
Posted 2 years ago

But " represent the string, it should not apply mutiplication here. Why it mutiplicate here?

POSTED BY: Zhenyu Zeng
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard