0

I want my figures to be captioned Figure x.n for the nth figure in chapter x. The below MWE gives correct figure numbers in the captions, but \ref gives the chapter numbers instead of x.y:

\documentclass{scrbook}

\usepackage{chngcntr}

\counterwithin{figure}{chapter}

\newcommand{\myfig}[3]
{
    \begin{figure}[ht]
        \label{#1}
        \centering
        %\input{figures/#2}
        \caption{#3}
    \end{figure}
}

\begin{document}

\chapter{Chapter One}

\myfig{figone}{foo}{This is the first figure.}
\myfig{figtwo}{foo}{This is the second figure.}

\chapter{Chapter Two}

\myfig{figthree}{foo}{This is the third figure.}

\chapter{Chapter Three}

See figures \ref{figtwo} and \ref{figone} and \ref{figthree}.

\end{document}

I expected Chapter Three to contain

See figures 1.2 and 1.1 and 2.1.

Instead it contains

See figures 1 and 1 and 2.

How do I reference figures to get x.n numbering?

  • 4
    \label must come after \caption. The issue is not related to the use of the chngcntr package. – Mico Jul 5 '17 at 16:31
  • 1
    See Understanding how references and labels work. Essentially a duplicate. – Alan Munn Jul 5 '17 at 17:53
  • You're referencing the section counter because that's the last counter being used in \refstepcounter and \label grabs this information in order to provide the reference value – user31729 Jul 5 '17 at 17:53