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}
\refstepcounter{figcounter}
. You might also look at the hypcap package.