5

I have some problem with the counter of my figures. I added several figures, some of them with captions and some of them without captions. I want those figures with captions to be counted, the others should not increase the counter. Somehow it sometimes work, sometimes it does not.

For example, apparently the following figure is being counted (since when I erase it, the following figures' counters are decreased by one):

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{amsthm}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{chngcntr}
\usepackage{framed}
\usepackage{tikz}
\usepackage{geometry}
\usetikzlibrary{fit,matrix}
\counterwithin{figure}{section}

\begin{document}

\begin{figure}[h]
\begin{subfigure}[h]{0.3\textwidth}
\centering
\(u=\)
\tikz[baseline=(M.west)]{%
\node[matrix of math nodes,matrix anchor=west, ampersand replacement=\&]     (M) {%
~3 \& 0 \&~ 0 \&~ 0\\
~-3 \& 0 \&~ 0 \&~ 0\\
~0 \& 0 \&~ 0 \&~ 0\\
};
\node[draw,fit=(M-1-1)(M-3-4),inner sep=-1pt] {};
},
\end{subfigure}%
~
\begin{subfigure}[h]{0.215\textwidth}
\centering
\(v=\)
\tikz[baseline=(M.west)]{%
\node[matrix of math nodes,matrix anchor=west, ampersand replacement=\&] (M) {%
~3 \& 0 \&~ 0 \&~ 0\\
~0 \& 0 \&~ 0 \&~ 0\\
~-3 \& 0 \&~ 0 \&~ 0\\
};
\node[draw,fit=(M-1-1)(M-3-4),inner sep=-1pt] {};
}
\end{subfigure}
\end{figure}

\end{document}

I guess the figure looks weird to some of you, I am not very good at writing Latex documents. But the main problem is the numbering for now.

Does the counter depend on the size of the figure?

Can I manually tell the program to skip counting a specific figure?

lattice
  • 151
  • 1
    As far as I know, they are only counted when \caption is used (the unstarred version) –  Jul 05 '15 at 17:34
  • Apparently it also counts several figures without caption. My first figure with caption is counted correctly, as Figure 3.1 (I count them within sections). The next figure with caption should be Figure 3.2, but the caption appears as "Figure 3.3:...". But it is even more strange to me that when I reference this figure, the reference appears as Figure 3.5. The code basically looks like this: \begin{figure} ... \caption{...} \end{figure}\label{figure3.1} ... \begin{figure} ... \end{figure} ... \begin{figure} ... \caption{...} \end{figure}\label{figure3.2} – lattice Jul 05 '15 at 17:43
  • 1
    \label should be after \caption inside the figure environment. – Werner Jul 05 '15 at 17:48
  • Okay, I placed all labels after the \caption. Now the references work correctly, but the figure numbering has not changed. – lattice Jul 05 '15 at 17:59
  • Omitting the \caption should not increase the counter. We need to know what document class you are using. – egreg Jul 05 '15 at 18:06
  • figure (unless you have redefined it) does not use the counter at all, only \caption increments the counter. – David Carlisle Jul 05 '15 at 18:07
  • I am using the article document class. @David Carlisle Well at least in my document it does. I have not redefined the figures, I have only set \counterwithin{figure}{section} at the beginning of the document. – lattice Jul 05 '15 at 19:06
  • I have added the code of one figure that appears to increase the counter (see question). – lattice Jul 05 '15 at 19:12
  • article class figures do not increment any counter. Please always post a complete small document that shows the problem. Oh but you (now) show subfigure which redefines the numbering scheme so that changes the problem completely. But the example can not be run please compete it with \documentclass \end{document} etc and check that the code as posted produces bad numbering. – David Carlisle Jul 05 '15 at 19:14
  • Okay, I have completed the code. As mentioned already, when erasing this figure (including the subfigures), the following figures' counters are decreased by one. Thus, the figure is obviously counted in some way. If the subfigure redefines the numbering, how can I avoid that? I know that I can use some command like \setcounter{figure}{number} but I do not really like this method. – lattice Jul 05 '15 at 19:30

3 Answers3

4

It's a feature of the subcaption package. When subfigure is used, the figure counter is stepped independently on whether \caption is used. More precisely, this is done if the caption is expected to be at the bottom, which is usual for figures. The reason is simple: the caption to a subfigure needs to know the number of the main figure caption, which however would only be generated later.

In this case, the \caption command will adjust things so that the counter is not stepped again.

I find it quite illogical to have a figure environment without a (main) caption, which is useful for the readers. It's also quite strange to have subfigures without subcaptions.

However, you don't need subfigure, unless you want to specify a subcaption. A subfigure environment is just a minipage under cover, plus the adjustments I mentioned before.

By the way, the [h] specifier is not a good one for subfigure: it accepts [t] or [b], like minipage, with the same meaning.

\documentclass{article}

\usepackage{subcaption}

\newcommand{\mockpicture}{\fbox{\rule{0pt}{1cm}\rule{2cm}{0pt}}}

\begin{document}

\begin{figure}[!htp]
\centering
NO CAPTION HERE

\medskip

\begin{minipage}{0.3\textwidth}
\centering
\mockpicture
\end{minipage}
\begin{minipage}{0.3\textwidth}
\centering
\mockpicture
\end{minipage}

\end{figure}

\begin{figure}[!htp]
\centering

NO CAPTION HERE

\medskip

\mockpicture

\end{figure}

\begin{figure}[!htp]
\centering

CAPTION HERE

\medskip

\mockpicture

\caption{A caption text}

\end{figure}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thanks a lot, it worked. By the way, I use the figure environment for visualizing certain vectors in an 3x4 array. I just felt like it was needed to add subfigure environments if I want to place several such arrays next to each other. Deleting this environment solved the numbering problem, and did not change the figure itself. – lattice Jul 05 '15 at 20:47
4

I had success with the following when I still wanted a caption but I didn't want it numbered:

\begin{figure}
\caption{A figure}
. . .
\end{figure}
. . .
\begin{figure}\ContinuedFloat
\caption{A non-numbered figure}
. . .
\end{figure}
jsta
  • 161
1

The following is from another question No caption number for figures and tables. Hopefully you can get rid of the caption together with the labeling.

\documentclass{article}

\usepackage{caption}

\usepackage{graphicx}

\begin{document}

\begin{figure}[h!]
\centering
\includegraphics[height=2cm]{figure}
\caption{Une figure.}
\end{figure}

\begin{figure}[h!]
\centering
\includegraphics[height=2cm]{figure1}
\captionsetup{labelformat=empty}
\caption{Une figure.}
\end{figure}

\end{document}
Dainy
  • 11
  • I tried that but then the figure was not displayed correctly. – lattice Jul 05 '15 at 17:53
  • 4
    The figure* environment has nothing to do with numbering of the figures. It creates a double column float in a two document layout. If the layout is single column, it is equivalent to figure. – egreg Jul 05 '15 at 18:07
  • 1
    I don't see how the part below the edit addresses the problem (and please don't recommend [h!] as that usually just generates a latex warning, you should always include t or better p) The part before edit should be deleted as figure* is not related to the question at all. – David Carlisle Jul 05 '15 at 18:33