
番号付けされていないセクション (つまり ) と番号付けされた図 (セクション内では図に番号が付けられている) を含む文書を作成しようとしています。と を一緒に動作させること\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
。そうでない場合、セクション カウンターは refstep され、カウンターのリセット リスト内の他のすべてのカウンターもリセットされます。
ただし、カウンターがリセットされていない場合は、\counterwithout
ここではとにかく役に立たない(またはが使用されているかどうかに関係\counterwithout*
なく\counterwithout
)
1 つの可能性としては、数値カウンタを自動的にゼロに設定するように強制することが考えられます\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}