[Tex/LaTex] threeparttable not working with subcaption(subtable), error: table inside subtable

subcaptiontablesthreeparttable

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:

  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.
    enter image description here

\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.
    enter image description here

\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 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}

<code>threepartsubtable</code>