Message Boards Message Boards

0
|
2913 Views
|
14 Replies
|
1 Total Likes
View groups...
Share
Share this post:

ToExpress to TeXForm sometimes gives wrong results

Posted 1 year 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 1 year ago

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

POSTED BY: Zhenyu Zeng
Posted 1 year ago

You don't seem to have understood the answers provided already, so let's back up and walk through this slowly. In a Mathematica cell, type exactly this:

2+3

If you evaluate this cell, you'll get an output cell with a 5 in it. Now in another cell, type exactly this:

"2+3"

If you evaluate this cell, you'll get an output cell that displays as 2+3 but in fact the actual data is "2+3". You can see this if you use FullForm, so if you're curious, evaluate

FullForm["2+3"]

Now, the entire point of ToExpression is to take a string and sort of unbox it and interpret the result as an expression. So if you evaluate

ToExpression["2+3"]

The seqeunce of evaluation will be something like

"2+3" -> 2+3 -> 5

Now, we can get very meta about what's a string and what's not. I mean, you have to type in a sequence of characters to get 2+3, and isn't a sequence of characters a string? But don't get wrapped around the axle. Just think of the quote characters as special delimiters that say "the stuff inside should be interpreted as a literal sequence of characters with no meaning, and so it shouldn't be parsed at all".

Anyway, the entire point of ToExpression is to get something that's not a string in the sense I just explained. So, the result of ToExpression will be parsed as an expression just as if you had typed it without the quote delimiters.

Now, the TeXForm as the second argument is adding another layer of parsing. It's saying, "once you unbox the string, parse it as a TeX expression, and then map the result into Mathematica with the equivalent semantics. So, if we were to look at the output of your string in a TeX environment, it would look like H with a subscript 2 adjacent to an O. In that TeX environment, that's the final form, because TeX is just presenting formatted text, it's not evaluating it. The semantic mapping of that into Mathematica would be

`Subscript[H, 2] O`

But Mathematic doesn't stop there, because Mathematica isn't just presenting things, it's evaluating things. In Mathematica, adjacency is interpreted as multiplication (when it makes sense), and so this is parsed into

`Times[Subscript[H, 2], O]`

Mathematica has a canonical way to sort product terms, and so this becomes

Times[O, Subscript[H, 2]]
POSTED BY: Eric Rimbey
Posted 1 year 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

"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 1 year 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

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 1 year 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

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 1 year 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

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 1 year ago

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

POSTED BY: Zhenyu Zeng
Posted 1 year ago

Why it is tilted?

POSTED BY: Zhenyu Zeng

POSTED BY: Ahmed Elbanna

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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract