除了给定的命令之外,如何将环境转换为注释?

除了给定的命令之外,如何将环境转换为注释?

comment 包允许将给定的环境(我们称之为toto)设置为注释并使其内容消失。我怎样才能做到这一点,同时仍然有一个命令(我们称之为\keepthis{...}内部命令)正在执行?例如

\begin{toto}
Stuff which will disappear...
\keepthis{Something which must appear nevertheless}
Again stuff which will disappear...
\end{toto}

换句话说,我们如何才能拥有一个comment类似 - 的环境,它可以删除所有文本、数学、命令,只保留单个命令及其参数?这意味着comment不仅要搜索,\end{toto}还要搜索\keepthis{...}通过恢复所有 s 等来执行的内容\catcode,然后再次搜索两者,直到找到\end{toto}真正恢复所有内容的内容。这可能吗?

答案1

用于l3regex提取适当的标记列表。

\documentclass{article}

\usepackage{xparse,environ,l3regex}

\ExplSyntaxOn
\NewEnviron{toto}
 {
  \bool_if:NTF \g_yannis_toto_show_bool
   { \BODY }
   { \par \yannis_toto_keep:V \BODY }
 }
\NewDocumentCommand{\showtoto}{}
 {
  \bool_gset_true:N \g_yannis_toto_show_bool
 }
\NewDocumentCommand{\hidetoto}{}
 {
  \bool_gset_false:N \g_yannis_toto_show_bool
 }

\bool_new:N \g_yannis_toto_show_bool
\seq_new:N \l__yannis_toto_keep_seq

\cs_new_protected:Nn \yannis_toto_keep:n
 {
  \regex_extract_all:nnN { \c{keepthis}\s*\cB..*?\cE. } { #1 } \l__yannis_toto_keep_seq
  \seq_use:Nn \l__yannis_toto_keep_seq { \par }
 }
\cs_generate_variant:Nn \yannis_toto_keep:n { V }
\ExplSyntaxOff

\newcommand{\keepthis}[1]{#1}

\begin{document}

\hidetoto

\section{One instance}

Here is a toto environment

\begin{toto}
Stuff which will disappear...
\keepthis{Something which must appear nevertheless}
Again stuff which will disappear...
\end{toto}

\section{Two instances}
Here is a toto environment

\begin{toto}
Stuff which will disappear...
\keepthis{Something which must appear nevertheless}
Again stuff which will disappear...
\keepthis{Something which must appear nevertheless}
Again stuff which will disappear...
\end{toto}

\showtoto

\section{One instance}
Here is a toto environment

\begin{toto}
Stuff which will disappear...
\keepthis{Something which must appear nevertheless}
Again stuff which will disappear...
\end{toto}

\section{Two instances}
Here is a toto environment

\begin{toto}
Stuff which will disappear...
\keepthis{Something which must appear nevertheless}
Again stuff which will disappear...
\keepthis{Something which must appear nevertheless}
Again stuff which will disappear...
\end{toto}

\end{document}

在此处输入图片描述

相关内容