I am making a table with two subtables, each subtable has its own tablenotes so I am using threeparttable. But I am prompted with the following error:
! Package caption Error: table inside subtable.
See the caption package documentation for explanation.
Type H for immediate help.
...
l.7 \begin{threeparttable}
If you do not understand this error, please take a closer look
at the documentation of the `caption' package, especially the
section about errors.
A small example of my code here. It gives the error above. I searched around and it seems threeparttable
should be able to work with subcaption
, at least in my simple example here. Could any of you guys ever had issues alike and can point me a way? thanks.
\documentclass{article}
\usepackage{threeparttable}
\usepackage{subcaption}
\begin{document}
\begin{table}
\begin{subtable}{\textwidth}
\begin{threeparttable}
\begin{tabular}{ccc}
test1 &test1 &test1
\end{tabular}
\end{threeparttable}
\end{subtable}
\end{table}
\end{document}
Update 1
Thanks to @cfr's answer, if we put threeparttable
outside subtable
, the compile error would be gone. But we have new problems here. Let's first make clear what we want:
- we want one
table
, which means it has one main caption (Tab 1
). - Inside this top level
table
, we want two subtables, either these subtables are realized by the tagsubtable
or not doesn't matter. - For each subtable, it has its own caption (
Tab a
andTab b
) and table notes.
So now we have the following problem:
- If we put captions for subtable between
threeparttable
andsubtable
(code example as below), the caption for subtable will be using the top level caption counter, we end up withTab 1
,Tab 2
andTab 3
.
\documentclass{article}
\usepackage{threeparttable}
\usepackage{subcaption}
\begin{document}
\begin{table}
\centering
\caption{top level caption}
\begin{threeparttable}
\centering
\caption{sub level caption 1}
\begin{subtable}{\textwidth}
\begin{tabular}{|c|c|c|}
\hline
test1 &test1 &test1\\
\hline
\end{tabular}
\end{subtable}
\end{threeparttable}
\begin{threeparttable}
\centering
\caption{sub level caption 2}
\begin{subtable}{\textwidth}
\begin{tabular}{|c|c|c|}
\hline
test2 &test2 &test2\\
\hline
\end{tabular}
\end{subtable}
\end{threeparttable}
\end{table}
\end{document}
- if we put
caption
inside thesubtable
environment, the are separated by thethreeparttable
and will give us twoTab a
. Code example and pic below.
\documentclass{article}
\usepackage{threeparttable}
\usepackage{subcaption}
\begin{document}
\begin{table}
\centering
\caption{top level caption}
\begin{threeparttable}
\centering
\begin{subtable}{\textwidth}
\caption{sub level caption 1}
\begin{tabular}{|c|c|c|}
\hline
test1 &test1 &test1\\
\hline
\end{tabular}
\end{subtable}
\end{threeparttable}
\begin{threeparttable}
\centering
\begin{subtable}{\textwidth}
\caption{sub level caption 2}
\begin{tabular}{|c|c|c|}
\hline
test2 &test2 &test2\\
\hline
\end{tabular}
\end{subtable}
\end{threeparttable}
\end{table}
\end{document}
Best Answer
The problem is that the
caption
package redefines thethreeparttable
environment to set the caption type totable
.caption
uses the caption type to check for inappropriate nesting. In this case, it therefore finds an attempt to set caption type totable
when it is within thesubtable
environment. It knows that isn't allowed, so it gives an error.The simplest solution, especially if some of your
threeparttable
s are not of typesubtable
, is to use the fact that it works if you put thesubtable
inside thethreeparttable
. Alternatively, you can just set the caption type within thethreeparttable
environment rather than using thesubtable
environment at all.However, as your update points out, this sadly restarts the counter for each sub-table. Here is a solution which avoids this difficulty.
Note that the idea here is that you could change the options for
threepartsubtable
specifically, just as you could change them forthreeparttable
. I think there must be a way to do this more elegantly, but have not yet found one.That is, I have precious little clue ...
Caveat emptor.
[Edited in light of comments, although not really in accordance with them.]