0

As external software generates figures without labels similar to the figures included in the example, I am unable to reference them. However, I need to reference them in my text using e.g. \ref{fig:1}. ChatGPT proposed me a solution included below that will automatically add a label with an increasing number from a separate counter in the label, counting across sections that my figures are otherwises numbered within. Therefore, the links work and show the correct figure number, but when clicking them, you will always end up on the figure with the correct number in the first section (e.g., clicking figure 2.2 will send you to 1.2, 2.1 to 1.1).

I believe I am getting something wrong with the counters. Very happy for any advice. MWE below:

\documentclass{article}


\usepackage[english]{babel}
\usepackage[letterpaper,
            top=2cm,
            bottom=2cm,
            left=3cm,
            right=3cm,
            marginparwidth=1.75cm
            ]{geometry}
\usepackage{amsmath}
\numberwithin{figure}{section}

\newcounter{figcounter}
\let\oldfigure\figure
\let\oldendfigure\endfigure
\renewenvironment{figure}{
    \oldfigure
    \stepcounter{figcounter}
}{
    \label{fig:\thefigcounter}
    \oldendfigure
}


\usepackage[colorlinks=true, allcolors=blue]{hyperref}

\title{Your Paper}
\author{You}
\begin{document}
\maketitle

\section{Introduction}

Link to Figure \ref{fig:1}\\
Link to Figure \ref{fig:2}\\
Link to Figure \ref{fig:3}\\
Link to Figure \ref{fig:4}

\begin{figure}
    \centering
    first figure in section 1
    \caption{Caption}
\end{figure}

\begin{figure}
    \centering
    second figure but still section 1
    \caption{Caption}
\end{figure}

\clearpage
\section{Another Section}

\begin{figure}
    \centering
    second section's first figure
    \caption{Caption}
\end{figure}

\begin{figure}
    \centering
    second section's second figure
    \caption{Caption}
\end{figure}

\clearpage
\listoffigures

\end{document}
1
  • In this MWE you always call \caption, but if you want labels to refer to figcounter, you need to use \refstepcounter{figcounter}. You might also look at the hypcap package. Jun 3 at 13:21

1 Answer 1

3

You need to redefine \theHfigure to make the link target unique. I would also number the labels by section, that makes it easier to keep track of the correct numbers:

\documentclass{article}


\usepackage[english]{babel}
\usepackage[letterpaper,
            top=2cm,
            bottom=2cm,
            left=3cm,
            right=3cm,
            marginparwidth=1.75cm
            ]{geometry}
\usepackage{amsmath}
\numberwithin{figure}{section}

\usepackage[colorlinks=true, allcolors=blue]{hyperref}

\renewcommand\theHfigure{\arabic{section}.\arabic{figure}}
\AddToHook{env/figure/end}{\label{fig:\theHfigure}}


\title{Your Paper}
\author{You}
\begin{document}
\maketitle

\section{Introduction}

Link to Figure \ref{fig:1.1}\\
Link to Figure \ref{fig:1.2}\\
Link to Figure \ref{fig:2.1}\\
Link to Figure \ref{fig:2.2}

\begin{figure}
    \centering
    first figure in section 1
    \caption{Caption}
\end{figure}

\begin{figure}
    \centering
    second figure but still section 1
    \caption{Caption}
\end{figure}

\clearpage
\section{Another Section}

\begin{figure}
    \centering
    second section's first figure
    \caption{Caption}
\end{figure}

\begin{figure}
    \centering
    second section's second figure
    \caption{Caption}
\end{figure}

\clearpage
\listoffigures

\end{document}
3
  • Since he already has a monotonic counter, he could use \let\theHfigure=\thefigcounter. Jun 3 at 13:36
  • @JohnKormylo well I was thinking the other way round: as I can create a unique counter representation and a label with the existing counters there is no need to introduce another one. Jun 3 at 13:39
  • @JohnKormylo they only say that there are no labels and in the example there are always captions. Jun 3 at 13:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .