[Tex/LaTex] How to properly display chemical compounds within \equation? (mhchem won’t do what I need)

equationsmhchem

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}
\begin{equation} X_{Li_{2}(SO_{4})_{0.5}} \end{equation}

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

\begin{equation} X_{\ce{Li2(SO4)0.5} \end{equation}

\begin{equation} X_{\ce{Li2(SO4)_{0.5}} \end{equation}
\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!!

Best Answer

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:

\begin{equation} X_{\ce{Li2(SO4)0.5}} \end{equation}

\begin{equation} X_{\ce{Li2(SO4)_{0.5}}} \end{equation}

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}}

Adduct: \ce{Na2SO4.10H2O}

\begin{equation}
 X_{\ce{Li2(SO4)_{0.5}}}
\end{equation}

\end{document}

enter image description here

Alternatively one can use (my own) chemformula:

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

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

Adduct: \ch{Na2SO4 * 10 H2O}

\begin{equation}
 X_{\ch{Li2(SO4)_{0.5}}}
\end{equation}

\end{document}

enter image description here