我想将文档第一章之后的图表编号从章节内改为章节内。但是,输入\counterwithin{figure}{chapter} \counterwithin{table}{chapter}
中间文档不起作用。使用星号选项确实会计算章节内的数字,但会将标题编号的格式重置为“图 2.1.1、2.1.2 等...”。
\documentclass[a4paper, 12pt]{report}
\usepackage[french]{babel}
\usepackage{caption}
\usepackage{chngcntr}
\begin{document}
% Setup caption numbering for first chapter
\renewcommand\thesection{\Alph{section}}
\counterwithin{figure}{section}
\counterwithin{table}{section}
\chapter{Chapter 1}
\section{Section 1}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\section{Section 2}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
% Setup caption numbering for subsequent chapters
\renewcommand\thesection{\thechapter.\arabic{section}}
\counterwithin{figure}{chapter}
\counterwithin{table}{chapter}
\chapter{Chapter 2}
\section{Section 1}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\section{Section 2}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\end{document}
答案1
看看这个解决方案是否适合您:
\documentclass[a4paper, 12pt]{report}
%\usepackage[french]{babel}
\usepackage{caption}
\usepackage{etoolbox}
\AtBeginEnvironment{figure}{%
\ifnum\value{chapter}=1
\renewcommand\thefigure{\thesection-\arabic{figure}}
\else
\renewcommand\thefigure{\thechapter-\arabic{figure}}
\fi}
\AtBeginEnvironment{table}{%
\ifnum\value{chapter}=1
\renewcommand\thetable{\thesection-\arabic{table}}
\else
\renewcommand\thetable{\thechapter-\arabic{table}}
\fi}
\begin{document}
\chapter{Chapter 1}
\section{Section 1}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\section{Section 2}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\chapter{Chapter 2}
\section{Section 1}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\section{Section 2}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\end{document}
在这种情况下,使用chngcntr
和宏\counterwithin{figure}{chapter}
并 \counterwithin{table}{chapter}
不会像您预期的那样工作。即使按照etoolbox
我在姆韦以上。因此我根据章节号使用\thefigure
和的重新定义。\thetable
答案2
您忘了拨打相关\counterwithout
指示。
\documentclass[a4paper, 12pt]{report}
\usepackage{chngcntr}
\begin{document}
% Setup caption numbering for first chapter
\renewcommand\thesection{\Alph{section}}
\counterwithin{figure}{section}
\counterwithin{table}{section}
\chapter{Chapter 1}
\section{Section 1}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\section{Section 2}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
% Setup caption numbering for subsequent chapters
\renewcommand\thesection{\thechapter.\arabic{section}}
\counterwithout{figure}{section}
\counterwithout{table}{section}
\counterwithin{figure}{chapter}
\counterwithin{table}{chapter}
\chapter{Chapter 2}
\section{Section 1}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\section{Section 2}
\begin{figure}[h!]
\caption{A figure}
\end{figure}
\end{document}