我想根据布尔条件显示和隐藏verbatim
或spverbatim
环境(例如\showenv
环境。我尝试了以下解决方案:根据布尔值隐藏自定义环境内容,例如使用
\usepackage{environ}
\NewEnviron{myenv}{
\ifshow
\spverbatim{\BODY}
\fi}
...
\begin{myenv}
Test 1
Test 2
\end{myenv}
我收到以下错误:
! Undefined control sequence.
\env@myenv@process -> \ifshow
! Extra \else.
! Extra \fi.
! Paragraph ended before \@tempa was complete.
! Paragraph ended before \verbatim@test was complete.
答案1
我认为 verbatim 包的 comment-environment 就是您要找的。下面是一个如何根据您的要求定义 myenv 的示例。
\documentclass{article}
\usepackage{verbatim}
\newif\ifshow
\newenvironment{myenv}%
{\ifshow\expandafter\verbatim\else\expandafter\comment\fi}%
{\ifshow\expandafter\endverbatim\else\expandafter\endcomment\fi}
\begin{document}
\showtrue
\begin{myenv}
Test 1
% some verbatim text
\end{myenv}
\showfalse
\begin{myenv}
Test 2
% some verbatim text
\end{myenv}
\end{document}