# conditional variable not set true inside a macro

In my class file I have this code:

\newif\if@showsolution
\def\solutions{\@showsolutiontrue}

\if@showsolution
\newenvironment{solution}{\textbf{L\"osung:}\\}{}
\else
\excludecomment{solution}
\fi


In my document I then call \solutions{}, but nothing happens. The environment "solution" is not included. Why is showsolution not set true?

Here is a full code example:

• When the class is read, \excludecomment{solution} is executed. Doing \solutions in the document just changes the value of the conditional. – egreg Jul 3 at 17:17

The command \solutions just switches the conditional \if@solutions, but cannot change already executed code. And \excludecomment{solution} has been performed.

The class should do

\excludecomment{solution}
\newcommand{\solutions}{\renewenvironment{solution}{\textbf{L\"osung:}\\}{}}


You can define a command that switches environments depending on the value of \ifshowsolution and call it either at the beginning of your document or when you want to change the behavior of the solution environment.

To define the "on" environment, you can use the \specialcomment macro from the comment package. Activating a specific environment depending on a condition is the main purpose of this command.

\newif\ifshowsolution
\def\changeSolutionEnvironment{
\ifshowsolution
\specialcomment{solution}{\begingroup\textbf{L\"osung:}\\}{\endgroup}
\else
\excludecomment{solution}
\fi
}
\def\solutions{\showsolutiontrue\changeSolutionEnvironment}
\def\nosolutions{\showsolutionfalse\changeSolutionEnvironment}
\nosolutions % default behaviour