我正在尝试创建一个环境,根据可选参数,该环境可能会或可能不会显示带有 minted 的源代码。我做了一些测试,发现以下代码不起作用。编译器显示\begin{document} ended by \end{VerbatimOut}
。问题是什么?
\usepackage{verbatim} % for \comment
\usepackage{fancyvrb}
\usepackage{minted}
\newif\ifdisplaycode
\newenvironment{ccode}[1][]
{
\IfSubStr{#1}{d}{\displaycodetrue}{\displaycodefalse}
%\ifdisplaycodefalse
\iftrue
\comment
\fi
\VerbatimEnvironment
\begin{minted}{julia}}
{
\end{minted}
\iftrue
\endcomment
\fi
}
\begin{ccode}[d]
a=1
b=2
\end{ccode}
答案1
\documentclass{article}
\usepackage{verbatim} % for \comment
\usepackage{fancyvrb}
\usepackage[cache=false]{minted}
\newif\ifdisplaycode
\makeatletter
\newenvironment{ccode}[1][]{%
\IfSubStr{#1}{d}{\displaycodetrue}{\displaycodefalse}%
\ifdisplaycode\expandafter\@secondoftwo\else\expandafter\@firstoftwo\fi
{\comment}{%
\VerbatimEnvironment
\begin{minted}{julia}%
}%
}{%
\ifdisplaycode\expandafter\@secondoftwo\else\expandafter\@firstoftwo\fi
{\endcomment}{\end{minted}}%
}%
\makeatother
\begin{document}
\begin{ccode}[d]
a=1
b=2
\end{ccode}
\end{document}