如何在文件中的不同位置停用/啟用 etoolbox 全域命令效果?

如何在文件中的不同位置停用/啟用 etoolbox 全域命令效果?

我不知道如何做到這一點,我確實看了etoolbox文檔

問題很容易解釋:我使用 etoolbox在大型文件中的\clearpage任何內容之前附加 a ,以便每個部分都從一個新頁面開始。 \section(這是一本書本式的文檔,但下面的MWE是一篇文章)

有幾個部分很小,我想告訴 etoolbox 不要對以下部分執行此操作,然後告訴它在該部分完成後重新開始。

這個怎麼做? etoolbox 或 Latex 指令是什麼?

這是一個 MWE

\documentclass[12pt]{article}%        
\usepackage{etoolbox}\preto\section{\clearpage}
\usepackage{lipsum}
\begin{document}
\section{A}
\lipsum[75]

\section{B}
\lipsum[75]

%do not add \clearpage here, how? 
\section{C}
\lipsum[75]

%now start adding \clearpage, it is ok
\section{D}
\lipsum[75]
\end{document}

Texlive 2013,Linux mint。

答案1

一種可能性是建立一個開關,並根據開關是 true 還是 false 來執行命令\clearpage

\documentclass[12pt]{article}%        
\usepackage{etoolbox}
\usepackage{lipsum}

\newtoggle{clearpagebeforesection}

\preto{\section}{\iftoggle{clearpagebeforesection}{\clearpage}{}}

\begin{document}
\toggletrue{clearpagebeforesection}
\section{A}
\lipsum[75]

\section{B}
\lipsum[75]

%do not add \clearpage here, how? 
\togglefalse{clearpagebeforesection}
\section{C}
\lipsum[75]

%now start adding \clearpage, it is ok
\toggletrue{clearpagebeforesection}
\section{D}
\lipsum[75]

\section{E}
\lipsum[75]
\end{document}

答案2

一種可能性是創建一個本地命令(讀取在團體中) relaxes \clearpage:

% locally disable clearpage
\newcommand{\specialsection}[1]{
\begingroup
\let\clearpage\relax
\section{#1}
\endgroup
}

這種方式\clearpage預設為文件中的所有部分,但\specialsection不適用於 s。

完整的例子:

\documentclass[12pt]{article}%
\usepackage{etoolbox}

\preto\section{\clearpage}

% locally disable clearpage
\newcommand{\specialsection}[1]{
\begingroup
\let\clearpage\relax
\section{#1}
\endgroup
}

\usepackage{lipsum}
\begin{document}
\section{A}
\lipsum[75]

\section{B}
\lipsum[75]

% we use the new command
\specialsection{C}
\lipsum[75]

\section{D}
\lipsum[75]
\end{document}

相關內容