For a syllabus .cls I'm updating for work, I already have a answer environment, that is only shown when the documentclass is provided the answers toggle (by default, the class receives noanswer). I use this toggle and environment to hide/show answers, depending on whether I compile the syllabus for students, or staff.

When I use a minted environment inside this answer environment, I get errors. I understand from the minted documentation that there might be a conflict with fancybox, which might as well occur with mdframed(?).

I've tried adapting my method to a minted-specific version by reading this earlier post, but with no success.

I have included three attempts in the MWE.

%% file: example.cls
\ProvidesClass{example}[For demonstration]





%% file: mwe.tex


% Three flavours of my answercode environment attempts, iterated on through the raised errors/documentation read.  Each includes a description of its origin and why I think it doesn't work.

% Direct copy of the answer environment.  In form this should work, but apparently, minted and NewdocumentEnvironment don't want to play together(?).

% Based on linked help thread. Doesn't work because the \if is not completed within the argument braces of \newenvironment

% Based on the above. Doesn't work because the \if wraps \begin and \end commands(?).


This text is always visible.

    This is only visible if \texttt{answers} is provided to the documentclass.


% With noanswers: no errors.
% With answers: FancyVerb Error:
% Extraneous input ` test \end {minted} \toggletitlecolors \fi \end {answercode1}' between \begin{minted}[<key=value>] and line end

%% With noanswers: no errors.
%% With answers: similar FancyVerb error:
% Extraneous input `test\end {minted} \toggletitlecolors \fi \end {answercode1}' between \begin{minted}[<key=value>] and line end

%% With noanswers: incomplete \iffalse.
%% With answers: same FancyVerb error.
% Extraneous input `test' between \begin{answercode2}[<key=value>] and line end.

%% With noanswers: Runaway argument? File ended while scanning use of \next.
%% With answers: similar FancyVerb error.
% Extraneous input `\else \begin {comment}\fi test' between \begin{answercode3}[<key=value>] and line end

So the concrete question is: how can I make an environment that contains a minted environment, and which can be hidden by setting a documentclass-level toggle?

  • Wrapping verbatim environments is always a tricky thing... Have you tried to use the comment package for that?
    – Rmano
    Aug 23 at 21:05
  • I tried, but with some undesired results. When variable names in the Python code include underscore characters, the compiler attempts to insert $s to activate math mode, which kinda messed with the rest of the document formatting. I did find a solution in just moving the \if@answers...\fi part outside of the environment definition.
    – pkok
    Aug 25 at 9:41

1 Answer 1


So after some good nights' sleep, I realized I could just move the \if@answers...\fi outside of the environment definition, and define the answercode environment for both @answers and @noanswers. This code has made it in the syllabus .cls:

        % Required when defining your own environments which include a minted environment

I have chosen to create an 'empty' \NewDocumentEnvironment if @noanswers, instead of creating a new comment environment with \includecomment{answercode}. The latter recognized _ characters and tried inserting $s to turn them into ill-defined math environments. That wasn't working for me. The empty \NewDocumentEnvironment at the end doesn't process the argument at all, so it seems.

I started out wanting to be able to include the minted environment in the custom answer/mdframed environments, but I'm okay with just closing and opening smaller environments.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .