# Memoir sub table numbering wrong

I am using sub tables and it seems because I but the table caption above I get the wrong number in the sub table references. How can I fix this?

I am pretty sure it is related to the package float package. I am very interested in both quick hacks to fix this specific problem with this combination and in a better set of tools that I can use the next time to avoid these sort of problem all together.

Here is what I am doing:

\documentclass[11pt,a4paper]{memoir}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{float}

\newsubfloat{table}

\begin{document}

\begin{table}[H]
\centering
\begin{minipage}{0.9\textwidth}
\caption{Multple tables\label{theTable}}
\begin{minipage}{0.5\textwidth}
\subcaption{A sub table\label{firstSubTable}}
\end{minipage}
\begin{minipage}{0.5\textwidth}
\subcaption{Second sub table\label{secondSubTable}}
\end{minipage}
\end{minipage}
\end{table}

Referencing the table (Table~\ref{theTable}) seems right but in referencing the
sub tables (Table~\ref{firstSubTable} and Table~\ref{secondSubTable}) they get
the wrong table number.

\end{document}


And how it looks:

-
The problem is in the fact that the package float is apparently not compatible with memoir. Don't use the [H] option: if a table must go here, then it's difficult to think it need captions. Leave it to float. –  egreg Dec 6 '12 at 18:14
Thing is, I want it there. And I want captions on everything. Looks kind of silly if every table has number and caption except the one that I used the [H] "hack" to place. How I made the thing be placed at a location hardly has anything to do with whether it has caption or not I would say... –  jonalv Dec 7 '12 at 9:13

In my opinion, you encountered a bug and you should properly report it. A workaround (stupid but working) is to add this at the end of your preamble:

\makeatletter
\let\X@old@caption\caption
\def\X@caption@br[#1]#2{\X@old@caption[#1]{#2}\X@caption@minusone}
\def\X@caption@nobr#1{\X@old@caption{#1}\X@caption@minusone}
\def\caption{\@ifnextchar[\X@caption@br\X@caption@nobr}
\makeatother


How does it work: Originally, \caption globally increases the counter by one. We, using the low-level macro \advance decrease is locally, which means that inside the float, it's unmodified, but outside, it will have the correct value (as you can check by adding a 2nd table). The rest are tweaks needed because \caption has an optional argument.

-
Can I ask what is wrong with the answer? The fact that I didn't notice that it's an incompatibility of packages makes the answer wrong? –  yo' Dec 12 '12 at 12:40

Float messes with \caption for some strange reason, this seems to do the trick

\let\stdcaption\caption
\usepackage{float}
\let\caption\stdcaption


basically we restore the memoir \caption after loading float. As already mentioned, I do not see the need for using the float package

-
Yes, that will do. But do the float package add anything that I couldn't do without it (in memoir)? –  Ipsen Dec 12 '12 at 12:38
Hm, maybe memoir has another way of doing what float does with the [H] setting? I find it a very handy way of putting figures where I want them, including inside shaded boxes. Of course it means a bit more manual work than floating images so I only use it when floating fails me. The alternative of faking the caption and figure numbering and stuff is just a pain to write... –  jonalv Dec 12 '12 at 12:46
I never use it. Usually then people want to use the [H] part, it is because they are using floats the wrong way, in other cases on can just keep it from creping into the following section via \FloatBlock. –  daleif Dec 12 '12 at 12:47
Also, I can't shake the feeling that float messes with \caption for a reason and that we actually want those changes... –  jonalv Dec 12 '12 at 12:48
Faking the caption? see \newfixedcaption –  daleif Dec 12 '12 at 12:50

I don't know if I understand the problem. Can't you just do it without the float package? If you simply comment out the float package it works.

\documentclass[11pt,a4paper]{memoir}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
% \usepackage{float}
\newsubfloat{table}
\begin{document}
\begin{table}[htp]
\caption{Multple tables\label{theTable}}
\begin{minipage}{.4\textwidth}
\centering
Table 1
\subcaption{A sub table\label{firstSubTable}}
\end{minipage}
\hfill
\begin{minipage}{.4\textwidth}
\centering
Table 2
\subcaption{Second sub table\label{secondSubTable}}
\end{minipage}
\end{table}
Referencing the table (Table~\ref{theTable}) seems right but in referencing the
sub tables (Table~\ref{firstSubTable} and Table~\ref{secondSubTable}) they get
the wrong right number.
\end{document}


-