# How does mhchem print its superscripts and subscripts?

I need to replicate mhchem's way of typesetting sub- and superscripts. It's different from what $X^y$ and $Z_w$ do:

\documentclass{article}
\usepackage[version=4]{mhchem}
\usepackage[dvipsnames, svgnames, x11names, table]{xcolor}
\begin{document}
\noindent
\rlap{$\mathrm{A}_2^+$}%
\textcolor{PineGreen}{\ce{A2+}}
\end{document}

• Try again with \rlap{${\mathrm{A}_2}^+$} (Note the extra braces). – Henri Menke Dec 22 '15 at 13:14
• What are you up to? Maybe I can help even further than you ask for. – mhchem Dec 22 '15 at 17:25
• @mhchem I wanted to to draw a structure with chemfig that was consistent with mhchem's output. Inside a \chemfig{} all the content of \ce{} is considered a single "atom" (in chemfig terminology), so something like \ce{A+} would be too wide, having bonds pointing midway between the "A" and the plus sign. I resolved by defining two macros, \newcommand{\pluscharge}{\rlap{$_{\phantom{i}}^+$}} and an analogous for the minus sign, so \chemfig{A\pluscharge-[:-90]B} prints the atoms like mhchem would do and draws the structure correctly. – Arch Stanton Dec 22 '15 at 23:48
• Late edit for the above: for better spacing and alignment it's better to define the macro as \newcommand{\pluscharge}[1][]{\ensuremath{_{\vphantom{8}}^{#1+}}} (this also takes the number of charges as an optional argument) and treat the sign as a separate atom: \chemfig{A|\pluscharge-[:-90]B}. – Arch Stanton Dec 23 '15 at 17:31

mhchem places the index by itself without the superscript. This way the index is not shifted down, as it doesn't need room for the descenders of a superscript. The superscript is then applied to a seperate atom made up from the letter plus the index.

\documentclass{article}
\usepackage[version=4]{mhchem}
\usepackage{xcolor}
\begin{document}
\noindent
\rlap{${\mathrm{A}_2}^+$}%
\textcolor{green}{\ce{A2+}}
\end{document}


• For the record, if one needs to write only a superscript, he has to fake the presence of a subscript higher than one ex, otherwise the superscript will be lower in math mode than inside \ce{}. E.g. I use $\mathrm{A}_{\phantom{i}}^+$ (compare it with \ce{A+}) – Arch Stanton Dec 22 '15 at 13:37
• That's a good approximation. mhchem's internals are a little bit more complex, because there are more cases than just a number as subscript and a positive charge as subscript. Most important, this solution would work only in math mode, but not with text fonts, im headings, TOCs or page headers. – mhchem Dec 22 '15 at 17:24
• @mhchem What do you suggest to use then? – Arch Stanton Dec 22 '15 at 23:51
• If you do it by hand, everything is fine. You already figured out one special case, no subscript. There might be a few others, but you'll see. – But please use \vphantom{8} instead of \phantom{i}. It's better to use a number and you only need the vertical spacing. – mhchem Dec 23 '15 at 0:21