LyXLyx, math, and mhchem

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
Post Reply
marvelty
Posts: 6
Joined: Tue Apr 14, 2009 8:14 pm

Lyx, math, and mhchem

Post by marvelty »

Hi, I'm a bit new to lyx, and am trying to use it to write a chem. paper. I have been using mhchem to make my formulae and equations, but I am having some problems that I would appreciate any help with:

Problem 1:
In order to enter formulae, spaces are required between each term, ie:

Code: Select all

\ce{2H2 + O2 -> 2H2O}
However, once math mode is entered, using spacebar boots the cursor out past the closing brace, instead of entering a space. I have been working around this by typing out the code elsewhere and pasting it into math, which mostly works. Is there a better way?

Problem 2:
When writing the formula for a multiply charged ion (eg oxide O^2-), I cannot get it to display properly. The code for this according to the documentation would be:

Code: Select all

\ce{O^2-}   OR   \ce{O^{2-}}

The first option results in the 2 being superscript, but not the "-", the second causes lyx to interpret it as some sort of math formula, changing the font to the default math font (instead of matching the body text) and adding an \ensuremath{} before the ion that is causing errors when I try to preview as a dvi. Adding an extra \cf{} around the ion solves the font problem, but not the \ensuremath{} addition. Any explanations or solutions for this?

I am using Lyx 1.6.1 and mhchem 3.07 on eeebuntu (based on ubunto 8.10).

Thanks in advance for your help!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Lyx, math, and mhchem

Post by josephwright »

Can't help with the lyx, but you should get the correct result with

Code: Select all

\ce{O^{2-}}
if you use LaTeX directly. So you are at least doing the right thing!
Joseph Wright
marvelty
Posts: 6
Joined: Tue Apr 14, 2009 8:14 pm

Lyx, math, and mhchem

Post by marvelty »

Thanks, it's good to know I'm not totally crazy. I think the problem has to do with Lyx hijacking the triggers for super/subscript rather than letting mhchem handle them. I've found I get the same \ensuremath{} insertion/error when trying to enter something like:

Code: Select all

V_{ERS}
, whether by typing directly in the math box, or pasting it in from outside. If it helps, what I am seeing in the error dialog for the above example would look like this:

Code: Select all

\ce{H2 + V_{ERS}}
(as typed) becomes
\ce{H2 + 2\ensuremath{V_{ERS}}}\end{equation}
and the actual error is "Arguement of /@firstofone has an extra }."
marvelty
Posts: 6
Joined: Tue Apr 14, 2009 8:14 pm

Lyx, math, and mhchem

Post by marvelty »

OK, well in case anyone's keeping track, I found a workaround for the second problem (I still have to cut and paste everything into the mathbox):

To make complex formulae work (such as O^2-) without having Lyx barf all over it in the meantime, use the $ to exclude the problem bits from being processed by mhchem, forcing them to be "math". For example, to make the oxide ion, copy O$^{2-}$ into the braces of \ce{ }. This results in the proper look (though it uses the math font in the exponent rather than the text font, it's barely noticeable). For making subscripts with chemical formulae in them, use a \cf{ } inside the $ exclusion, such as V$_{\cf{ZrO2}}$.

It's a bit of a hack, but it works. It also works for inserting symbols/greek letters and other bits of math code into chem. equations.
Post Reply