I'm typesetting a series of documents with the article class which present a series of problems with figures and/or tables, using the amsthm package. There are no sections in the document. What I'm aiming to do is to number each figure and table by the problem number, followed by the figure/table number. For example, the 3rd figure in the 4th problem would be numbered 'Figure 4.3', and the 2nd table in the 5th problem would be numbered 'Table 5.2'. For this purpose I wrote the following code (trimmed down to a MWE):
\documentclass[12pt]{article}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{hyperref}
\theoremstyle{definition}
\newtheorem{problem}{Problem}
\renewcommand{\thefigure}{\arabic{problem}.\arabic{figure}} % custom figure numbering
\renewcommand{\thetable}{\arabic{problem}.\arabic{table}} % custom table numbering
\newcommand{\newproblem}{\setcounter{figure}{0} \setcounter{table}{0}} % reset figure and table counter
\begin{document}
\title{Minimum Working Example}
\author{R. J. Drofnats}
\maketitle
\begin{problem}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Pellentesque quis est at lacus auctor semper sit amet nec orci.
Sed ultricies felis aliquam leo auctor aliquet.
Vivamus eget metus et ligula aliquet hendrerit vitae ac nisl.
Aenean eget diam ut nisi condimentum bibendum.
Sed nulla magna, mollis in blandit a, finibus eu risus.
Aliquam justo felis, tristique at diam sed, blandit ullamcorper nunc.
Mauris non diam et purus convallis dapibus.
Morbi eget egestas ligula, ornare cursus odio.
See Figure \ref{fig:mwe_1-1}.
See also Figure \ref{fig:mwe_1-2}.
\begin{figure}[ht]
\centering
\includegraphics[width=0.4\textwidth]{mwe_1-1.png}
\caption{A first figure in Problem 1.}
\label{fig:mwe_1-1}
\end{figure}
\begin{figure}[ht]
\centering
\includegraphics[width=0.4\textwidth]{mwe_1-2.png}
\caption{A second figure in Problem 1.}
\label{fig:mwe_1-2}
\end{figure}
\end{problem}
\newproblem
\begin{problem}
Curabitur cursus vitae diam sit amet imperdiet.
Pellentesque eleifend libero ut dui venenatis faucibus.
Proin iaculis ligula nisi, vitae rhoncus enim ultrices quis.
Donec quis lacus sit amet lacus posuere feugiat. Vestibulum in condimentum nulla.
Sed ornare velit sit amet eros dignissim egestas.
Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
In luctus condimentum elementum.
Duis quis volutpat lacus.
Fusce finibus enim eget turpis hendrerit, sed lacinia lectus efficitur.
Maecenas vel libero metus.
Cras quis placerat lacus.
Donec ac sapien eu tellus elementum egestas non dictum diam.
Donec a posuere neque, non euismod dolor.
See Figure \ref{fig:mwe_2-1}.
\begin{figure}[ht]
\centering
\includegraphics[width=0.4\textwidth]{mwe_2-1.png}
\caption{A first figure in Problem 2.}
\label{fig:mwe_2-1}
\end{figure}
\end{problem}
\end{document}
You may wish to copy the MWE to your machine and typeset it there to better understand my question.
The idea is that the figure and table counters would reset after every problem. The downside was that I would have to type in \newproblem or \newproblem* after every single problem, which obviously is very inelegant. This was originally a stopgap method to be used until I was better acquainted with the float system and hyperref.
The method worked for a while. However, very recently I tried to click on the hyperlink for Figure 2.1 in the MWE above, and it redirected me to Figure 1.1 instead. It appears that hyperref could only read the figure counter and not the problem counter, so it linked the references to Figure 1.1 and Figure 2.1 in the copy all to Figure 1.1, as they shared the same figure counter.
What I am looking for is a method to remedy this issue, i.e. design a method to allow figures and tables to be numbered as described above, while still linking references to figures with the same figure counter but different problem counters to the correct figures.
Any advice to avoid having to manually type \newproblem after every problem would be welcome as well.
\counterwithin{figure}{problem} \counterwithin{table}{problem}instead of redefining\thetableand\thefigureand manually resetting the counter after each problem? – leandriis Aug 11 '20 at 06:44problem. – Mico Aug 11 '20 at 07:03