1

I want to label subfigures, across many figures, with sequential positive integers.

For example, the first figure may have two subfigures with label tags {fig1:subfig1} and {fig1:subfig2} and the second figure may have three subfigures with label tags {fig2:subfig1}, {fig2:subfig2}, and {fig2:subfig3}.

Then, when referencing these labels,

{fig1:subfig1}
{fig1:subfig2}
{fig2:subfig1}
{fig2:subfig2}
{fig2:subfig3}

would display as

1
2
3
4
5.

How can I do this using the subfigure package?

I didn't know how to use the subfig package yet. In the meantime, there is a MWE using the subfigure package.

\documentclass[12pt]{article}
\usepackage{subfigure}
\begin{document}
\begin{figure}[ht]
 \subfigure[subfigure A]{This is a figure. \label{label1}}
 \subfigure[subfigure B]{This is a figure. \label{label2}}
\end{figure}
\begin{figure}[ht]
 \subfigure[subfigure A]{This is a figure. \label{label3}}
 \subfigure[subfigure B]{This is a figure. \label{label4}}
 \subfigure[subfigure C]{This is a figure. \label{label5}}
\end{figure}

Tesing referencing labels: \ref{label1} \ref{label2} \ref{label3} \ref{label4}

Desired result: 1 2 3 4 5
\end{document}
10
  • Welcome to TeX.sx! What package are you using for the sub-figures? A minimal working example (MWE) is always appreciated. Commented Jun 27, 2011 at 15:36
  • Please add to your question a minimal working example illustrating how you are building your subfigures. Commented Jun 27, 2011 at 15:37
  • I am not sure how to use the subfig package. Every example I found is not a "minimal working example". Can you point me to one? Commented Jun 27, 2011 at 16:37
  • @Martin Scharrer @Gonzalo Medina is there a MWE of the subfig package? Commented Jun 27, 2011 at 19:02
  • @Tyson: Your above MWE does not include main captions. Are you sure you need subfigures? Or do you simply want to place two or three figures side-by-side? In the latter case simple use minipages which hold the figure content and a \caption. There can be multiple figures with captions in one figure environment. Commented Jun 27, 2011 at 19:39

4 Answers 4

2

You need to disconnect the subfigure counter from the figure counter. This can be best done using the chngcntr package. It provides the \counterwithout{<counter>}{<parent counter>} macro for this and disconnects the <counter> from the original <parent counter>.

For the subfigure package you can use:

\usepackage{subfigure}% Actually replaced by `subfig`!
\usepackage{chngcntr}
\counterwithout{subfigure}{figure}

Update:

The above seems to work only for the sub-caption numbers but not for the references. You could add an own counter which is increased using \refstepcounter which makes it the target of the following \label.

The following code works for me. I hope it is what you want.

\documentclass[12pt]{article}
\usepackage{subfigure}

