7

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!

  • The trouble is tcolorbox already uses environ. (And so you wouldn't save a package regardless.) – cfr Jun 29 '16 at 1:48
  • You can work around this. You can also add \tcblower to \proof. However, I think the more basic problem is the specific way in which the different environs work and how \tcblower is implemented. Because you can use a \NewEnviron to wrap an existing environ environment. But I don't think that \tcblower is compatible with this. – cfr Jun 29 '16 at 2:03
  • There are times when I think wipet is right about LaTeX and many of those times involve environ. This is not a criticism of environ, really. (I can't do this because prooftrees not only uses environ but nests environ environments in the way I just said is possible.) – cfr Jun 29 '16 at 2:05
  • @cfr \pretocmd{\proof}{\tcblower}{}{} and \apptocmd{\proof}{\tcblower}{}{} both don't work. Am I doing this wrong, or did you have something else in mind? – Jonathan Gleason Jun 29 '16 at 5:56
  • one can do this with \BeforeBeginEnvironment{solution}{\tcblower} rather than proof – touhami Jun 29 '16 at 6:32
8

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

0

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}

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.