0

I am trying to label a figure outside of the float environment. I am using the command \label{} and \ref{}. Unfortunately, Latex does print the correct number of the figure. Does someone know how to fix this? Thank you in advance!

\documentclass{article}
\usepackage[utf8]{inputenc}

\title{b} \author{} \date{February 2021}

\begin{document}

\maketitle

\section{Introduction}

\begin{itemize} \item f \begin{center} \includegraphics[width=0.5\textwidth]{Versuchsaufbau.JPG} \label{fig:Versuchsaufbau} \captionof{figure}{nj} \end{center} \end{itemize}

\end{document}

Adam
  • 95
  • 4
    Did you try inserting the \label command after \captionof{figure}{…}? – Bernard Feb 24 '21 at 14:51
  • 2
    Or, better yet, inside the second argument of \captionof? The correct placement for \label is \caption{<stuff>\label{<label>}} or \captionof{<type>}{<stuff>\label{<label>}}. \label always references the last increased counter. The counter is increased by \caption, that's why you have to put it either inside of or after \caption. But if you put it after \caption this can lead to inconsistent spacing in some border cases, so inside of \caption is the best place (just make sure to not put a space before/after \label). – Skillmon Feb 24 '21 at 14:54
  • Thank you! It worked – Adam Feb 24 '21 at 15:04
  • And \label should be after \captionof{...}{....}. – Zarko Feb 24 '21 at 15:25

1 Answers1

0

As others have said, labels must go after captions.

Did you try your MWE? It did not work for me as there were missing packages and no \ref{...} so of course there would be no figure number printed in the text body.

% figlabelprob.tex  SE 584762

\documentclass{article} \usepackage[utf8]{inputenc} \usepackage{graphicx} \usepackage{caption}

\title{b} \author{} \date{February 2021}

\begin{document}

\maketitle

\section{Introduction} \section{Another}

\begin{itemize} \item f \begin{center} \includegraphics[width=0.5\textwidth]{Versuchsaufbau.JPG} \captionof{figure}{nj} \label{fig:Versuchsaufbau} % \captionof{figure}{nj} \end{center} \end{itemize}

The figure is \ref{fig:Versuchsaufbau}.

\end{document}

The above code prints "The figure is 1." However, if you (un)comment the two \captionof... the code will print "The figure is 2" where the "2" refers to the number of the second section.

enter image description here

Peter Wilson
  • 28,066