\newcounter{mysubfig}
\newcommand{\mysubfig}[2][]{%
    \subfigure[#1]{#2}%
    \refstepcounter{mysubfig}%
}

\usepackage{chngcntr}
\counterwithout{subfigure}{figure}
\renewcommand{\thesubfigure}{(\arabic{subfigure})}

\begin{document}
\begin{figure}[ht]
 \mysubfig[mysubfig A]{This is a figure.}\label{label1}
 \mysubfig[mysubfig B]{This is a figure.}\label{label2}
 \caption{Main caption}\label{main1}
\end{figure}
\begin{figure}[ht]
 \mysubfig[mysubfig A]{This is a figure.}\label{label3}
 \mysubfig[mysubfig B]{This is a figure.}\label{label4}
 \mysubfig[mysubfig C]{This is a figure.}\label{label5}
 \caption{Main caption}\label{main2}
\end{figure}

Main figure still work? \ref{main1} \ref{main2}

Tesing referencing labels: \ref{label1} \ref{label2} \ref{label3} \ref{label4} \ref{label5}

Desired result: 1 2 3 4 5
\end{document}

Result:

Image

12
  • Great. I updated the question with the package I am using to create subfigures. Commented Jun 27, 2011 at 15:47
  • @Tyson: About subfigure in general, see ftp.heanet.ie/pub/CTAN/tex/help/Catalogue/entries/…: "This package is now obsolescent: new users should use subfig instead." Commented Jun 27, 2011 at 15:53
  • @Tyson: See my update. Commented Jun 27, 2011 at 16:19
  • @Martin Scharrer This solution didn't work. The two subfigures in the first figure are correctly numbered 1 and 2, but the next subfigure in the next figured is numbered 23. Commented Jun 27, 2011 at 19:01
  • @Tyson: Without a MWE I couldn't test it myself. I will have a look on it. Commented Jun 27, 2011 at 19:09
1

The caption package provides \ContinuedFloat also for this purpose:

\documentclass[a4paper]{book}
\usepackage{subfig}
\usepackage{caption}
\captionsetup[subfloat]{listofformat=subsimple}
\renewcommand{\thesubfigure}{\arabic{subfigure}}
\usepackage[demo]{graphicx}
\begin{document}
text

\begin{figure}[htp]
\centering
\subfloat[first]{\includegraphics[width=3cm]{x}\label{f}}
\qquad
\subfloat[second]{\includegraphics[width=4cm]{x}\label{s}}
\caption{A figure}\label{full}
\end{figure}
\begin{figure}[htp]\ContinuedFloat
\centering
\subfloat[third]{\includegraphics[width=6cm]{x}\label{t}}
\caption{A figure (continued)}
\end{figure}

\subref{f}
\subref{s}
\subref{t}
\end{document}

enter image description here

7
  • @egreg Your solution does not work. See the following: \documentclass[a4paper]{book} \usepackage{subfig} \usepackage{caption} \begin{document} \begin{figure} \centering \subfloat[first]{This is a figure. \label{label1}} \qquad \subfloat[second]{This is a figure. \label{label2}} \caption{A figure} \end{figure} \begin{figure}\ContinuedFloat \centering \subfloat[third]{This is a figure. \label{label3}} \caption{A figure (continued)} \end{figure} Tesing referencing labels: \ref{label1} \ref{label2} \ref{label3} Desired result: 1 2 3 \end{document} Commented Jun 27, 2011 at 19:39
  • 1
    Yes, it works. Of course you have to modify the way the subfigure counter is represented and use \subref, not \ref. See edit.
    – egreg
    Commented Jun 27, 2011 at 19:58
  • @egreg When I compile your example, my last line is a b c, not 1 2 3. Commented Jun 27, 2011 at 20:11
  • I've forgotten the \renewcommand\thesubfigure{\arabic{subfigure}} line, sorry.
    – egreg
    Commented Jun 27, 2011 at 20:31
  • 1
    So you want (a) in the subcaption and 1 in the reference? That's absurd.
    – egreg
    Commented Jun 27, 2011 at 21:05
1

This answer is based on Martin Scharrer's but used the subfig package (as he and others recommended).

I have (eventually) figured out how to use the subfig package and I really like the solution it provides for this problem. Because myCounter is not related to the figure or subfigure counters, everything else works (and thus looks) "normal".

\documentclass[12pt]{article}

\usepackage{subfig}
\newcounter{myCounter}
\newcommand{\mySubfloat}[2][]{\subfloat[#1]{#2}\refstepcounter{myCounter}}

\begin{document}
\begin{figure}[ht]
 \mySubfloat[mysubfig A]{This is a figure.}\label{label1}
 \mySubfloat[mysubfig B]{This is a figure.}\label{label2}
 \caption{Main caption}\label{main1}
\end{figure}
\begin{figure}[ht]
 \mySubfloat[mysubfig A]{This is a figure.}\label{label3}
 \mySubfloat[mysubfig B]{This is a figure.}\label{label4}
 \mySubfloat[mysubfig C]{This is a figure.}\label{label5}
 \caption{Main caption}\label{main2}
\end{figure}

Main figure still work? \ref{main1} \ref{main2}

Tesing referencing labels: \ref{label1} \ref{label2} \ref{label3} \ref{label4} \ref{label5}

Desired result: 1 2 3 4 5
\end{document}

enter image description here

1

Not that the memoir class was specified, but in case it's useful to others, here's a MWE for multi-page figures with subfigures in memoir (I can't imagine another reason to not reset subfigure numbering when we get a new figure):

\documentclass[10pt,oneside]{memoir}

\newsubfloat{figure}

\begin{document}
\chapter{A Chapter}
The following figure (if it had real content) would be large
enough to require multiple pages. It is made of several subfigures,
labeled (a), (b), and (c). Though we have multiple figure environments,
the \verb|contsubbottom| command treats them as part of one logical
figure.
\begin{figure}
    \centering
    \subbottom[First Subfigure Caption]{
        \framebox[0.9\textwidth]{Here's the content of a subfigure.}
        \label{fig:ve_uw}
    }
    \\
    \subbottom[Second Subfigure Caption]{
        \framebox[0.9\textwidth]{Here's the content of a subfigure.}
        \label{fig:ve_iw}
    }
    \caption{First Figure Caption}
\end{figure}
%
\clearpage
%
\begin{figure}
    \centering
    \contsubbottom[Third Subfigure Caption]{
        \framebox[0.9\textwidth]{Here's the content of a subfigure.}
        \label{fig:ve_eiw}
    }
    \contcaption{Continued}
    \label{fig:ve}
\end{figure}
Since the previous figure has a subfigure on the top of this page,
we'll place this second figure anywhere *but* the top of the page.
\begin{figure}[hbp]
\centering
\framebox[0.9\textwidth]{Here's the content of another figure.}
\caption{Second Figure Caption}
\label{fig:second}
\end{figure}

\end{document}

Result:

enter image description here

3
  • This is not what I am asking. Commented Jun 27, 2011 at 18:58
  • is it possible to modify your answer to get the behavior I am looking for? Commented Jun 27, 2011 at 20:12
  • 1
    I'd say stick with Martin's version for now. The behavior you're looking for, as you had indicated, is very non-standard. If his reference implementation works in a MWE, but doesn't in your final document, then the real question is to figure out what other package or code is causing the difference. Starting with the MWE, start adding packages from your non-working document until the MWE stops working. That will narrow the problem down. Commented Jun 27, 2011 at 21:39

You must log in to answer this question.

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