# New environment for boxed equations

I would like to define new environments to highlight certain equations in some way. For example, I would like a highlighted equation to appear in red text or with a box around it. I have the red text version working but the boxed version is giving me a pair of related errors:

Missing } inserted. \begin{boxedeq*}

Extra }, or forgotten \$. \end{boxedeq*}

Since there are unmatched braces in the before and after sections of the new environment, it is my understanding that I need to use \bgroup and \egroup instead of { and }. This works for the red text case but the errors indicate that it isn't working for the boxed case (a "missing" } is inserted in the before section and an "extra" } is found in the after section).

Here is the MWE:

\documentclass{article}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{color}

% equation/equation* environments highlighted in red
\newenvironment{higheq$$}{$$\textcolor{red}\bgroup$$}{$$\egroup}
\newenvironment{higheq*$$}{$$\textcolor{red}\bgroup\begin{equation*}$$}{$$\end{equation*}\egroup}

% equation/equation* environments highlighted with box
\newenvironment{boxedeq$$}{$$\boxed\bgroup$$}{$$\egroup}
\newenvironment{boxedeq*$$}{$$\begin{equation*}\boxed\bgroup$$}{$$\egroup\end{equation*}}

\begin{document}
\begin{higheq}
\sin^2(x) + \cos^2(x) = 1
\end{higheq}

$$\boxed{\sin^2(x) + \cos^2(x) = 1}$$

\begin{boxedeq*}
\sin^2(x) + \cos^2(x) = 1
\end{boxedeq*}

\end{document}


And here is the output:

How do I avoid this error?

• \boxed\bgroup...\egroup is not at all equivalent to \boxed{...}. In the former case the argument to \boxed is \bgroup. You should look at the empheq package. Mar 6 '15 at 22:57
• @egreg So how do I get the equivalent of \boxed{...}?
– Null
Mar 6 '15 at 22:59

Just in case you are interested in fancier boxes, tcolorbox can be used and it nicely interacts with empheq. A little example showing some possibilities:

\documentclass[dvipsnames]{article}
\usepackage{empheq}
\usepackage[many]{tcolorbox}

\tcbset{
highlight math style={
enhanced,
colframe=NavyBlue!70!black,
colback=NavyBlue!10,
boxrule=1pt,
}
}

\newenvironment{boxedeq}
{\empheq[box=\tcbhighmath]{equation}}
{\endempheq}
\newenvironment{boxedeq*}
{\empheq[box=\tcbhighmath]{equation*}}
{\endempheq}

\begin{document}

\begin{boxedeq}
\sin^2(x) + \cos^2(x) = 1
\end{boxedeq}

\begin{boxedeq*}
\sin^2(x) + \cos^2(x) = 1
\end{boxedeq*}

\tcbset{
highlight math style={
enhanced,
colframe=Maroon,
colback=Peach!10,
arc=4pt,
boxrule=1pt,
}
}

\begin{boxedeq}
\sin^2(x) + \cos^2(x) = 1
\end{boxedeq}

\begin{boxedeq*}
\sin^2(x) + \cos^2(x) = 1
\end{boxedeq*}

\tcbset{
highlight math style={
enhanced,
colframe=black,
colback=white,
arc=0pt,
boxrule=1pt,
}
}

\begin{boxedeq}
\sin^2(x) + \cos^2(x) = 1
\end{boxedeq}

\begin{boxedeq*}
\sin^2(x) + \cos^2(x) = 1
\end{boxedeq*}

\end{document}


• Nice, way to go above and beyond!
– Null
Mar 7 '15 at 3:39

Saying \boxed\bgroup<tokens>\egroup is not the same as \boxed{<tokens>}. You can't use \bgroup as a substitute for an argument delimiter.

You accomplish your need with the empheq package.

\documentclass{article}
\usepackage{empheq}

\newenvironment{boxedeq}
{\empheq[box=\fbox]{equation}}
{\endempheq}
\newenvironment{boxedeq*}
{\empheq[box=\fbox]{equation*}}
{\endempheq}

\begin{document}

\begin{boxedeq}
\sin^2(x) + \cos^2(x) = 1
\end{boxedeq}

\begin{boxedeq*}
\sin^2(x) + \cos^2(x) = 1
\end{boxedeq*}

\end{document}


• Thanks. But what's the difference between \textcolor{red}{<tokens>} and \boxed{<tokens>} such that \bgroup and \egroup work for the former but not the latter?
– Null
Mar 6 '15 at 23:10
• @Null With color you're lucky because of how the command is implemented. Mar 6 '15 at 23:12
• So is there a better way to implement the red equation environment?
– Null
Mar 6 '15 at 23:15
• @Null Your code is pretty much equivalent to saying \color{red} in the “begin part” and in the “end part”. Mar 6 '15 at 23:26