4

Is it possible to use colors and underbraces in an mhchem environment? I've been searching for it but couldn't find anything. Here is my code:

\documentclass[]{article}
\usepackage{mhchem,amsmath}
\begin{document}
\ce{\color{red}{O2} -> 2O}
\ce{\underbrace{O2}_{oxygen molecule} -> 2O}
\ce{\color{red}{\underbrace{O2}_{oxygen molecule}} -> 2O}
\end{document}

In the first one I am trying to make O2 red, but it doesn't work and messes up the environment a bit (the 2 doesn't appear as a subscript anymore).

For the second one, I am trying to write something underneath the O2 with an underbrace but it doesn't work either. The formatting messes up!

In the last one I am trying to do both of these at the same time, but of course it doesn't work.

If anyone could tell me what I'm doing wrong, I'd appreciate it! Again, it doesn't seem like any of these functions are documented online anywhere, so I decided that asking here could help.

1
  • Welcome to TeX. SE. See if \ce{$\underbrace{\color{red}\text{O2}}_{\text{oxygen molecule}}$ -> 2O} gives desired result. Of course, in preamble you had to ad package xcolor (or color).
    – Zarko
    Dec 22, 2016 at 19:53

1 Answer 1

5

Maybe this? The \ce must be reutilized as the innermost macro, in order to derive the benefits of the mhchem parser.

\documentclass[]{article}
\usepackage{amsmath,xcolor,mathtools}
\usepackage[version=4]{mhchem}
\begin{document}
\ce{O2}

\ce{\textcolor{red}{\ce{O2}} -> 2O}

\ce{$\underbrace{\ce{O2}}_{\textrm{oxygen molecule}}$ -> 2O}

\ce{$\underbrace{\textcolor{red}{\ce{O2}}}_{\textrm{oxygen molecule}}$ -> 2O}

\ce{$\underbrace{\ce{O2}}_{\mathclap{\textrm{oxygen molecule}}}$ -> 2O}

\ce{$\underbrace{\textcolor{red}{\ce{O2}}}_{\mathclap{\textrm{oxygen molecule}}}$ -> 2O}
\end{document}

enter image description here

1
  • Wow, this is perfect and incredibly detailed! Thank you for the advice. It worked really well! :)
    – Ryland Doe
    Dec 22, 2016 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .