# Box around reaction chemmacros

I've been trying to use tcolorbox to frame a reaction using the reaction environment from chemmacros. The use of \begin{tcolorbox}and \end{tcolorbox} does work:

\documentclass{exam}
\usepackage{chemmacros}
\usepackage{tcolorbox}
\chemsetup{modules = all}

\begin{document}

Document starts

\begin{tcolorbox}
\begin{reaction}
3 O2 -> 2 O3
\end{reaction}
\end{tcolorbox}

document continues here

\end{document}


This produces the document with no errors; however, as expected, the box spans the text width. I want the box to fit the reaction, so I tried to use the tcbox command. However, if I put it before the reaction environment, I get a bad math delimiter error and the reaction number appears inside the box, and if I use it inside it won't compile (it says LaTeX error: "kernel/empty-search-pattern" Empty search pattern. For immediate help type H . \end).

Any suggestions?

• Apparently, the error is unrelated to tcolorbox, since it occurs with \fbox and \mboxas well. – CampanIgnis Mar 28 '17 at 23:54

When chemmacros starts a reaction it basically does three things:

• it temporarily sets the equation counter to be equal to the reaction counter
• it sets the tag form to reaction; for this it loads the mathtools package because the latter allows easy control of the tag form of an equation.
• it then puts the reaction content inside the formula controlling macro – per default chemformula's \ch.

The last point is the problem when you try to use a box macro inside a reaction.

But knowing the above we can simulate a reaction using equation which then allows us to put frames around parts of the content:

$$\refstepcounter{reaction}\usetagform{reaction} \ch{...} \tag{\thereaction}$$


For example using tcolorbox and its library theorems we can now put a framed box around the content using \tcboxmath or \tcbhighmath (see the tcolorbox manual for details):

\documentclass{article}
\usepackage{chemmacros}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}

\chemsetup{
formula = chemformula ,
modules = reactions
}

\begin{document}

Document starts
$$\refstepcounter{reaction}\usetagform{reaction} \tcboxmath{\ch{3 O2 -> 2 O3}} \tag{\thereaction}$$
document continues here
$$a + b = c$$

\end{document}


BTW: please don't start new paragraphs with a math or chemistry display unless it is absolutely necessary…

• It works! Thank you! I was thinking on defining a new "boxed" environment using the reaction counter, but since I don't have that many instances of this I think this is much easier. – ralk912 Mar 29 '17 at 17:17

I can propose this, based on empheq (needless to load amsmath since it loads mathtools):

\documentclass[svgnames]{exam}
\usepackage{chemmacros}
\usepackage{empheq}
\usepackage{tcolorbox}
\chemsetup{modules = all}
\newcommand*\widecolorbox[1]{\fboxsep=2ex\fboxrule=1pt\fcolorbox{black}{Gainsboro!40}{\hspace{1em}#1\hspace{1em}}}

\begin{document}

Document starts

%
\begin{empheq}[box =\widecolorbox]{equation}
\ch{
3 O2 -> 2 O3}
\end{empheq}
%
document continues here

\end{document}


• I see it works, however, I also use equations that are numbered differently than the reactions (hence why I'm using the reactions module), but this doesn't work with the reactions module by substituting "equation" with "reaction". Am I missing something here? – ralk912 Mar 29 '17 at 0:28
• I tested first with reaction, and it didn't woirk. That's why I used equation. You can use a different counter for some equations. There must be posts on TeX Stack Exchange about how to do that. – Bernard Mar 29 '17 at 0:35
• I tried using \newcounter{react} followed by \newenvironment{react}{\refstepcounter{react}\equation}{\tag{\thereact}\endequation} and the environment is not recognized by empheq. (Package keyval Error: react undefined. \begin{empheq}[box =\widecolorbox]{react}) – ralk912 Mar 29 '17 at 2:21