# @currenvir check returns false

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}

\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:

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

What am I doing wrong here?

• You need to say \makeatletter before trying to use commands with @ in their name (see: tex.stackexchange.com/q/8351/134574). Feb 6, 2020 at 14:44
• ...and then \makeatother when you're finished making changes (ideally)! Feb 6, 2020 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. Feb 6, 2020 at 19:54

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}
\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}


• \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 Feb 6, 2020 at 15:58
• Thanks! I was not so sure about that. Feb 6, 2020 at 16:16
• Thanks a lot, that works perfectly. Feb 6, 2020 at 19:53