切换隐藏铸造环境

切换隐藏铸造环境

对于我正在为工作更新的教学大纲 .cls,我已经有一个answer环境,该环境仅在 documentclass 提供answers切换时显示(默认情况下,类接收noanswer)。​​我使用此切换和环境来隐藏/显示答案,具体取决于我是否为学生或教职员工编写教学大纲。

当我minted在此answer环境中使用环境时,会出现错误。我从 minted 文档中了解到,可能与 存在冲突fancybox,这可能也与mdframed(?)一起发生。

我尝试通过阅读将我的方法调整为特定于 minted 的版本这篇早期文章,但没有成功。

我在 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}

所以具体的问题是:如何创建一个包含minted环境的环境,并且可以通过设置文档类级切换来隐藏它?

答案1

经过几个晚上的睡眠后,我意识到我可以将\if@answers...\fi环境定义的外部移开,并answercode为和@answers定义环境@noanswers。此代码已包含在大纲 .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

我选择创建一个“空”的\NewDocumentEnvironmentif @noanswers,而不是用 创建一个新comment环境\includecomment{answercode}。后者识别_字符并尝试插入$s 以将它们变成定义不明确的数学环境。这对我来说不起作用。\NewDocumentEnvironment最后的空根本不处理参数,所以看起来是这样。

我开始希望能够将minted环境包含在自定义answer/mdframed环境中,但我只需关闭和打开较小的环境就可以了。

相关内容