将 \comment 或 \begin{comment} 添加到 \AtBeginDocument?

将 \comment 或 \begin{comment} 添加到 \AtBeginDocument?

为了部分编译命令/环境?,我想在文档的开头添加\begin{comment}或(这样我就可以在代码的其他地方将其关闭)。\comment

因此,即使在更大范围内它是无用的 - 在这个 MWE 中,作为一项练习,我希望整个文本基本上通过命令行有条件地被清空(结果将是空白页,甚至没有 pdf)。

编辑:但是,我希望以这样的方式完成,以便我可以任意添加\end{comment}/\begin{comment}稍后在文档上,并且仅有的该代码片段编译。

\documentclass[11pt]{book}
\usepackage{lipsum}

\ifx\doskip\relax
  \typeout{DOSKIP}
  \usepackage{etoolbox}
  \usepackage{comment}
  % https://tex.stackexchange.com/questions/14135/how-to-automatically-add-text-immediately-after-begindocument
  %\AtBeginDocument{\comment}         % ! Extra \endgroup.
  %\AtBeginDocument{\begin{comment}}  % Runaway argument? ! File ended while scanning use of \next.
  %\AfterEndPreamble{\comment}        % ! Extra \endgroup.
  %\AfterEndPreamble{\begin{comment}} % Runaway argument? ! File ended while scanning use of \next.
  %\AtEndDocument{\endcomment}%{\end{comment}}
  % desperate try (also fails w/ ! Extra \endgroup.):
  \protected\def\startcomment{\expandafter\comment}
  \AtEndPreamble{
\startcomment
  } % ! Extra \endgroup.
\fi

\begin{document}

\chapter{Some chapter}
\section{Section One}
\lipsum[1-3]

% later I might want to use here:
% \end{comment}
% \lipsum[5] % this would be typeset
% \begin{comment} % from this point on, again blanked

\end{document}

这个想法是,如果通过...在命令行上调用 Latex,文档将被“清空”,
pdflatex "\let\doskip\relax\input{test.tex}"
但是,显然存在一些分组错误;否则,无需干预,只需使用pdflatex test.tex,就可以正常工作。

有什么想法可以实现吗?


EDIT2:更接近,但还没有到达:我见过kpsewhich comment.sty其他“注释”环境由 ... \excludecoment{versionb} 定义;这些环境用作 \begin{versiona} ... \end{versiona},其打开和关闭命令再次位于其自己的行上。这不是 LaTeX 环境:对于包含的注释,\begin 和 \end 行表现为不存在。特别是,它们并不意味着分组...

因此,在下面的例子中,使用\excludecomment{versionb},begin{document} 被成功覆盖,第一部分被跳过,然后注释被中断,\lipsum[5]被排版,并且文档编译没有错误 - 但除此之外,很难正确关闭未中断的(完全空白的文档)或在中断后继续空白:

\documentclass[11pt]{book}
\usepackage{lipsum}
\usepackage{trace}

\ifx\doskip\relax
  \typeout{DOSKIP}
  \usepackage{etoolbox}
  \usepackage{comment}
  \excludecomment{versionb}
  %\AtEndPreamble{ % here causes ! LaTeX Error: Missing \begin{document}.
  \AtBeginDocument{%
    % \expandafter\begin{versionb}\relax
    \begin{versionb}
  } % 
  %\AtEndDocument{\end{versionb}} % ! LaTeX Error: \begin{document} ended by \end{versionb}.
\fi

% \traceon
\begin{document}

\chapter{Some chapter}
\section{Section One}
\lipsum[1-3]

\end{versionb}
\lipsum[5]
% \begin{versionb}

%% \end{versionb} % can this be implicit, as per \AtEndDocument?
\end{document}
%%ENNDDD

可以将A\typeout添加到此宏中comment.sty

 \gdef\ProcessCommentLine#1^^M{\def\test{#1}
      \typeout{test: \meaning\test, \meaning\expandafter \csname End\CurrentComment Test\endcsname}

\end{versionb}...所以如果在之前没有\lipsum[5],我们可以看到问题发生是因为试图读取文件末尾以外的行:

...
test: macro:->\end{document}, \expandafter\end{versionb}
test: macro:->%%ENNDDD, \expandafter\end{versionb}
test: macro:->, \expandafter\end{versionb}
)
Runaway argument?
! File ended while scanning use of \next.
<inserted text> 
                \par 
<*> \let\doskip\relax\input{test.tex}

所以,我仍然需要帮助,以使该代码在所有情况下都能正常工作......

答案1

您可以在之后的序言中的某处使用以下代码\documentclass

\def\doskipA{\begin{document}\end{document}}
\ifx\doskip\relax \expandafter\doskipA\fi

答案2

好的,我终于找到了一些方法,尽管可能并不理想。下面的 MWE 行为如下:

  • 如果使用 进行编译pdflatex test.tex,则会忽略该comment内容,并且文档会完全编译;但日志中会生成这两个警告(不知道如何修复):

    (\end occurred inside a group at level 2)
    
    ### semi simple group (level 2) entered at line 86 (\begingroup)
    ### semi simple group (level 1) entered at line 86 (\begingroup)
    ### bottom level...
    
  • 如果使用 进行编译pdflatex "\let\doskip\relax\input{test.tex}";则有一个\excludecomment定义,称为SKIPLINES,并\begin{document}自动启动它;代码编译时不会出现警告,并且只会排版\end{SKIPLINES}和之间的段\begin{SKIPLINES}(请注意顺序),即此处的\lipsum[5]。此外,类似这样的消息将输出到终端:

    ...
    test: macro:->, macro:->\end{SKIPLINES}
    noend macro:-> macro:->\end{document}
    test: macro:->\end{document}, macro:->\end{SKIPLINES}
    yesend
    

