Graphics, Figures & TablesPlaced labels after captions but \ref{} still printing section number rather than figure/table number

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
vertebraille
Posts: 3
Joined: Sat Dec 28, 2019 9:23 am

Placed labels after captions but \ref{} still printing section number rather than figure/table number

Post by vertebraille »

I have read many posts on here inquiring as to why \ref{} produces a section number instead of the number of a labeled figure/table. In nearly all cases, the issue is that \label{} is placed before \caption{}. However, in the example below (see "this should show figure 2"), I have placed the label just before \end{figure} but nevertheless the section number is referenced. Can someone explain what is wrong?

Code: Select all

\documentclass[crop=false]{standalone}
\usepackage{graphicx}
\usepackage{hyperref}
\newenvironment{fignote}{\begin{quote}\footnotesize}{\end{quote}}

\title{Working Example}
\begin{document}
	
\ifstandalone	
\maketitle
\fi

\section{Foo}

This should show ``Figure 1'': \autoref{fig:fig_1}

\begin{figure}[h!]
	\centering
	\textbf{\caption{Title of Fig 1}}
	\includegraphics[width=0.98\linewidth]{ente.png}
	\label{fig:fig_1}
\end{figure}

This should show ``Figure 2'': \autoref{fig:fig_2}

\begin{figure}
	\centering
	\textbf{\caption{Title of Fig 2}}
	\includegraphics[width=\linewidth]{ente.png}
	\begin{fignote}
		Note: Here is a ton of text usually.
	\end{fignote}
	\label{fig:fig_2}
\end{figure}

\end{document}
Note: the example reproduces the issue but requires an image named ente.png - not sure if there are base images for minimal working examples.
Last edited by vertebraille on Fri Jun 11, 2021 10:33 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Placed labels after captions but \ref{} still printing section number rather than figure/table number

Post by Bartman »

Please use the Code button instead of the LaTeX button to mark multiline code.

I don't know what \textbf does internally, but you get the same result if you surround the second \caption command with braces, i.e. put it in a group.

Add the \label command after the \caption command in the argument of the \textbf command or better use the options of the caption package to make the captions and their labels bold.

Off topic: The csquotes package can help you to set the quotes.
vertebraille
Posts: 3
Joined: Sat Dec 28, 2019 9:23 am

Placed labels after captions but \ref{} still printing section number rather than figure/table number

Post by vertebraille »

1. I am not sure why \textbf{} around captions has that consequence, but it was the cause of the problem.

2. Tried grouping but it didn't seem to work.

3. For completeness, here is a working example that maintains captions bolding using the caption package but fixes the issue of numbering.

Code: Select all

\documentclass[crop=false]{standalone}
\usepackage{graphicx}
\usepackage{hyperref}
\newenvironment{fignote}{\begin{quote}\footnotesize}{\end{quote}}
\usepackage[font=bf]{caption}

\title{Working Example}
\begin{document}
	
\ifstandalone	
\maketitle
\fi

\section{Foo}

This should show ``Figure 1'': \autoref{fig:fig_1}

\begin{figure}[h!]
	\centering
	\textbf{\caption{Title of Fig 1}}
	\includegraphics[width=0.98\linewidth]{ente.png}
	\label{fig:fig_1}
\end{figure}

This should show ``Figure 2'': \autoref{fig:fig_2}

\begin{figure}
	\centering
	\caption{Title of Fig 2}
	\includegraphics[width=\linewidth]{ente.png}
	\begin{fignote}
		Note: Here is a ton of text usually.
	\end{fignote}
	\label{fig:fig_2}
\end{figure}

\end{document}
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Placed labels after captions but \ref{} still printing section number rather than figure/table number

Post by Ijon Tichy »

Additional explanation:

All \text… font commands use an internal group to delimit the font change to the argument. So such a font command restricts all local changes. The reference selection by \refsetpcounter is necessarily a local change. So with

Code: Select all

\textbf{\caption{foo}}
\label{foo}
the caption and the label are not in the same group and foo will reference the last label at the same (or higher) group. With

Code: Select all

\textbf{\caption{foo}\label{foo}}
the caption and the label are at the same group and the reference would work.

However using \caption inside a font command is somehow dangerous. It may work or not, depending on the class and packages you are using. If the class or a package inserts an automatic \normalfont, it would not work. I recommend not to use such not documented features but use a documented interface to change the font of the caption, e.g., the already suggested package.

BTW: standalone is a very special class and therefore IMHO not the best choice for a minimal working example. Better use a more general class like article.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply