1

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
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{example}[For demonstration]

\RequirePackage{xcolor}
\colorlet{titlecolor}{blue}
\colorlet{support}{yellow}

\newif\if@answers
\DeclareOption{answers}{\@answerstrue}
\DeclareOption{noanswers}{\@answersfalse}
\ExecuteOptions{noanswers}
\ProcessOptions\relax
\LoadClass[10pt]{article}

\colorlet{lightbg}{titlecolor!7!white}
\colorlet{darkbg}{titlecolor!25!white}
\colorlet{lightsupport}{support!7!white}
\colorlet{darksupport}{support!25!white}

\DeclareRobustCommand{\toggletitlecolors}{%
  \colorlet{tempcolor}{titlecolor}
  \colorlet{titlecolor}{support}
  \colorlet{support}{tempcolor}
  
  \colorlet{tempcolor}{lightbg}
  \colorlet{lightbg}{lightsupport}
  \colorlet{lightsupport}{tempcolor}
  
  \colorlet{tempcolor}{darkbg}
  \colorlet{darkbg}{darksupport}
  \colorlet{darksupport}{tempcolor}
}

\RequirePackage{mdframed}
\NewDocumentEnvironment{answer}{+b}
  {\if@answers%
       \toggletitlecolors%
       \begin{mdframed}[linecolor=titlecolor,backgroundcolor=lightbg]#1\end{mdframed}%
       \toggletitlecolors%
   \fi}{}
%% file: mwe.tex
\documentclass[answers]{example}

\usepackage{minted}

% 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.
\makeatletter

% Direct copy of the answer environment.  In form this should work, but apparently, minted and NewdocumentEnvironment don't want to play together(?).
\NewDocumentEnvironment{answercode1}{+b}
  {\if@answers%
       \toggletitlecolors%
       \begin{minted}{python}#1\end{minted}
       \toggletitlecolors%
   \fi}{}

% Based on linked help thread. Doesn't work because the \if is not completed within the argument braces of \newenvironment
\newenvironment{answercode2}[1][]
 {\if@answers
  \VerbatimEnvironment
  \begin{minted}{python}}
 {\end{minted}\fi}

% Based on the above. Doesn't work because the \if wraps \begin and \end commands(?).
\usepackage{comment}
\newenvironment{answercode3}[1][]
 {\if@answers
  \VerbatimEnvironment
  \begin{minted}{python}\else\begin{comment}\fi}
 {\if@answers\end{minted}\else\end{comment}\fi}
 
\makeatother


\begin{document}

This text is always visible.

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

\begin{minted}{python}
this_works
\end{minted}

% 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
\begin{answer}
\begin{minted}{python}
test
\end{minted}
\end{answer}

%% 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
\begin{answercode1}
test
\end{answercode1}

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

%% 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
\begin{answercode3}
test
\end{answercode3}
\end{document}

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?

2
  • 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

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:

\if@answers
    \newenvironment{answercode}{%
        \toggletitlecolors%
        % Required when defining your own environments which include a minted environment
        \VerbatimEnvironment%
        \begin{minted}{python}%
    }{\end{minted}\toggletitlecolors}
\else
    \NewDocumentEnvironment{answercode}{+b}{}{}
\fi

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 .