我使用 chngcntr 包来相应地命名我的图形。
\counterwithin{figure}{section}
\counterwithin{figure}{subsection}
\counterwithin{figure}{subsubsection}
\counterwithin{figure}{paragraph}
我的问题是,当一个图仅出现在小节中而不出现在小节和段落中时,图名看起来是这样的:图 1.1.0.0.1。但我希望它看起来像这样:图 1.1.1 如果有人能帮忙,我将不胜感激 :)
答案1
你可以做到。但要三思(或者,最好多思几次)是否要做这件事。
\documentclass{article}
\usepackage{capt-of} % just for the example
\setcounter{secnumdepth}{4}
\counterwithin*{figure}{section}
\counterwithin*{figure}{subsection}
\counterwithin*{figure}{subsubsection}
\counterwithin*{figure}{paragraph}
\newcommand{\notifzero}[1]{%
\ifnum\value{#1}>0
\arabic{#1}.%
\fi
}
\renewcommand{\thefigure}{%
\notifzero{section}%
\notifzero{subsection}%
\notifzero{subsubsection}%
\notifzero{paragraph}%
\arabic{figure}%
}
\begin{document}
\captionof{figure}{A}
\section{B}
\captionof{figure}{C}
\subsection{D}
\captionof{figure}{E}
\subsubsection{F}
\captionof{figure}{G}
\paragraph{H} Some text
\captionof{figure}{I}
\section{J}
\captionof{figure}{K}
\end{document}
我以前\captionof
只是为了制作一张较短的图(否则几个图就需要更多的页面)。
答案2
这是一个使用该ifthen
包及其\ifthenelse
宏的解决方案。它适用于figure
以及表号。
\documentclass{article}
\usepackage[T1]{fontenc}
\counterwithin{figure}{paragraph}
\counterwithin{table}{paragraph}
\setcounter{secnumdepth}{4}
\usepackage{ifthen,etoolbox}
\newcommand\figandtabnums{%
\ifthenelse{\value{subsection}=0}{%
\renewcommand\thefigure{\thesection.\arabic{figure}}%
\renewcommand\thetable{\thesection.\arabic{table}}}{%
\ifthenelse{\value{subsubsection}=0}{%
\renewcommand\thefigure{\thesubsection.\arabic{figure}}%
\renewcommand\thetable{\thesubsection.\arabic{table}}}{%
\ifthenelse{\value{paragraph}=0}{%
\renewcommand\thefigure{\thesubsubsection.\arabic{figure}}%
\renewcommand\thetable{\thesubsubsection.\arabic{table}}}{%
\renewcommand\thefigure{\theparagraph.\arabic{figure}}%
\renewcommand\thetable{\theparagraph.\arabic{table}}}}}}
\pretocmd{\caption}{\figandtabnums}{}{}
\begin{document}
\section{Buenos días}
\begin{figure}[h!]\caption{First figure}\label{fig:uuu} \end{figure}
\begin{table}[h!]\caption{First table}\label{tab:uuu} \end{table}
\subsection{Bonjour}
\begin{figure}[h!]\caption{Second figure}\label{fig:vvv} \end{figure}
\begin{table}[h!]\caption{Second table}\label{tab:vvv} \end{table}
\subsubsection{Buon giorno}
\begin{figure}[h!]\caption{Third figure}\label{fig:www} \end{figure}
\begin{table}[h!]\caption{Third table}\label{tab:www} \end{table}
\paragraph{Kaliméra} What a day!
\begin{figure}[h!]\caption{Fourth figure}\label{fig:xxx} \end{figure}
\begin{table}[h!]\caption{Fourth table}\label{tab:xxx} \end{table}
\bigskip
Cross-references to Figures \ref{fig:uuu}, \ref{fig:vvv}, \ref{fig:www}, and \ref{fig:xxx}.
Cross-references to Tables \ref{tab:uuu}, \ref{tab:vvv}, \ref{tab:www}, and \ref{tab:xxx}.
\end{document}