# Figure caption within enumerate environment

My first ever question here. I want numbering of figures based on the enumerate number, kinda like \numberwithin{figure}{section} but for enumerate environment.

Sample Code:

\documentclass[10pt,a4paper]{report}
\usepackage{graphicx}
\renewcommand\labelenumi{Q\theenumi.}
\begin{document}
\begin{enumerate}
\item
\begin{enumerate}
\item Question 1 Part (a)
\item Question 1 Part (b)
\begin{figure}[h]
\centering
\includegraphics[width=0.2\linewidth]{imageQ1b}
\caption[Figure Q1(b)]{The caption should be appear as Figure Q1(b) instead of Figure 1: }
\end{figure}
\item Question 1 Part (c)
\begin{figure}[h]
\centering
\includegraphics[width=0.2\linewidth]{imageQ1c}
\caption{The caption should be appear as Figure Q1(c) instead of Figure 2: }
\end{figure}
\end{enumerate}
\item
\begin{enumerate}
\item Question 2 Part(a)
\begin{figure}[h]
\centering
\includegraphics[width=0.2\linewidth]{imageQ2a}
\caption{The caption appear as Figure Q2(a) instead of Figure 3: }
\end{figure}
\item Question 2 Part(b)
\end{enumerate}
\end{enumerate}
\end{document}


Thanks!

In principle this is just a matter of redefining the printed representation of the figure number by e.g.

\renewcommand{\thefigure}{Q\theenumi(\theenumii)}


(assuming you have just one figure per second level enumeration item, so the actual value of figure is irrelevant). However, if you have figures elsewhere in you document you will get strange numbering of those. So firstly this change should just be applied at the start of enumerate.

However, now the number of the other figures (outside enumerate) jumps over some values. One way around this is to define a newcounter

 \newcounter{curfigure}


save the original value of the figure counter in this at the beginning of the enumerate environment

\setcounter{curfigure}{\value{figure}}


and then restore it at the end via the corresponding \setcounter{figure}{\value{curfigure}}.

with cross references

As you don't want your figures to float, you should use a center environment and \captionof{figure}{....} from the caption package.

The wide figure numbers mean that labels in the list of figures overlap the text in the standard settings for report, so in the code below I have adjusted that too.

\documentclass[10pt,a4paper]{report}

\usepackage{graphicx,caption}

\renewcommand\labelenumi{Q\theenumi.}
\newcounter{curfigure}

\makeatletter
\renewcommand{\l@figure}{\@dottedtocline{1}{1.5em}{3em}}
\makeatother

\begin{document}

\listoffigures

\clearpage

\begin{figure}
\centering
Test figure.
\caption{Test figure.}
\label{fig:test}
\end{figure}

\begin{enumerate}
\setcounter{curfigure}{\value{figure}}
\renewcommand{\thefigure}{Q\theenumi(\theenumii)}
\item
\begin{enumerate}
\item Question 1 Part (a)
\item Question 1 Part (b)
\begin{center}
\centering
\includegraphics[width=0.2\linewidth]{example-image-a}
\captionof{figure}{The caption appears as Figure
\end{center}
\item Question 1 Part (c)
\begin{center}
\centering
\includegraphics[width=0.2\linewidth]{example-image-b}
\captionof{figure}{The caption appears as Figure Q1(c): instead
of Figure 2:}\label{fig:1c}
\end{center}
\end{enumerate}
\item
\begin{enumerate}
\item Question 2 Part(a)
\begin{center}
\centering
\includegraphics[width=0.2\linewidth]{example-image-a}
\captionof{figure}{The caption appears Figure Q2(a): instead of
Figure 3:}\label{fig:2a}
\end{center}
\item Question 2 Part(b)
\end{enumerate}
\end{enumerate}
\setcounter{figure}{\value{curfigure}}

\begin{figure}
\centering
Test figure two.
\caption{Test figure two.}
\label{fig:test-2}
\end{figure}

\begin{figure}
\centering
Test figure three.
\caption{Test figure three.}
\label{fig:test-3}
\end{figure}

The figures in the questions are \ref{fig:1b}, \ref{fig:1c}
and~\ref{fig:2a}.

The other figures are \ref{fig:test}, \ref{fig:test-2}
and~\ref{fig:test-3}.

\end{document}