
我正在嘗試編寫一個包含未編號部分(即\setcounter{secnumdepth}{0}
)和編號數字的文檔,其中數字在部分內編號。我無法secnumdepth
一起chngcntr
工作。我能得到的最好的是:
\documentclass{article}
\usepackage{chngcntr}
\counterwithin*{figure}{section}
%\setcounter{secnumdepth}{0}
\begin{document}
\section{Section the First }
\begin{figure}[h]
\centering
\caption{Figure A}
\end{figure}
\begin{figure}[h]
\centering
\caption{Figure B}
\end{figure}
\section{Section the Second}
\begin{figure}[h]
\centering
\caption{Figure C}
\end{figure}
\end{document}
這得到了正確的數字,但我有部分編號:
如果我取消註釋該行\setcounter{secnumdepth}{0}
,則這些部分將按照我的意願未編號,但數字現在再次「counterwithout」:
如何在未編號的部分實現每個部分的圖形編號?
答案1
計數器未重置的原因在公共\@sect
代碼中(請參閱latex.ltx
)
\def\@sect#1#2#3#4#5#6[#7]#8{%
\ifnum #2>\c@secnumdepth
\let\@svsec\@empty
\else
\refstepcounter{#1}%
\protected@edef\@svsec{\@seccntformat{#1}\relax}%
\fi
....
此程式碼測試節層級 ( #2
) 是否大於 的值secnumdepth
。如果不是這種情況,則節計數器會重新步進,並且計數器的重置清單中的所有其他計數器也被重置。
但是,如果計數器沒有重置,\counterwithout
這裡無論如何都沒用(無論是否使用\counterwithout*
或)\counterwithout
一種可能是強制\section
將數字計數器自動設為零,但是,這也可以使用\section*
。然而,我認為這並不是一個真正的問題。
\documentclass{article}
%\usepackage{chngcntr}
\usepackage{xpatch}
\setcounter{secnumdepth}{0}
%\counterwithin*{figure}{section} Not needed any longer
\xpretocmd{\section}{\setcounter{figure}{0}}{}{} % Prepend the \section code with a figure counter reset.
\begin{document}
\section{Section the First }
\begin{figure}[h]
\centering
\caption{Figure A}
\end{figure}
\begin{figure}[h]
\centering
\caption{Figure B}
\end{figure}
\section{Section the Second}
\begin{figure}[h]
\centering
\caption{Figure C}
\end{figure}
\end{document}