7

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: typset with pdfLaTeX

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

typeset with LuaLaTeX

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

with option input

  • @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
8

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}

enter image description here

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}

enter image description here

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}

enter image description here


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}

enter image description here

| improve this answer | |
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.