[Tex/LaTex] Command to disable environments

conditionalsenvironments

I have defined an environment called answer that displays the answer of a question of a test I'm supposed to prepare.

The idea is to compile a version where the answers are displays and one were they are not (in order to test the students).

Is there an elegant way to disable environments?

One can of course define a command \ifanswer and then write \ifanswer{\begin{answer}foo\end{answer}}. For each environment. The problem with this approach however is that one needs to remember and insert this command each time, and of course forgetting this one time would be embarrassing.


MWE:

\documentclass{article}
\newenvironment{answer}{\textit{Answer} }{\textit{the end.}}
\begin{document}
Question: Bla
\begin{answer}
Foo
\end{answer}
\end{document}

Best Answer

As told by David, you can use comment package:

\documentclass{article}
\usepackage{comment}
\newenvironment{answer}{\textit{Answer} }{\textit{the end.}}
\includecomment{answer}    %%% comment out to show the answer
\begin{document}
Question: Bla
\begin{answer}
Foo answer
\end{answer}
\end{document}

If you want to do it the hard way -

\documentclass{article}
\usepackage{environ}
\usepackage{ifthen}
\newboolean{answers}
\setboolean{answers}{true}  %%% uncomment to show answers properly
%\setboolean{answers}{false}  %%% comment to show answers properly


\ifthenelse{\boolean{answers}}%
  {%
  \NewEnviron{answer}
    {%
    \textit{Answer}
    \BODY{}
    \textit{the end.}
        }%
}%
    {\NewEnviron{answer}
    {%
    \textit{Answer}
    \textit{the end.}
       }%
}%


\begin{document}
Question: Bla
\begin{answer}
Foo answer
\end{answer}
\end{document}
Related Question