我猜,这需要\excludecomment避免额外的(?)分组。但是,仍然需要一些技巧,因此代码包括 和 的修改版本{comment}\excludecomment特别\ProcessCommentLine是,正如@UlrikeFischer 指出的那样,如果在读取 时 Latex 处于“注释模式” \end{document},它将被跳过,从而导致错误;因此重新定义\ProcessCommentLine的还会检查作为停止注释模式的标记,然后如果遇到该条件,\end{document}则重新执行正确的操作。\end{document}

这意味着,人们应该能够在\doskip模式下随意添加\end{SKIPLINES}\begin{SKIPLINES}定义可编译区域的部分,而不必担心它们是否正确关闭\end{document}:一对\end{SKIPLINES}\begin{SKIPLINES}应该可以工作,就像没有它们(或带有额外的)的文档一样好\end{SKIPLINES}

显然,这不是一个真正合适的解决方案(这个解决方案不适用于\inputted 文件),所以我希望最终会出现一个更博学的答案;但是现在,这里是 MWE(对于造成的混乱,我们深表歉意):

\documentclass[11pt]{book}
\usepackage{lipsum}
\usepackage{trace}

\newif\ifSKIPLINESAct % if active
\ifx\doskip\relax
  \typeout{DOSKIP}
  \usepackage{etoolbox}
  \usepackage{comment}
  \usepackage{xstring}
  \def\excludecommentB
   #1{\message{Excluding comment '#1'}%
      %\csarg\newif{if#1Act}% declare globally; http://tex.stackexchange.com/questions/228994/handling-a-conditionally-defined-newif-nested-in-an-ifx-conditional
      \csarg\def{#1}{\endgroup \message{Excluding '#1' comment.}%
          \csname #1Acttrue\endcsname
          \begingroup
             \DefaultCutFileName \def\ProcessCutFile{}%
             \def\ThisComment####1{}\ProcessComment{#1}}%
      \csarg\def{After#1Comment}{%
        \global\csname #1Actfalse\endcsname% must globalize - inside a group here!
        \CloseAndInputCutFile \endgroup}
      \CommentEndDef{#1}}
  % redef this too:
  \csarg\xdef{EndDocTest}{\string\end\string{document\string}}
%   \showme\EndDocTest
  {\catcode`\^^M=12 \endlinechar=-1 %
   \gdef\xComment#1^^M{\ProcessCommentLine}
   \gdef\ProcessCommentLine#1^^M{\def\test{#1}
        \typeout{test: \meaning\test, \expandafter\meaning \csname End\CurrentComment Test\endcsname}
        \csarg\ifx{End\CurrentComment Test}\test
            \edef\next{\endgroup\noexpand\EndOfComment{\CurrentComment}}%
        \else \ThisComment{#1}\let\next\ProcessCommentLine
        \fi%
        % also add for end document; use \IfStrEq else catcodes could be a problem
        \IfStrEq*{\EndDocTest}{\test}{%
            %\traceon%
            \typeout{yesend}%
            \def\next{%\endgroup\noexpand\EndOfComment{\CurrentComment}%
            \endgroup\noexpand\EndOfComment{\noexpand\CurrentComment}% % must add \noexpand to \CurrentComment, else it leaks and gets typeset in the doc!
            %\SKIPLINESActfalse% no; use:
            \AfterSKIPLINESComment% % both sets false and ends group
            \enddocument%
            }%
        }{\typeout{noend \meaning\test\space\meaning\EndDocTest}%\ThisComment{#1}\let\next\ProcessCommentLine% don't execute here, just typeout
        }%
        \next}
  }
  \excludecommentB{SKIPLINES} % define, also \ifSKIPLINESAct
  %\AtEndPreamble{ % here causes ! LaTeX Error: Missing \begin{document}.
  \AtBeginDocument{%
    % \expandafter\begin{SKIPLINES}\relax
    \begin{SKIPLINES}
  } % ! Extra \endgroup.
  %\AtEndDocument{\end{SKIPLINES}} % ! LaTeX Error: \begin{document} ended by \end{SKIPLINES}. % redefine instead?
  %% no need for this:
  % \global\let\oldenddocument\enddocument
  % \gdef\enddocument{% redefine
  %   %\ifSKIPLINESAct\expandafter\end\expandafter{SKIPLINES}\fi
  %   \oldenddocument%
  % }
\else
  % still need this, when calling without \doskip:
  \makeatletter
  \newenvironment{SKIPLINES}{%
  \begingroup\def\@currenvir{document}%
  }{%
  % comment.sty: % sabotage LaTeX's environment testing \begingroup\def\@currenvir{#1}\end{#1}
  \begingroup\def\@currenvir{SKIPLINES}%\endgroup
  }
  % \def\endSKIPLINES{\begingroup\def\@currenvir{SKIPLINES}} % nope
  \makeatother
\fi

% \traceon
\begin{document}

\chapter{Some chapter}
\section{Section One}
\lipsum[1-3]

% \traceon
\end{SKIPLINES}
% \traceoff
\lipsum[5]
\begin{SKIPLINES} % will cause ### semi simple group (level 2) entered at line 90 if without \doskip and unclosed, but doc will compile

\lipsum[10]

% \end{SKIPLINES} % if this is uncommented when \doskip, it will allow the next typeout to execute (else they will be gobbled)

% these print to stdout only if \end{SKIPLINES} occured last previously:
\typeout{\meaning\endSKIPLINES}
\ifx\doskip\relax
  \ifSKIPLINESAct\typeout{SKIPLINES Active}\else\typeout{no SKIPLINES}\fi%
\else
\fi

\end{document}
%%ENNDDD

相关内容