2

Take this as an example

\begin{figure}[htbp]
    \centering
    \includegraphics[keepaspectratio, width=0.8\linewidth]{transformer_img.pdf}
    \caption{Transformer}
    \label{fig:transformer}
\end{figure}

I've searched online but didnt find similar questions? Many questions such as this Is there any difference between nesting \label in \caption and putting \label outside \caption? did not tell me the differences between them.

I wonder where people learnt the basics of this?

Also, from my vague understanding, after using \label{fig:transformer} I can cross-reference the figure anywhere by using \ref{fig:transformer}. Is it right? Besides this, where else can \label be used for?

Qrrbrbirlbel
  • 119,821
  • Normally, \caption increments the figure counter (\refstepcounter{figure}) and writes the caption. \label writes \@currentlabel and \thepage to the aux file (\newlabel). The caption packaage and hyperref modify the code is several ways. – John Kormylo Feb 20 '23 at 17:15
  • @JohnKormylo Thank you! this is very complicated for me to understand atm. The only thing I noticed is that the information I input in \label{} is not shown anywhere in the output pdf. – user900476 Feb 20 '23 at 17:17
  • \label is like id= in html, it makes no output. It is just an internal name for \ref to reference – David Carlisle Feb 20 '23 at 17:17
  • @DavidCarlisle so what are \label{} used for? Im still very confused about this. PS: Oh thank you I see. So It is only used in \ref – user900476 Feb 20 '23 at 17:18
  • somewhere else in your document use see figure \ref{fig:transformer} and latex will fill in the number. It is like having <h2 id="foo">a heading</h2> in html which you link to with <a href="#foo">xxx</a> the text foo does not appear in the html – David Carlisle Feb 20 '23 at 17:21
  • Take a look at the aux file, which consists of LaTeX code to be implemented dusing \begin{document} in the next run. BTW, \newlabel{foo}{...} will create a global macro \r@foo. – John Kormylo Feb 20 '23 at 17:30
  • 3
    As to where people learned this, I read about it in Lamport's book. Others might have read about it in the Not So Short Guide, which is free. – Ian Thompson Feb 20 '23 at 17:38
  • 1
    also https://www.learnlatex.org/en/lesson-09 which is available in 9 languages, – David Carlisle Feb 20 '23 at 18:47
  • @IanThompson Thank you for the resources! – user900476 Feb 20 '23 at 20:07
  • @DavidCarlisle Thank you for the resources! – user900476 Feb 20 '23 at 20:07

1 Answers1

1

Here are some thoughts, deliberately phrased at an introductory to intermediate level.

  • For floating environments -- including the figure and table environments,\caption can be used without \label but \label without \caption won't generate the desired result.

  • The two floating environments provided by the LaTeX kernel are called figure and table, respectively. Both of these environments can take one or more \caption directives. Some LaTeX packages either provide additional "floating" envionments "out of the box" -- e.g., the algorithm package provides the algorithm float -- or make it reasonably straightforward for users to create new floating environments from scratch.

  • The main cross-referencing mechanism provided by the LaTeX kernel builds on \label and \ref statements.

  • If placed correctly, \label{<arg>} creates an association between <arg> and the value of the most-recently-incremented LaTeX counter variable. Conversely, \ref{<arg>} outputs the value of the counter associated with <arg>.

  • The condition "if placed correctly" used above means that, for figure and table floats, the \label directive must come after the \caption directive. Why? Because it's the \caption directive that increments the counters called figure and table, respectively. Incidentally, it is immaterial whether the \label directive is placed inside or outside (but after) the argument of the \caption directive. (By "outside but after", it is understood that the \label directive should placed before the current float environment ends. In practice, you can't go wrong if you place \label immediately after \caption.)

  • Several LaTeX packages, including prettyref, smartref, fancyref, hyperref, and cleveref, provide additional possibilities for creating cross-references. Some of these packages -- notably, hyperref and cleveref -- modify the default properties of the basic \label directive. For more information about these packages, please see the posting Cross-reference packages: which to use, which conflict?

Hope this helps.

Mico
  • 506,678
  • 2
    Note that @currentlabel is set locally, so \label has to be in the same environment or group as the \caption. – John Kormylo Feb 20 '23 at 17:38
  • @JohnKormylo - Thanks. Good point. I'll be happy to incorporate your point in my answer. I won't refer to \@currentlabel explicitly, though. :-) – Mico Feb 20 '23 at 17:48