
為了部分編譯命令/環境?,我想在文檔的開頭添加\begin{comment}
or (這樣我就可以在程式碼的其他地方將其關閉)。\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}
這個想法是,如果在命令列上通過
pdflatex "\let\doskip\relax\input{test.tex}"
...調用 Latex,則該文件將被「空白」。否則,無需幹預,只需使用 即可pdflatex test.tex
正常工作。
有什麼想法可以如何實施嗎?
編輯2:更接近,但還沒有:我在“中看到kpsewhich comment.sty
”其他「評論」環境由 ... \excludecomment{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}
……因此,如果之前沒有 a \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}
)。
顯然,這不是一個真正正確的解決方案(這個解決方案不適用於\input
ted 文件),所以我希望最終會出現一個更博學的答案;但現在,這是 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