隐藏自定义环境的所有实例(仅保留一个实例)

隐藏自定义环境的所有实例(仅保留一个实例)

我有一个文件,其中包含许多自定义环境,这些环境由计数器“exercisesheetnumber”编号。我想使用第二个计数器“printonly”来隐藏除“printonly”中给出的环境实例之外的所有实例。

这可能与问题有关根据布尔值隐藏自定义环境内容但我不知道如何挑选出一个不应该隐藏的环境。

答案1

这是建立在以下问题的答案之上的隐藏重组证明环境。我得走了,稍后我会注释代码。

\documentclass{article}

\newcounter{exsheetnumber}
\newcounter{onlysheet}

% this is (moreorless) whatever environment defition you want
\newenvironment{innerexsheet}{
  \paragraph{Sheet no.\@ \arabic{exsheetnumber}}
}{\textit{End of sheet.}}

\makeatletter
% `killcontents` environment removes all its contents
\usepackage{environ}
\NewEnviron{killcontents}{}
% `xkillcontents` does the same as `killcontents`
% and additionally it restores the correct definition of `exsheet`
\newenvironment{xkillcontents}
  {\killcontents}
  {\endkillcontents
    \global\let\exsheet\origexsheet
    \global\let\endexsheet\endorigexsheet
  }
% the environment `exsheet` itself:
\newenvironment{exsheet}{
  \stepcounter{exsheetnumber}
  \ifnum\value{onlysheet}=\value{exsheetnumber}
    % if we wabt to display the environment, we just use it
    \innerexsheet
    % and we use its end as well
    \let\endexsheet\endinnerexsheet
    % this is for the rest to work
    \let\reserved@a\relax
  \else
    % if we don't want to display anything, we step out of `exsheet`,
    \end{exsheet}
    % then we re-define `exsheet` to be `xkillcontents`
    \let\exsheet\xkillcontents
    \let\endexsheet\endxkillcontents
    % and we step in `exsheet` again, just we have to do it after `\fi`
    \def\reserved@a{\begin{exsheet}}
  \fi
  \reserved@a
}{}
% and we save the correct definition of `exsheet` as `origexsheet`
\let\origexsheet\exsheet
\let\endorigexsheet\endexsheet
\makeatother

\begin{document}

\setcounter{onlysheet}{5}

\begin{exsheet} ONE \end{exsheet}
\begin{exsheet} TWO \end{exsheet}
\begin{exsheet} THREE \end{exsheet}
\begin{exsheet} FOUR \end{exsheet}
\begin{exsheet} FIVE \end{exsheet}
\begin{exsheet} SIX \end{exsheet}
\begin{exsheet} SEVEN \end{exsheet}

\end{document}

相关内容