在书籍类中,图片默认编号为 \thechapter.\arabic{figure}。但是,当一章中只有一个图片时,{figure} 编号是多余的。
在这种情况下,我该如何从图形标签中删除 \arabic{figure},以便单个图形仅被标记和引用为 Figure \thechapter 而不是 Figure \thechapter.\arabic{figure}
在我的 MWE 中,第 1 章包含两个图(带有默认标签),第 2 章仅包含一个图。如何让该图仅标记为图 2,而不是图 2.1
\documentclass{book}
\usepackage{graphicx}
\begin{document}
\chapter{First chapter with two figures}
In this chapter, Figures \ref{fig1.1} and \ref{fig1.2} are labeled as intended.
\begin{figure}[tbh]
\centering
\includegraphics[height=3cm]{example-image-a}
\caption{Figure is labeled 1.1 as intended}
\label{fig1.1}
\end{figure}
\begin{figure}[tbh]
\centering
\includegraphics[height=3cm]{example-image-b}
\caption{Figure is labeled 1.2 as intended}
\label{fig1.2}
\end{figure}
\chapter{Second chapter with one single figure}
In this chapter the only Figure \ref{fig2} should be labeled and referenced as just 2, not 2.1.
\begin{figure}[tbh]
\centering
\includegraphics[height=3cm]{example-image-c}
\caption{This is the only figure in this chapter. The .1 in the numbering is redundant and I would like to remove it.}
\label{fig2}
\end{figure}
\end{document}
我之所以要从孤立数字中删除 \arabic{figure} 是因为 .1 可以被解释为至少 .2 的含义。
请注意,我建议的编号使所有图形标签保持唯一。
答案1
如果您认为有更多章节只包含一个图片的标签,我建议关闭每个章节重置图片编号的功能。这样标签将在整个文档中保持唯一。
实现上述目标的一种方法是使用
\counterwithout*{figure}{chapter}
带星号的版本,以便\thefigure
整个文档的格式保持不变。然后,在不需要章节编号的图形环境中重新定义标签。slfigure
为此,我创建了一个名为的新环境,其中包含以下行
\renewcommand*{\thefigure}{\arabic{figure}}
以下是示例
\documentclass[openany]{book} % [openany] only for demonstration to avoid empty pages
\usepackage{graphicx}
\usepackage[Export]{adjustbox} % For \adjustboxset
\counterwithout*{figure}{chapter} % Prevents from resetting figure numbers
\adjustboxset{width=0.5\linewidth} % To avoid repetition with \inlcudegraphics[width=0.5\linewidth]{...}
% This environment redefines figure labelling, locally
\NewDocumentEnvironment{slfigure}{O{} +b}{
\begin{figure}[#1]
\renewcommand*{\thefigure}{\arabic{figure}}
#2
\end{figure}
}{}
\begin{document}
\chapter{Chapter with only one figure}
In this chapter the only Figure \ref{fig1} should be referenced as just 1, not 1.1.
\begin{slfigure}[tbh]
\centering
\includegraphics{example-image-a}
\caption{Single figure}
\label{fig1}
\end{slfigure}
\chapter{Chapter with several figures}
In this chapter, the Figures \ref{fig2.2} and \ref{fig2.3} can be referenced as intended.
\begin{figure}[tbh]
\centering
\includegraphics{example-image-b}
\caption{Figure is labeled 2.2 as intended}
\label{fig2.2}
\end{figure}
A sentence between figures
\begin{figure}[tbh]
\centering
\includegraphics{example-image-c}
\caption{Figure is labeled 2.3 as intended}
\label{fig2.3}
\end{figure}
\chapter{Chapter with only one figure}
The second chapter with a single Figure~\ref{fig:4}
\begin{slfigure}[tbh]
\centering
\includegraphics{example-image}
\caption{Single figure no. 4}
\label{fig:4}
\end{slfigure}
\end{document}
答案2
- 欢迎来到 TeX.SE!
- 您的问题不完全清楚:应该如何对之前插入的数字进行编号
\section{...}
? - 我预计,这就像没有章节一样
- 在这种情况下,请尝试在序言中添加以下代码片段:
\usepackage{etoolbox}
\AtBeginEnvironment{figure}%
{
\ifnum\arabic{section}=0
\renewcommand\thefigure{\thechapter.\arabic{figure}}
\else
\renewcommand\thefigure{\thesection.\arabic{figure}}
\fi
}
- 完成 MWE(稍微改变了你的 MWE)
\documentclass{book}
\usepackage{graphicx}
\usepackage{etoolbox}
\AtBeginEnvironment{figure}%
{
\ifnum\arabic{section}=0
\renewcommand\thefigure{\arabic{figure}}
\else
\renewcommand\thefigure{\thesection.\arabic{figure}}
\fi
}
\begin{document}
\chapter{Chapter with only one figure}
In this chapter the only Figure \ref{fig1} should be referenced as just 1, not 1.1.
\begin{figure}[ht]
\centering
\includegraphics[height=3cm]{example-image-a}
\caption{This is the only figure in this chapter. The .1 in the numbering is redundant and I would like to remove it.}
\label{fig1}
\end{figure}
\begin{figure}[ht]
\centering
\includegraphics[height=3cm]{example-image-a}
\caption{This is the only figure in this chapter}
\label{fig2}
\end{figure}
\chapter{Chapter with several figures}
\begin{figure}[ht]
\centering
\includegraphics[height=3cm]{example-image-a}
\caption{Figure is labeled 2.1 as intended}
\label{fig2.1}
\end{figure}
\section{first section in the second chapter}
\begin{figure}[ht]
\centering
\includegraphics[height=3cm]{example-image-a}
\caption{Figure is labeled 2.2 as intended}
\label{fig2.2}
\end{figure}
\end{document}
给出