1

Good day,

I'm trying to check for the solution environment and close it if it's open. I have tried this answer and checking directly for the @insolution toggle that is set by solution environment when exam document class is used, but both return false.

I'm pretty sure that I'm making some very stupid mistake, but can not find it.

Here's a minimal working example:

\documentclass{exam}
\printanswerstrue

\begin{document}
\begin{questions}

\question
\emph{TEST QUESTION}

\end{questions}
\begin{solution}

% Use @currenvir to check if we are in the solution environment
\ifx\@currenvir\@solution
currenvir is solution
\else
currenvir is not solution
\fi

% Use @insolutiontrue to check if we are in the solution environment
\if@insolution
in solution is TRUE
\else
in solution is FALSE
\fi
\emph{TEST SOLUTION}

\end{solution}
\end{document}

And here is what it produces:

MWE

As one can see, both checks return FALSE, while they should instead return TRUE.

What am I doing wrong here?

1 Answers1

1

Commands with the @ character can only be called between \makeatletter and \makeatother (see the answer here for a more detailed explanation on that).

The correct way to achieve what you are trying to do here would be to define new commands in the preamble, between \makeatletter and \makeatother. Also, the command \@solution is in fact not defined, so you have to define it. Here is a complete example.

\documentclass{exam}
\printanswerstrue
\makeatletter
\newcommand*{\@solution}{TheSolution}
\newcommand{\ifcurrenvirsolution}{%
    \ifx\@currenvir\@solution
        currenvir is solution
    \else
        currenvir is not solution
    \fi
}
\newcommand{\ifinsolution}{%
    \if@insolution
        in solution is TRUE
    \else
        in solution is FALSE
    \fi
}
\makeatother
\begin{document}
\begin{questions}

\question
\emph{TEST QUESTION}

\end{questions}
\begin{solution}

\ifcurrenvirsolution

\ifinsolution

\emph{TEST SOLUTION}
\end{solution}
\end{document}

Vincent
  • 20,157