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?
comment
package for that?$
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.