1

I am trying to write exam papers which include graphical elements. I have been able to make the figure reference match the question number but when there are two figures in a question I would like to reference each figure with the format eg 2.1 and 2.2. for figures in question 2. I need this to be automatic to give me the option of moving questions for a best fit. I feel that I probably need to use sections and subsections but have had no success. In the simple code below you will see Q3 has two figures which are currently Fig.3 and Fig.4 - I would like them to be Fig.3.1 and Fig.3.2.

\documentclass[12pt,a4paper]{article}
\usepackage[left=12mm, top=12mm, right=12mm, bottom=12mm]{geometry}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{enumerate}
\usepackage[shortlabels]{enumitem}
\usepackage[figurename=Fig.]{caption}
\usepackage{float}
\usepackage{pgfplots}
\usepackage{tkz-euclide}

\begin{document}
\begin{enumerate}

\item Question 1

\setcounter{figure}{\value{enumi}}%
\item Fig.\ref{A1} shows triangle \textit{ABC}.
\begin{figure}[H]
    \centering{\begin{tikzpicture}[scale = 0.4]
            \draw [line width = 0.5mm] (0,0) coordinate(A) node[left]{A}--++(0:3) coordinate(B) node[right]{B}--++(64:4) coordinate(C) node[above]{C}--cycle;
        
        \end{tikzpicture}
        \caption{} \label{A1} }
\end{figure}


\item Fig.\ref{A2} shows a sector of a circle of radius 8 cm.\\[2mm]
The angle of the sector is 2.1 radians.
\begin{figure}[H]
    \centering{         \begin{tikzpicture}[scale=0.4]
            \draw [line width = 0.5mm,fill=gray!30] (0,0) --  (220:4)  arc(220:120:4) -- cycle ;
        
        \end{tikzpicture}
        \caption{} \label{A2} }
\end{figure}

Fig.\ref{A3} shows a sector of a circle of radius 8 cm.\\[2mm]
The angle of the sector is 2.1 radians.
\begin{figure}[H]
    \centering{         \begin{tikzpicture}[scale=0.4]
            \draw [line width = 0.5mm,fill=gray!30] (0,0) --  (220:4)  arc(220:120:4) -- cycle;         
        \end{tikzpicture}
        \caption{} \label{A3} }
\end{figure}

\setcounter{figure}{\value{enumi}}%
\item Another Question. Fig \ref{A4} 
\begin{figure}[H]
    \centering{ \begin{tikzpicture}[scale=0.4]
            \draw [line width = 0.5mm,fill=gray!30] (0,0) --  (220:4)  arc(220:120:4) -- cycle;         
        \end{tikzpicture}
        \caption{} \label{A4} }
\end{figure}

\end{enumerate}
\end{document}
3
  • 1
    Yeah, I think you should really use \sections, and then simply \numberwithin{figure}{section}. Unrelated: I seriously doubt you are really using latin1 encoding.
    – campa
    Feb 20, 2021 at 13:47
  • Do you every use figures outside questions? Feb 20, 2021 at 14:22
  • I thought i had removed all unnecessary packages!! In answer to your question John - No, not when I am writing exam papers.
    – ToyBoy
    Feb 20, 2021 at 14:41

1 Answer 1

0

You can also redefine \thefigure at will. They are local to groups (environments), but \item is not a group. You could create a question environment (like exam class) which calls \item for you.

\documentclass[12pt,a4paper]{article}
\usepackage[left=12mm, top=12mm, right=12mm, bottom=12mm]{geometry}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{enumerate}
\usepackage[shortlabels]{enumitem}
\usepackage[figurename=Fig.]{caption}
\usepackage{float}
\usepackage{pgfplots}
\usepackage{tkz-euclide}

\begin{document}
\begin{enumerate}
\renewcommand{\thefigure}{\theenumi}% reverts after \end{enumerate}
\item Question 1
\item Fig.\ref{A1} shows triangle \textit{ABC}.
\begin{figure}[H]
    \centering{\begin{tikzpicture}[scale = 0.4]
            \draw [line width = 0.5mm] (0,0) coordinate(A) node[left]{A}--++(0:3) coordinate(B) node[right]{B}--++(64:4) coordinate(C) node[above]{C}--cycle;
        
        \end{tikzpicture}
        \caption{} \label{A1} }
\end{figure}


\item Fig.\ref{A2} shows a sector of a circle of radius 8 cm.\\[2mm]
The angle of the sector is 2.1 radians.
\begin{figure}[H]
  \renewcommand{\thefigure}{\theenumi.A}% reverts after figure
    \centering{         \begin{tikzpicture}[scale=0.4]
            \draw [line width = 0.5mm,fill=gray!30] (0,0) --  (220:4)  arc(220:120:4) -- cycle ;
        
        \end{tikzpicture}
        \caption{} \label{A2} }
\end{figure}

Fig.\ref{A3} shows a sector of a circle of radius 8 cm.\\[2mm]
The angle of the sector is 2.1 radians.
\begin{figure}[H]
  \renewcommand{\thefigure}{\theenumi.B}% reverts after figure
    \centering{         \begin{tikzpicture}[scale=0.4]
            \draw [line width = 0.5mm,fill=gray!30] (0,0) --  (220:4)  arc(220:120:4) -- cycle;         
        \end{tikzpicture}
        \caption{} \label{A3} }
\end{figure}

\item Another Question. Fig \ref{A4} 
\begin{figure}[H]
    \centering{ \begin{tikzpicture}[scale=0.4]
            \draw [line width = 0.5mm,fill=gray!30] (0,0) --  (220:4)  arc(220:120:4) -- cycle;         
        \end{tikzpicture}
        \caption{} \label{A4} }
\end{figure}

\end{enumerate}
\end{document}
1
  • That is great - works exactly as I wanted and much easier than I had anticipated. I hadn't seen '\thefigure' and '\theenumi' as ways to reference then relevant numbers before.
    – ToyBoy
    Feb 20, 2021 at 14:57

You must log in to answer this question.

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