I'm not sure why but you can't use \begin{align}
and \end{align}
in the definition of a new environment; you have to use the "lower-level" macros \align
and \endalign
instead. Edit: as pointed by alexwlchan in his comment, you can find more details about that in section 6 of Technical notes on the amsmath
package.
Here I've used the equivalent of an align*
environment (see Herbert's answer to Define a custom align, and align* environment).
Note that you will get an error if you try to reset your grammarcounter
at each chapter in the article
class, because the latter doesn't have chapters; \section
is the most high-level sectioning command in the article
class. Did you mean
\numberwithin{grammarcounter}{section}
instead?
\documentclass{article}
\usepackage{amsmath}
\newcounter{grammarcounter}[section]
\makeatletter
\newenvironment{grammar}
{%
\refstepcounter{grammarcounter}
\start@align\@ne\st@rredtrue\m@ne
\tag{$\Gamma_{\thegrammarcounter}$}
}{%
\endalign
}
\makeatother
\begin{document}
\section{Foo}
\begin{grammar}
\label{gr:label}
E &::= E + E \\
E &::= a \\
E &::= b
\end{grammar}
\begin{grammar}
\label{gr:label2}
E &::= E + E \\
E &::= a \\
E &::= b
\end{grammar}
\end{document}
The \ifthenelse
condition ends prematurely and leaves an open environment
hanging around in the middle of nowhere.
In conjunction with tcolorbox
environment, the end - delimiter is \endtcolorbox
and I suggest to use two \ifthenelse
statements, one for the start
code of the environment and another one for the end code.
A better approach would use \DeclareTColorbox
, in my opinion or a weird \scantokens
construct.
Also possible: Use \tcolorboxenvironment
to wrap around an existing solution
environment.
\documentclass{article}
\usepackage{ifthen}
\usepackage[most]{tcolorbox}
\newboolean{solution}
\newenvironment{solution}{%
\ifthenelse{\boolean{solution}}{%
\tcolorbox[breakable, width=\textwidth, colframe=red, colback=white]
}{%
}%
}{\ifthenelse{\boolean{solution}}{\endtcolorbox}{}}
\begin{document}
\setboolean{solution}{true}
\begin{solution}
\begin{align*}
x^2 + y^2 &= z^2\\
\Rightarrow x &= \sqrt{z^2 - y^2}\\
&= ...
\end{align*}
\end{solution}
\setboolean{solution}{false}
\begin{solution}
\begin{align*}
x^2 + y^2 &= z^2\\
\Rightarrow x &= \sqrt{z^2 - y^2}\\
&= ...
\end{align*}
\end{solution}
\end{document}
Cleaner solution with two different environments
\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbset{
commonboxes/.style={nobeforeafter},
nobox/.style={commonboxes,blank,breakable},
solutionbox/.style={commonboxes,breakable, colframe=red, colback=white}
}
\newtcolorbox{solutionbox}[1][]{
solutionbox,#1
}
\newtcolorbox{solutionbox*}[1][]{%
nobox,#1
}
\begin{document}
\begin{solutionbox*}
\begin{align*}
x^2 + y^2 &= z^2\\
\Rightarrow x &= \sqrt{z^2 - y^2}\\
&= ...
\end{align*}
\end{solutionbox*}
\begin{solutionbox}
\begin{align*}
x^2 + y^2 &= z^2\\
\Rightarrow x &= \sqrt{z^2 - y^2}\\
&= ...
\end{align*}
\end{solutionbox}
\end{document}
Third installment of a solution with \NewEnviron
and the \BODY
command.
\documentclass{article}
\usepackage{environ}
\usepackage{ifthen}
\usepackage[shortlabels]{enumitem}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage[most]{tcolorbox}
\newboolean{solution}
\tcbset{
commonboxes/.style={nobeforeafter,breakable},
nobox/.style={commonboxes,blank,breakable},
solutionbox/.style={commonboxes,breakable, colframe=red, colback=white}
}
\NewEnviron{solution}[1][]{%
\ifthenelse{\boolean{solution}}{%
\tcolorbox[solutionbox, width=\textwidth,#1]
\BODY
}{%
}%
}[\ifthenelse{\boolean{solution}}{\endtcolorbox}{}]
\begin{document}
\begin{enumerate}[label={\alph*)}]
\item Compute the Fourier transform of $e^{-|x|}$ for $x\in \mathbb{R}$.
\begin{solution}[colframe=blue]
\begin{align*}
\hat{f}(\xi)&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}e^{-|x|}e^{-ix\xi}dx\\
&=\frac{1}{\sqrt{2\pi}}\int_{0}^{\infty}e^{-x-ix\xi}dx+\int_{-\infty}^0e^{x-ix\xi}dx\\
&=\frac{1}{\sqrt{2\pi}}\int_{0}^{\infty}(e^{-x-ix\xi}-e^{-x+ix\xi})dx\\
&=\frac{1}{\sqrt{2\pi}}[\frac{1}{-(1+i\xi)}(-1)-\frac{1}{-1+i\xi}(-1)]\\
&=\frac{1}{\sqrt{2\pi}}[\frac{1-i\xi}{1+\xi^2}+\frac{-(1+i\xi)}{1+\xi^2}]\\
&=\frac{1}{\sqrt{2\pi}}\frac{-2i\xi}{1+\xi^2}\\
&=-\sqrt{\frac{2}{\pi}}\frac{i\xi}{1+\xi^2}
\end{align*}
\end{solution}
\item Compute the Fourier transform of $e^{-a|x|^2},~a>0$, directly, where $x\in \mathbb{R}$.\\
\begin{solution}
\begin{align*}
\hat{f}(\xi)&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}e^{-a|x|^2}e^{-ix\xi}dx\\
&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}e^{-a(x+\frac{i\xi}{2a})^2+\frac{-\xi^2}{4a}}dx~~~~~~~~x'\doteq x+\frac{i\xi}{2a}\\
&=\frac{1}{\sqrt{2\pi}}e^{-\frac{\xi^2}{4a}}\int_{-\infty}^{\infty}e^{-ax^2}dx\\
&=\frac{e^{-\frac{\xi^2}{4a}}}{2a}
\end{align*}
\end{solution}
\end{enumerate}
\setboolean{solution}{true}
\begin{enumerate}[label={\alph*)}]
\item Compute the Fourier transform of $e^{-|x|}$ for $x\in \mathbb{R}$.
\begin{solution}[colframe=blue]
\begin{align*}
\hat{f}(\xi)&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}e^{-|x|}e^{-ix\xi}dx\\
&=\frac{1}{\sqrt{2\pi}}\int_{0}^{\infty}e^{-x-ix\xi}dx+\int_{-\infty}^0e^{x-ix\xi}dx\\
&=\frac{1}{\sqrt{2\pi}}\int_{0}^{\infty}(e^{-x-ix\xi}-e^{-x+ix\xi})dx\\
&=\frac{1}{\sqrt{2\pi}}[\frac{1}{-(1+i\xi)}(-1)-\frac{1}{-1+i\xi}(-1)]\\
&=\frac{1}{\sqrt{2\pi}}[\frac{1-i\xi}{1+\xi^2}+\frac{-(1+i\xi)}{1+\xi^2}]\\
&=\frac{1}{\sqrt{2\pi}}\frac{-2i\xi}{1+\xi^2}\\
&=-\sqrt{\frac{2}{\pi}}\frac{i\xi}{1+\xi^2}
\end{align*}
\end{solution}
\item Compute the Fourier transform of $e^{-a|x|^2},~a>0$, directly, where $x\in \mathbb{R}$.\\
\begin{solution}
\begin{align*}
\hat{f}(\xi)&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}e^{-a|x|^2}e^{-ix\xi}dx\\
&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{\infty}e^{-a(x+\frac{i\xi}{2a})^2+\frac{-\xi^2}{4a}}dx~~~~~~~~x'\doteq x+\frac{i\xi}{2a}\\
&=\frac{1}{\sqrt{2\pi}}e^{-\frac{\xi^2}{4a}}\int_{-\infty}^{\infty}e^{-ax^2}dx\\
&=\frac{e^{-\frac{\xi^2}{4a}}}{2a}
\end{align*}
\end{solution}
\end{enumerate}
\end{document}
The \BODY
command contains the environment 'text' and is printed only in the case solution
is true.
Best Answer
When starting an environment
foo
by writing\begin{foo}
a couple of things happen; among these, the current environment's name is stored in the macro\@currenvir
, and then the macro\foo
is expanded. That's the reason why when defining wrappers for environments it's always a good idea to use the internal macros\foo
and\endfoo
: in this way you (usually) get clearer error messages.However,
tcolorbox
does things differently, and the macro\tcolorbox
(more exactly, some other macro a couple of expansions later) also sets\@currenvir
:I don't quite know the rationale behind this choice, but I guess that's (also) why a
tcolorbox
version of\newenvironment
is provided, namely\newtcolorbox
: