标签、引用和子图/子标题存在问题

标签、引用和子图/子标题存在问题

尊敬的 StackExchange 用户,

我想在我的文档中使用子图,但到目前为止我得到的解决方案扰乱了我在文档中使用标签和引用的能力。

\documentclass{scrartcl}

\usepackage{subcaption} % both packages throw me an error
\usepackage{subfig} % so choose one

\begin{document}
\begin{figure}
  \label{fig:first}
  \caption{First figure}
\end{figure}

Now I refer to it the first figure by~\ref{fig:first}.

\end{document}

无论我多么频繁地编写文档,我总是收到这样的消息

Package caption Warning: \label without proper reference on input line 8.

LaTeX Warning: Reference `fig:first' on page 1 undefined on input line 12.

LaTeX Warning: There were undefined references.

并且显示的是文档??而不是参考资料。该caption软件包也没有针对该特定错误消息的解决方案。

问候雅各布

答案1

添加标签基本上有三种方法:

  • 标题前 -错误的(标题会增加图形的计数器,因此在计数器更新之前不要放置标签)
  • 内部标题 -正确的
  • 标题后 -正确的

下面的一个带有鸭子和包的基本例子showlabels应该可以帮助您理解。

\documentclass[convert]{article}
\usepackage{tikzducks}
\usepackage[inline]{showlabels}
\begin{document}
\begin{figure}[h]
\centering
\label{fig:before}
\begin{tikzpicture}
  \randuck
\end{tikzpicture}
\caption{A duck with a label before caption.}
\end{figure}
\begin{figure}[h]
\centering
\begin{tikzpicture}
  \randuck
\end{tikzpicture}
\caption{A duck with a label inside caption.\label{fig:inside}}
\end{figure}
\begin{figure}[h]
\centering
\begin{tikzpicture}
  \randuck
\end{tikzpicture}
\caption{A duck with a label after caption.}
\label{fig:after}
\end{figure}
There are three ways in which you can put a label. The first one is in fig.~\ref{fig:before} and as you can see is wrong. The other two, the ones in fig.~\ref{fig:inside} and fig.~\ref{fig:after} are both right. But as you can see using the package \texttt{showlabels} they are in different places.
\end{document}

鸭子

答案2

如果将标签放在标题后面,代码应该可以工作:

    \begin{figure}
      \caption{First figure}\label{fig:first}
    \end{figure}

相关内容