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?

  • 4
    You need to say \makeatletter before trying to use commands with @ in their name (see: tex.stackexchange.com/q/8351/134574). – Phelype Oleinik Feb 6 at 14:44
  • ...and then \makeatother when you're finished making changes (ideally)! – rbrignall Feb 6 at 15:11
  • Got it, thank you very much. Now I have to also remove some \makeatletter as I copied them with some of the code. – Eichhorn Feb 6 at 19:54
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}

| improve this answer | |
  • 1
    \makeatletter and \makeatother can be used (almost) anywhere. The recommendation, however, is that all commands are defined in the preamble, and thus \makeatletter should not be necessary in the document – Phelype Oleinik Feb 6 at 15:58
  • Thanks! I was not so sure about that. – Vincent Feb 6 at 16:16
  • Thanks a lot, that works perfectly. – Eichhorn Feb 6 at 19:53

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.