mhchem - adding more than two conditions/steps to reaction arrow in chemical equation

mhchem has support for denoting reaction conditions/steps above and below the reaction arrow using the following format:

\ce{A + B ->[text above][text below] C}

Is there a nice way to add multiline conditions both above and below the arrow, that doesn't go out of bounds, get left justified, and support \ce inside of it?

1 Answer

One way to go about it is using stackengine package. I don't suppose you require automatic numbering offered by the enumerate environment in this case, so manual numbering will suffice.

Here I use \stackon{anchor}{text} and \stackunder{anchor}{text} as an example and proof of concept:

\documentclass{article}
\usepackage[version=4]{mhchem}
\usepackage{stackengine}

\begin{document}
The first reaction is \ce{A + B -> C + D}.

% Adding multiline conditions
A sample reaction:
\ce{A + B ->[\stackon{2.~Add \ce{NaOH}.}{1.~Add \ce{HCl}.}][\stackunder{1.~Add \ce{HCl}.}{2.~Apply heat.}] C}

The next reaction would then go here. \ce{A + B -> C + D}.
\end{document}


Note that \ce can still be used within the stack. The specific settings and commands to use from stackengine depends heavily on what you actually want to typeset.

There's no simple way to align the two (top + bottom) stacks together, as far as I can see. (\mhchem sets the alignment of the text of the arrows to be center by default.) For now, you can just add in space manually via \hspace.

\documentclass{article}
\usepackage[version=4]{mhchem}
\usepackage{stackengine}

\renewcommand\stackalignment{l}
\savestack{\topcondition}{%
\scriptstyle\stackunder{1.~Add \ce{HCl}.}{2.~Add \ce{NaOH}.}%
}
\savestack{\botcondition}{%
\scriptstyle\stackunder{3.~Step3}{4.~Step 4}%
\hspace{20pt}%
}

\begin{document}
The first reaction is \ce{A + B -> C + D}.

% Adding multiline conditions
A sample reaction:
\ce{A + B ->[\topcondition][\botcondition] C}

The next reaction would then go here. \ce{A + B -> C + D}.
\end{document}


• When there are only three steps they usually get distributed as one on top two on the bottom (as opposed to all three on the same side). Is there a way to left justify all of them regardless of whether one is in a stack or not? (Stacks can be left justified via \renewcommand\stackalignment{l}, but for some reason, it only works with the stack below the arrow) – szentsas May 22 '18 at 1:31
• \renewcommand\stackalignment{l} left aligns both the top and bottom stack individually, as it should. It is mhchem that is centering them as a group. I'm presuming what you actually want is for the top and bottom stack to align left as a whole? – Troy May 22 '18 at 7:46
• Yes, that's what I need. Also, when there are three steps usually one goes on top and two on the bottom, meaning that there's no need for a stack on top. Is there a better way to align that left as well other than using a half-empty stack? – szentsas May 22 '18 at 16:44
• There's no need for a stack at all. Just type it as it is, then add some manual \hspace as I mentioned before. e.g. \ce{A + B ->[\topcondition][{3.~\ce{H2}\hspace{31pt}}] C}. – Troy May 22 '18 at 17:34