Interaction of \BeforeBeginEnvironment and \NewEnviron

by Jonathan Gleason   Last Updated November 08, 2018 23:23 PM

Consider the following MWE.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{environ}
\usepackage{tcolorbox}
\usepackage{amsthm}

\BeforeBeginEnvironment{proof}{\tcblower}

\NewEnviron{solution}{\begin{proof}[Solution]%
\BODY%
\end{proof}%
}

\begin{document}

\begin{tcolorbox}
Upper box.
\begin{proof}[Solution]
My solution.
\end{proof}
\end{tcolorbox}

\begin{tcolorbox}
Upper box.
\begin{solution}
My solution.
\end{solution}
\end{tcolorbox}

\end{document}

The first tcolorbox displays as intended, whereas the second, which, naively, should produce the exact same thing, throws several errors: extra {s, \begin{xyz} ended by \end{notxyz}, blah blah, etc. etc..

On the other hand, if I just use \newenvironment{solution}{\proof[Solution]}{\endproof}, the thing compiles fine, but the \tcblower is no longer appended before \begin{solution}, so this doesn't automatically get set into the lower box.

I have at least two questions regarding this:

First of all, is it possible to get \BeforeBeginEnvironment (and related commands) to work with \<environment> and \end<environment>? Not only would this fix the problem, but, if this worked as one would hope, this provides a cleaner solution anyways (just one less package I have to load).

Secondly, what the hell is going on? I mean just look at it: this code is just begging to compile correctly!



Answers 2


\tcblower is a very special marker which ends an environment (the content box of the upper part) and begins another environment (the content box of the lower part). Therefore, all environments and TeX groups of the upper part have to be closed, before \tcblower is used. This also means that you cannot already begin an environment or TeX group for the lower part and use \tcblower afterwards.

\BeforeBeginEnvironment{proof}{\tcblower} does the right thing, namely adding \tcblower before the proof environment begins.

On the other hand, solution creates a new environment where \tcblower implicitely is used after the begin of the environment.

The avoid the problem, you could do the following:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{environ}
\usepackage{tcolorbox}
\usepackage{amsthm}

\newenvironment{myproof}[1][]{\begin{proof}[#1]}{\end{proof}}
\BeforeBeginEnvironment{myproof}{\tcblower}

\newenvironment{solution}{\begin{proof}[Solution]}{\end{proof}}
\BeforeBeginEnvironment{solution}{\tcblower}


\begin{document}

\begin{tcolorbox}
Upper box.
\begin{proof}[Solution]
My solution.
\end{proof}
\end{tcolorbox}

\begin{tcolorbox}
Upper box.
\begin{solution}
My solution.
\end{solution}
\end{tcolorbox}

\end{document}

Here, myproof and solution are two final environments which get an additional \tcblower.

Thomas F. Sturm
Thomas F. Sturm
June 29, 2016 06:24 AM

Alternatively, you could invoke a \tcblower for both the proof and solution environments and redefine \tcblower at the start of the solution environment so that you don't get a second \tcblower (which would cause the same problem that you encountered):

\BeforeBeginEnvironment{proof}{\tcblower}
\BeforeBeginEnvironment{solution}{\tcblower}

\NewEnviron{solution}{%
    \def\tcblower{}% <--- was already added before \begin{solution}.
    \begin{proof}[Solution]%
        \BODY%
    \end{proof}%
}

Now both tcolorbox environments yield identical results:

enter image description here

Code:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{environ}
\usepackage{tcolorbox}
\usepackage{amsthm}

\BeforeBeginEnvironment{proof}{\tcblower}
\BeforeBeginEnvironment{solution}{\tcblower}

\NewEnviron{solution}{%
    \def\tcblower{}%
    \begin{proof}[Solution]%
        \BODY%
    \end{proof}%
}

\begin{document}

\begin{tcolorbox}
Upper box.
\begin{proof}[Solution]
My solution.
\end{proof}
\end{tcolorbox}

\begin{tcolorbox}
Upper box.
\begin{solution}
My solution.
\end{solution}
\end{tcolorbox}

\end{document}
Peter Grill
Peter Grill
November 08, 2018 23:18 PM

Related Questions


Updated October 05, 2017 18:23 PM

Updated February 26, 2018 12:23 PM

Updated March 23, 2017 10:23 AM

Updated July 08, 2015 13:10 PM

Updated February 23, 2018 23:23 PM