# Coloring part of an equation in chemmacros

For a chemistry lecture handout, I would like to emphasize specific parts of a chemical equation written with chemmacros with colors; however, I have not been able to use common symbols such as +, -, etc. as separators in the colored part of the code. The superscript character depend largely of how the color is specified. The following code highlights most of the issues I've encountered :

\documentclass{minimal}
\usepackage{xcolor}
\usepackage{chemmacros}
\chemsetup{modules=all}

\begin{document}
\begin{reactions*}
H2O &-> H+ + OH-\\
\color{green}H2O &-> H+ + OH-\\
H2O &-> {\color{cyan}H+ + OH-}\\
H2O &-> \color{blue}{H+ + OH-}\\
H2O &-> \color{red}{H^{+} + OH^{-}}\\
H2O &-> \textcolor{magenta}{H^{+} + OH^{-}}\\
\end{reactions*}
\end{document}

Such code, when typeset with (pdf)LaTeX, results in the following result:

Note that adding the fontspecpackage and typesetting with the LuaLaTeX gives a subtly different result (using only LuaLaTeX gives the pdf shown above):

The same problem takes place with the \ch command.

[EDIT after comments by @clemens :] Specifying the color "word by word" gave the right color but not the desired size and height; using chemformula's option input @{...} gave the desired size and height, but not the right color:

H2O &-> {\color{purple}H+} {\color{purple} + } {\color{purple} OH-}\\
H2O &-> {\color{violet}H+} @{format=\color{violet}} + {\color{violet} OH-}\\

• @clemens I checked, however there is no mention of coloring only one side or one part of the equation. I was able to find the code to color a single specie or the whole line. It may be that the \color command makes LaTeX ignore the spaces and thus chemformula sees the + as a triple bond, but I'm not sure about how \color works. – v_manz Nov 2 '19 at 20:04
• This is a nice question, but please keep in mind that color should be used with caution. On average, every 15th person exhibits color blindness of some form, and many people find using multiple colors (especially inconsistently) annoying. There are numerous studies in the field of scientific data representation, which mostly boil down to use black-and-white colors and emphasize via other means (try to change formatting or placement). – andselisk Nov 3 '19 at 6:59

The question isn't really about chemmacros but about how to color a part of a formula in chemformula (the package that is loadad per default by chemmacros for the reactions).

Your example doesn't work because it combines the three distinct compounds H+, +, and OH- into one H++OH-:

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

\ch{H2O -> {H+ + OH-}}\par
\ch{H2O -> H++OH-}

\end{document}

The solution depends a bit on how much of the formula you want to color. In each case the easiest solution is to use chemformula's escaping mechanism:

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

\ch{ H2O -> \textcolor{red}{H^+} + OH- } \par % one formula
\ch{ H2O -> "\color{red}" H+ + OH- } \par % the rest of the expression
\ch{ H2O -> "\begingroup\color{red}" H+ + "\endgroup" OH- }% more than one part

\end{document}

This works the same in the reaction* environment (but you should keep in mind that it works similar to a tabular in that the cells between & and \\ form groups…):

\documentclass{article}
\usepackage{xcolor}
\usepackage{chemmacros}
\chemsetup{modules=reactions}

\begin{document}

\begin{reactions*}
H2O &-> H+ + OH- \\
\color{green}H2O &-> H+ + OH- \\
H2O &-> "\color{cyan}" H+ + OH- \\
H2O &-> "\color{blue}" H+ + OH- \\
H2O &-> \color{red}H^{+} + OH^{-} \\
H2O &-> "\begingroup\color{magenta}" H^{+} + "\endgroup" OH^{-}
\end{reactions*}

\end{document}

By the way: it seems to be more intuitive with mhchem instead of chemformula:

\documentclass{article}
\usepackage{xcolor}
\usepackage{chemmacros}
\chemsetup{
formula=mhchem ,
modules=reactions
}

\begin{document}

\begin{reactions*}
H2O &-> H+ + OH- \\
\color{green}H2O &-> H+ + OH- \\
H2O &-> {\color{red}H^{+}} + OH^{-} \\
H2O &-> \textcolor{magenta}{H^{+} +} OH^{-}
\end{reactions*}

\end{document}

• Thanks @clemens ! – v_manz Nov 3 '19 at 10:04
• Just a last note : it seems that the escaping mechanism varies a bit depending on your OS/language setting. As for me (macOS, swiss-french qwertz keyboard) I had to use '\color{<color>}' to get the desired pdf. – v_manz Nov 3 '19 at 10:11
• @v_manz that sounds rather unlikely… " should be " no matter what keyboard or OS you use. – cgnieder Nov 3 '19 at 14:49