s
我想通过 '\NewDocumentEnvironment' 定义的环境内的参数类型进行本地切换。
如果新环境的第一个参数为star
(switch on),则环境内的所有代码都会被注释掉。如果不指定星号(switch off),则环境内的所有代码都会正常运行。这样可以方便代码测试。
有人能帮我解决这个问题吗?最好不要再使用任何其他软件包。
例子:
\documentclass{article}
\usepackage{listings}% only for test
\NewDocumentEnvironment{switchenv}{s}{...}{...}%how to do?
\begin{document}
% Test 1: all codes inside switchenv working in normal way
\begin{switchenv}
some code\\
\makeatletter\@for\i:={1,2,3}\do{(\i)}\makeatother
\par
some code\\
\ExplSyntaxOn
\clist_item:nn {1in ,2in ,3in}{2}\\
\ExplSyntaxOff
some code\\
\begin{lstlisting}
\for
\end{lstlisting}
\end{switchenv}
\vskip3ex
% Test 2: all codes inside switchenv be commented
|\begin{switchenv}*
some code\\
\makeatletter\@for\i:={1,2,3}\do{(\i)}\makeatother
\par
some code\\
\ExplSyntaxOn
\clist_item:nn {1in ,2in ,3in}{2}\\
\ExplSyntaxOff
some code\\
\begin{lstlisting}
\for
\end{lstlisting}
\end{switchenv}|
\end{document}
答案1
*
*
环境形式的名称中包含( tabular*
、align*
等)
\documentclass{article}
\usepackage{listings}% only for test
\usepackage[T1]{fontenc}% for |
\NewDocumentEnvironment{switchenv*}{+b}{}{\ignorespacesafterend}
\NewDocumentEnvironment{switchenv}{}{}{}
\begin{document}
% Test 1: all codes inside switchenv working in normal way
\begin{switchenv}
some code\\
\makeatletter\@for\i:={1,2,3}\do{(\i)}\makeatother
\par
some code\\
\ExplSyntaxOn
\clist_item:nn {1in ,2in ,3in}{2}\\
\ExplSyntaxOff
some code% uderfull hbox\\
\begin{lstlisting}
\for
\end{lstlisting}
\end{switchenv}
\vskip3ex
% Test 2: all codes inside switchenv be commented
|\begin{switchenv*}
some code\\
\makeatletter\@for\i:={1,2,3}\do{(\i)}\makeatother
\par
some code\\
\ExplSyntaxOn
\clist_item:nn {1in ,2in ,3in}{2}\\
\ExplSyntaxOff
some code% uderfull hbox\\
\begin{lstlisting}
\for
\end{lstlisting}
\end{switchenv*}|
\end{document}