# threeparttable not working with subcaption(subtable), error: table inside subtable

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


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:

1. we want one table, which means it has one main caption (Tab 1).
2. Inside this top level table, we want two subtables, either these subtables are realized by the tag subtable or not doesn't matter.
3. For each subtable, it has its own caption (Tab a and Tab b) and table notes.

So now we have the following problem:

1. If we put captions for subtable between threeparttable and subtable(code example as below), the caption for subtable will be using the top level caption counter, we end up with Tab 1, Tab 2 and Tab 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}

1. if we put caption inside the subtable environment, the are separated by the threeparttable and will give us two Tab 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}

• Compiles fine here. Maybe delete your temporary files and try again? Dec 14, 2016 at 2:28
• Welcome! @MichaelPalmer Are you sure? If so, what distribution etc. do you have?
– cfr
Dec 14, 2016 at 2:34
• @cfr - texlive on debian jessie Dec 14, 2016 at 3:11
• @MichaelPalmer Presumably not current then ... You have the error to look forwards to ;).
– cfr
Dec 14, 2016 at 3:50
• Please do not edit questions to include answers. It is not how this site is supposed to work and it makes the answers pointless. You can always answer your own question if none of the answers are suitable (or there are no answers and you meantime find a solution).
– cfr
Dec 16, 2016 at 1:43

The problem is that the caption package redefines the threeparttable environment to set the caption type to table. caption uses the caption type to check for inappropriate nesting. In this case, it therefore finds an attempt to set caption type to table when it is within the subtable environment. It knows that isn't allowed, so it gives an error.

The simplest solution, especially if some of your threeparttables are not of type subtable, is to use the fact that it works if you put the subtable inside the threeparttable. Alternatively, you can just set the caption type within the threeparttable environment rather than using the subtable 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 for threeparttable. 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.]

\documentclass{article}
\usepackage{threeparttable}
\usepackage{subcaption}
\makeatletter
\newcommand\threepart@subtable{% based on code modified from caption.sty
\caption@setoptions{threepartsubtable}%
\caption@ORI@threeparttable
}
\newenvironment{threepartsubtable}{%
\threepart@subtable
}{%
\endthreeparttable
}
\makeatother
\captionsetup[threepartsubtable]{subtype}

\begin{document}
\begin{table}
\centering
\caption{Standard main caption}
\begin{subtable}{\textwidth}
\caption{Caption}
\begin{tabular}{cc}
a & b
\end{tabular}
\end{subtable}
\end{table}

\begin{table}
\centering
\caption{Main caption}
\begin{threepartsubtable}
\caption{Caption}
\begin{tabular}{ccc}
test 1 & test 1 & test 1
\end{tabular}
\end{threepartsubtable}

\begin{threepartsubtable}
\caption{Caption}
\begin{tabular}{ccc}
test 2  & test 2 & test 2
\end{tabular}
\end{threepartsubtable}

\begin{threepartsubtable}
\caption{Caption}
\begin{tabular}{ccc}
test 3 & test 3 & test 3
\end{tabular}
\end{threepartsubtable}

\begin{threepartsubtable}
\caption{Caption}
\begin{tabular}{ccc}
test 4  & test 4 & test 4
\end{tabular}
\end{threepartsubtable}
\end{table}
\begin{table}
\centering
\caption{Another main caption}
\begin{threepartsubtable}
\caption{Caption}
\begin{tabular}{ccc}
test 5 & test 5 & test 5
\end{tabular}
\end{threepartsubtable}

\begin{threepartsubtable}
\caption{Caption}
\begin{tabular}{ccc}
test 6  & test 6 & test 6
\end{tabular}
\end{threepartsubtable}
\end{table}

\end{document}


• Hi @cfr, thanks a lot for the explanation. Moving threeparttable out of subtable removes the complie error. However, now we have new problems about the captions, plz see my update.
– WTH
Dec 14, 2016 at 4:54
• @WTH Please see edited answer above. Do you have any idea why I need to reset subcaption's default settings for subtable here?
– cfr
Dec 14, 2016 at 23:21
• What is weird is that I don't need to reset them for them to be applied to explicit subtables, but only these. I suspect that I'm doing something wrong: there's something I don't get about how this works and how it is meant to work.
– cfr
Dec 14, 2016 at 23:25
• thanks the solution works fine. I looked at caption.sty and I think you are not using the right options, you should use \caption@setoptions{sub} instead of \caption@setoptions{threepartsubtable}. That's why later on you have to explicitly reset it to default options. See my update.
– WTH
Dec 15, 2016 at 11:33
• @WTH If you do that, is it possible to add options for threepartsubtable in the way that you can add options for threeparttable`? Anyway, please see amended version which is a bit simpler.
– cfr
Dec 16, 2016 at 1:41