# Pgfplotstableset, longtable and subcaption

I am using pgfplotstableset and longtable to read data from a file and format it into a table composed of several subtables. I have managed to create the table and to include a caption over it. However, I would also like to include a subcaption over each one of the subtables and I have not been able to do it.

The desired output should look like:

I have created the table above with caption*. However, I would prefer to use subcaption to maintain the consistency of my references. Below, I include the minimal code, which I have used to create the table above. I have tried replacing caption* with subcaption and I get the following error:

! Package caption Error: \subcaption outside float.


Minimal code. article.tex:

\documentclass{article}
\usepackage{conf-simple}
\begin{document}
\includetable{data/minimal-data.dat}{Minimal data}{minimal-data}
\end{document}


conf-simple.sty:

\ProvidesPackage{conf-simple}
\usepackage{subcaption}
\usepackage{pgfplotstable}
\usepackage{longtable}
\usepackage{booktabs}

\pgfplotstableset{
begin table=\begin{longtable},
create on use/double/.style={create col/expr={\thisrowno{1}*2}},
end table=\end{longtable},
}

\newcommand{\includetable}[3]{

\noindent\begin{minipage}{\textwidth}
\pgfplotstabletypeset[
col sep=space,
column type=r,
columns={0,1,double},      % display specified columns
columns/0/.style={string type, column name =},
columns/1/.style={column name=\small{Value}},
columns/double/.style={column name=\small{Double}},
% requires booktabs to place horiz rules
every head row/.style={before row={\caption{#2 \label{table:#3}} \\ \caption*{(a) First subtable} \\ \toprule
\multicolumn{3}{|c|}{Numbers} \\ \midrule}, after row=\midrule},
every last row/.style={after row=\bottomrule},
]{#1}

\end{minipage}

\noindent\begin{minipage}{\textwidth}
\pgfplotstabletypeset[col sep=space,
columns={0,1},      % display specified columns
columns/0/.style={string type,column name=},
columns/1/.style={column name=\small{Value}},
% requires booktabs to place horiz rules
every head row/.style={before row={\caption*{(b) Second subtable} \\ \toprule
\multicolumn{2}{|c|}{Numbers} \\ \midrule }, after row=\midrule},
every last row/.style={after row=\bottomrule},
]{#1}
\end{minipage}

}

\end{document}


Minimal data file:

One 1
Two 2
Three 3


As far as I understand, the problem is related to longtable not accepting subcaptions but I do not know how to build my table in a different way.

I have found related questions like this and this but they do not solve my issue.

• How is it possible that article.tex has an \end{table} but no \begin{table} (and does not have an \end{document})? Please consider cleaning up your code such that others can start playing with it. – user121799 Sep 4 '18 at 13:34
• @marmot I am sorry about the mistake, I have changed it. The rest of the code has been copied directly from the minimal test I ran so there should be no more errors. – fa__ Sep 4 '18 at 13:53

Only use \fixsubtable inside a minipage or other group.

\documentclass{article}
\usepackage{subcaption}
\usepackage{longtable}

\makeatletter
\newcommand{\fixsubtable}{\let\caption=\caption@caption
\def\@captype{table}}
\makeatother

\begin{document}

\begin{longtable}{@{}c@{}}
\caption{No Data}
\endfoot
\begin{minipage}{\textwidth}
\fixsubtable
\subcaption{First subtable}
\centering
\rule{0.25\textwidth}{0.25\textwidth}
\end{minipage}\\
\end{longtable}

\end{document}

• This does work if I place the caption and subcaption at the beginning of the minipage. However if I try to place \subcaption in the style before row, as I was doing with the caption I get the error ! Missing \endgroup inserted.. Your solution servers me right except for the fact that it increases the blank space between the subcaption and the subtable. – fa__ Sep 5 '18 at 7:28
• I just noticed the begin table=\begin{longtable},. There is no point in using longtable inside a minipage. If you have a subtable that needs to have pagebreaks, that is a more difficult problem. – John Kormylo Sep 5 '18 at 12:46
• I am using longtable and pgfplotstableset to process the data from a file in order to create a table with several subtables. I am using a minipage for every one of the subtables. That is, I need pagebreaks between the subtables, not in them. – fa__ Sep 5 '18 at 13:29
• So stop using begin table=\begin{longtable}, etc., which is causing every subtable to use longtable (and consequently disables \subcaption again). – John Kormylo Sep 5 '18 at 13:44