# How do I properly display chemical compounds within \equation? (mhchem won't do what I need)

I am trying to programmatically convert a very large number of documents that have chemical compounds represented within math equations. I am converting from some flavor of RUNOFF (I think..it isn't well documented). I am having a few related problems displaying compounds both in and outside of equations. The mhchem package is great until I encounter decimal values within a compounds formula (as shown below). What I need is for the compound to be displayed in normal (non-italicized) type. There are four lines in the body below that illustrate my issues. The first line results in the compound being italicized. The second illustrates a related problem, that being that the portion enclosed in parenthesis is italicized. The third is how the \ce command handles a decimal point. The fourth shows a workaround for using the \ce command. The third and fourth lines also result in the equation number being flush with the equation instead of over on the right margin -- why?

\documentclass{article}
\usepackage{ulem}
\usepackage{fixltx2e}
\usepackage[version=3]{mhchem}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\begin{document}
$$X_{Li_{2}(SO_{4})_{0.5}}$$

Li_{2}(SO_{4})_{0.5}

$$X_{\ce{Li2(SO4)0.5}$$

$$X_{\ce{Li2(SO4)_{0.5}}$$
\end{document}


I've started looking into perhaps doing some Lua or modifying mhchem.sty. I've also tried using \text{}, \textnormal{}, etc. within my equation but apparently I haven't done it correctly yet. I am not terribly experienced with LaTeX and was hoping that somebody with more experience than me might step forward with something tidy.

Thanks for any help!!

-
@cgnieder: OK. I'll delete my comments shortly. –  Gonzalo Medina May 17 '12 at 15:06

Your code isn't working as it stands: the second line is not in math mode or inside \ce so _ must issue an error.

Li_{2}(SO_{4})_{0.5}


In the third and fourth line the groups are not balenced but miss a }. After correcting this:

$$X_{\ce{Li2(SO4)0.5}}$$

$$X_{\ce{Li2(SO4)_{0.5}}}$$


the behaviour of the third is documented mhchem behaviour to display adducts so the forth is not a workaround but the way to go.

\documentclass{article}
\usepackage[version=3]{mhchem}
\begin{document}

\ce{Li2(SO4)_{0.5}}

$$X_{\ce{Li2(SO4)_{0.5}}}$$

\end{document}


Alternatively one can use (my own) chemmacros:

\documentclass{article}
\usepackage{chemmacros}
\begin{document}

\ch{Li2(SO4)_{0.5}}

$$X_{\ch{Li2(SO4)_{0.5}}}$$

\end{document}


-
missed braces. i feel dumb!! i had worked out enclosing all my subscripted numbers in math mode last week before I started using mhchem and had since forgotten that. brain is just cooked a bit too much I guess. thanks very much for helping me. –  Huliax May 17 '12 at 18:58
btw, I looked over chemmacros a bit and I like them better. I'll bring it up with the customer and see if they have a preferences. –  Huliax May 17 '12 at 19:00
@Huliax Note that the syntax of chemmacros is different in details. Also it is more strict about its syntax. –  clemens May 17 '12 at 19:07

For example:

\ce{$3.043$ Fe2O3}


just use maths-escape mode, using  to embrace the number. There should not be any white spaces between \$ and the number otherwise the compiling would go wrongly, though this is not the case in normal equations.

-