如果子节=0,则按节对图表进行编号

如果子节=0,则按节对图表进行编号

我目前正在根据小节对表格和图表进行编号,如下所示:

\documentclass[twoside, 12pt]{article} 
\usepackage[english]{babel} 

\usepackage{chngcntr}
\counterwithin{figure}{subsection}
\counterwithin{table}{subsection}


\begin{document}

\section{foo}
\begin{figure} % I would like this figure to be numbered 1.1 as opposed to 1.0.1
    \centering
    \includegraphics[width=5cm]{pictures/Logo.jpg}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\subsection{xyz}
\begin{figure} % I would like this figure to be numbered 1.1.1 
    \centering
    \includegraphics[width=5cm]{pictures/Logo.jpg}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\section{bar}


\end{document}

基本上,每当我在输入子节之前添加表格或图形时,编号都会变成 x.0.y。有没有办法将表格/图形的编号更改为 \counterwithin{figure}{section},这样当 subsection=0 时它只计算节数,而当 subsection>0 时则按 \counterwithin{figure}{subsection} 进行编号?

任何建议都值得感激。

答案1

这会让读者感到困惑,尤其是当图表或表格浮在它所指的小节上方时。无论如何……

\documentclass[twoside, 12pt]{article} 
\usepackage[english]{babel} 
\usepackage{graphicx}
%\usepackage{chngcntr} % not needed with recent LaTeX

\counterwithin*{figure}{subsection}
\counterwithin*{table}{subsection}
% * means don't change the representation, because we'll do it now
\renewcommand{\thefigure}{%
  \ifnum\value{subsection}>0
    \thesubsection.%
  \else
    \thesection.%
  \fi
  \arabic{figure}%
}
\renewcommand{\thetable}{%
  \ifnum\value{subsection}>0
    \thesubsection.%
  \else
    \thesection.%
  \fi
  \arabic{table}%
}

\begin{document}

\section{foo}

Some text.

\begin{figure}[htp]
    \centering
    \includegraphics[width=5cm]{example-image}
    \caption{Caption}
    \label{fig:my_label-a}
\end{figure}

\subsection{xyz}

Some text.

\begin{figure}[htp]
    \centering
    \includegraphics[width=5cm]{example-image}
    \caption{Caption}
    \label{fig:my_label-b}
\end{figure}

\section{bar}


\end{document}

在此处输入图片描述

答案2

重新定义该图的编号。

字幕图片

\documentclass[twoside, 12pt]{article} 
\usepackage[demo]{graphicx} % remove [demo] 
\usepackage[british]{babel} 

\counterwithin{figure}{subsection}
\counterwithin{table}{subsection}


\begin{document}

\section{foo}
\begin{figure}\renewcommand{\thefigure}{\thesection.\arabic{figure}} % <-- added this
    \centering
    \includegraphics[width=5cm]{pictures/Logo.jpg}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\subsection{xyz}
\begin{figure} % I would like this figure to be numbered 1.1.1 
    \centering
    \includegraphics[width=5cm]{pictures/Logo.jpg}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\section{bar}


\end{document}

相关内容