I would like to create a solution environment that prints a solution in the case
where an optional argument is set to true (the default) and hides the solution
in the case that the argument is false. I tried to use egreg's idea from
Is there an "identity" (or "no-op") environment that simply uses its contents unaltered?
but my construction already fails due to the optional argument (Illegal parameter number in definition of \endsolution). The idea would be to not show anything (ideally no
vertical space would be created either) if we are in the ``false'' case.
Note: I'm aware of other solutions to this problem (see Comparing packages which facilitate typesetting exercises and solutions: exercise vs. answers vs. probsoln) but I am interested to see what I am doing wrong in my construction.
\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{ifthen}
% solution environment
\newenvironment{solution}[1][true]{
\ifthenelse{\boolean{#1}}{\par{\sffamily\bfseries Solution}\par}{\ignorespaces}
}{\ifthenelse{\boolean{#1}}{}{\ignorespacesafterend}}
\begin{document}
This is Exercise 1 with solution.
\begin{solution}
Solution to Exercise 1
\end{solution}
\bigskip
This is Exercise 2 (without solution).
\begin{solution}[false]
This should not appear.
\end{solution}
\end{document}
xparse; Why can't the end code of an environment contain an argument? and Passing \newenvironment parameters to the end block? – cmhughes Apr 19 '14 at 20:26exsheetspackage; it truly is great. – jub0bs Apr 19 '14 at 20:30