0

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
2

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:}\\}{}}
0

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

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.