![条件章节编号](https://linux22.com/image/449207/%E6%9D%A1%E4%BB%B6%E7%AB%A0%E8%8A%82%E7%BC%96%E5%8F%B7.png)
我想找到或实现一个系统,其中 LaTeX 仅在编号的(子)部分后面跟着另一个(子)部分时才会编译该部分。否则,(子)部分将保持未编号状态。实际上,这意味着:
i. Section (numbered)
ii. Subsection (numbered)
ii. Subsection (numbered)
i. Section (numbered)
* Subsection (unnumbered)
i. Section (numbered)
ii. Subsection (numbered)
ii. Subsection (numbered)
* Subsubsection (unnumbered)
ii. Subsection (numbered)
因此,仅当 (子) 节的计数器在当前节级别内达到大于 1 的值时,我才需要 LaTeX 对 (子) 节进行编号。有人能帮我吗?
答案1
这显然需要一个双程系统。
在.aux
文件中我存储了必要的信息,并在下次运行时检查给定级别是否存在多个部门单元。
\documentclass{article}
\NewCommandCopy{\latexsubsection}{\subsection}
\NewCommandCopy{\latexsubsubsection}{\subsubsection}
% a generic infrastructure
\NewDocumentCommand{\genericlevel}{mmsO{#5}m}{%
\IfBooleanTF{#3}{%
#1*{#5}% call is \level*
}{%
\wantsnumberingTF{#2}{%
#1[#4]{#5}%
}{%
#1*{#5}%
\addcontentsline{toc}{#2}{#4}%
\stepcounter{#2}%
}
}
}
% redefine the levels to use the generic infrastructure
\RenewDocumentCommand{\subsection}{}{\genericlevel{\latexsubsection}{subsection}}
\RenewDocumentCommand{\subsubsection}{}{\genericlevel{\latexsubsubsection}{subsubsection}}
\ExplSyntaxOn
\NewDocumentCommand{\wantsnumberingTF}{mmm}
{
% first write the relevant info
\tl_set:Nx \l_tmpa_tl
{
\str_case:nn { #1 }
{
{subsection}{\arabic{section}}
{subsubsection}{\arabic{section}.\arabic{subsection}}
}
}
\iow_now:cx {@auxout}
{
\savednumber { \l_tmpa_tl } { \arabic{#1} }
}
% now check whether the relevant number is greater than zero
\cs_if_exist:cTF { savednumber\l_tmpa_tl }
{
\int_compare:nTF { \use:c { savednumber\l_tmpa_tl } > 0 } { #2 } { #3 }
}
{
#2
}
}
\NewDocumentCommand{\savednumber}{mm}
{
\cs_gset:cpn { savednumber#1 } { #2 }
}
\ExplSyntaxOff
\begin{document}
\tableofcontents
\section{Numbered}
\subsection{Numbered}
\subsection{Numbered}
\section{Numbered}
\subsection{Unnumbered}
\section{Numbered}
\subsection{Numbered}
\subsubsection{Numbered}
\subsubsection{Numbered}
\subsection{Numbered}
\subsubsection{Unnumbered}
\subsection{Numbered}
\end{document}