更改部分以进行评论

更改部分以进行评论

我想要某些部分在特定条件下不显示。我知道评论包,但这需要 begin/end 来打开和关闭环境。

我想要的是对某个部分进行开关,以便将其(以及可能包括该部分的子部分)变成评论。

我有一份这样的文件:

\documentclass{memoir}
\usepackage{xkeyval}
\makeatletter
\define@cmdkeys{head}[head@]{level,label,role}{}
\presetkeys{head}{%
  label        = \@empty,
  role         = \@empty,
}{}
 \newcommand{\genHead}[1]{
    \@nameuse{\head@level}{#1}
}
\newcommand{\Headi}[2][]{
    \setkeys{head}{#1, level=section}
    \genHead{#2}
}
\newcommand{\Headii}[2][]{
    \setkeys{head}{#1, level=subsection}
    \genHead{#2}
}
\makeatother
\begin{document}
\def\currentrole{first} % defining what to stay
\Headi[role=first]{Section title to stay}
text
\Headi[role=second]{Section to disappear}
more text
  \Headii{subsection should disappear}
   sub text
\Headi{this one should turn back on}
text
\end{document}

在我的真实文档中,我进行了更多的处理并拥有更多的键(如果您想知道为什么我要不遗余力地覆盖部分创建)。

那么我可以做些什么来将完整的部分变成评论吗?

答案1

如果\clearpage在出现具有错误角色的分段命令时放置 对您来说没问题,那么下面的代码应该在atbegshi-package 的帮助下完成您想要的操作。请注意,您在具有错误角色的分段内增加的所有计数器仍会增加,因此在这些分段内使用tablefigure可能会导致与这些环境的标签相关的不良行为。这包括页码。

如果\clearpage不好,你可以看看 comment-包裹它具有有用的注释功能\includecomment{versiona}\excludecomment{versionb}。使用这些功能可能需要对您的文件进行修饰,但只需在相应版本中应有的部分周围放置\begin{versiona}...\end{versiona}\begin{versionb}...\end{versionb}。之后,您只需使用\includecomment/\excludecomment宏来打开/关闭它们。

\documentclass{memoir}
\usepackage{xkeyval}
\usepackage{atbegshi}
\makeatletter
\define@cmdkeys{head}[head@]{level,label,role}{}
\presetkeys{head}{%
  label        = \@empty,
  role         = \relax,
}{}
 \newcommand{\genHead}[1]{
    \@nameuse{\head@level}{#1}
}
\newif\ifrole@commentsection
\newif\ifrole@commentsubsection
\newcommand{\applyCommented}{%
    \csname ifrole@comment\head@level\endcsname%
        \clearpage%
        \global\csname role@comment\head@level false\endcsname%
    \fi%
    \ifx\head@role\currentrole%
    \else%
        \expandafter\ifx\head@role\relax%
        \else%
            \clearpage%
            \global\csname role@comment\head@level true\endcsname%
        \fi%
    \fi%
}
\newcommand{\Headi}[2][]{%
    \setkeys{head}{#1, level=section}%
    \applyCommented%
    \genHead{#2}%
}
\newcommand{\Headii}[2][]{%
    \setkeys{head}{#1, level=subsection}%
    \applyCommented%
    \genHead{#2}%
}
\AtBeginShipout{%
    \ifrole@commentsection%
        \AtBeginShipoutDiscard%
    \else\ifrole@commentsubsection%
        \AtBeginShipoutDiscard%
    \fi\fi%
}
\makeatother
\begin{document}
\def\currentrole{first} % defining what to stay
\Headi[role=first]{Section title to stay}
text
\Headi[role=second]{Section to disappear}
more text
  \Headii{subsection should disappear}
   sub text
\Headi{this one should turn back on}
text
\end{document}

编辑:如果您希望计数器保持不变,以下代码会使用额外的计数将计数器恢复到正确状态(适用于图形、表格、页面和公式)。如果您需要,您可以添加更多计数器(并且想要使用此代码而不是 -package comment)。

\documentclass{memoir}
\usepackage{xkeyval}
\usepackage{atbegshi}
\makeatletter
\define@cmdkeys{head}[head@]{level,label,role}{}
\let\shipout@bak\shipout
\presetkeys{head}{%
  label        = \@empty,
  role         = \relax,
}{}
 \newcommand{\genHead}[1]{
    \@nameuse{\head@level}{#1}
}
\newif\ifrole@commentsection
\newif\ifrole@commentsubsection
\let\refstepcounterbak\refstepcounter
\let\stepcounterbak\stepcounter
\newcommand{\applyCommented}{%
    \csname ifrole@comment\head@level\endcsname%
        \ifcsname tmp@pgcount\head@level\endcsname%
            \setcounter{page}{\csname tmp@pgcount\head@level\endcsname}%
            \setcounter{table}{\csname tmp@tabcount\head@level\endcsname}%
            \setcounter{figure}{\csname tmp@figcount\head@level\endcsname}%
            \setcounter{equation}{\csname tmp@eqcount\head@level\endcsname}%
        \fi%
        \let\refstepcounter\refstepcounterbak
        \let\stepcounter\stepcounterbak
        \clearpage%
        \global%
        \expandafter\global\csname role@comment\head@level false\endcsname%
    \fi%
    \ifx\head@role\currentrole%
    \else%
        \expandafter\ifx\head@role\relax%
        \else%
            \global\expandafter\newcount\csname tmp@pgcount\head@level\endcsname%
            \global\csname tmp@pgcount\head@level\endcsname=\value{page}%
            \global\expandafter\newcount\csname tmp@tabcount\head@level\endcsname%
            \global\csname tmp@tabcount\head@level\endcsname=\value{table}%
            \global\expandafter\newcount\csname tmp@figcount\head@level\endcsname%
            \global\csname tmp@figcount\head@level\endcsname=\value{figure}%
            \global\expandafter\newcount\csname tmp@eqcount\head@level\endcsname%
            \global\csname tmp@eqcount\head@level\endcsname=\value{equation}%
            \clearpage%
            \let\refstepcounter\relax
            \let\stepcounter\relax
            \expandafter\global\csname role@comment\head@level true\endcsname%
        \fi%
    \fi%
}
\newcommand{\Headi}[2][]{
    \setkeys{head}{#1, level=section}%
    \applyCommented%
    \genHead{#2}
}
\newcommand{\Headii}[2][]{
    \setkeys{head}{#1, level=subsection}%
    \applyCommented%
    \genHead{#2}
}
\AtBeginShipout{%
    \ifrole@commentsection%
        \AtBeginShipoutDiscard%
    \else\ifrole@commentsubsection%
        \AtBeginShipoutDiscard%
    \fi\fi%
}
\makeatother
\begin{document}
\def\currentrole{first} % defining what to stay
\Headi[role=first]{Section title to stay}
text
\Headi[role=second]{Section to disappear}
more text
  \Headii{subsection should disappear}
   sub text
\Headi{this one should turn back on}
text
\end{document}

相关内容