[Tex/LaTex] Usage of longtable (or equivalent) and pgfplotstabletypeset

longtablepgfplotspgfplotstabletikz-pgf

I'm a new user of TikZ pgfplots and pgfplotstable and have been stoked to discover how easy it is to go between Matlab (via matlab2tikz and writing to CSV) and LaTeX. I'm getting a little more ambitious now, and my question is, how do I properly implement a multi-page longtable together with pgfplotstableset inside a float (for placement, captioning, etc..)? So far I've been able to avoid this complication by splitting up these long lists into shorter subtables. I've also tried a couple of work-arounds to try to achieve the desired functionality, including adding captions to the end table alias as suggested in the pgfplots Manual or using the capt-of package to add a caption outside of a float. This works reasonably well but doesn't have the nice "Table ? continued" behavior of 'longtable'. Is there a better way to do this?

Here is a typical macro, the input arguments #1 are a CSV filename, and #2 a caption for the ToC/LoF:

\newrobustcmd{\strtable}[2]{
\pgfplotstableset{
begin table=\begin{longtable},
end table=\caption{#2}\end{longtable}
} % similar to manual for package pgfplotstable

\pgfplotstableset{columns/Channel/.style={string type}}
\pgfplotstableset{columns/Units/.style={string type}}
\pgfplotstableset{columns/Description/.style={string type,column type={p{6cm}}}}
\pgfplotstableread[col sep=comma]{#1}\strtable
%\tracingmacros=2\tracingcommands=2
\begin{center}
%\begin{longtable} % this bombs
%\captionof{table}{#2} % alternative way to try to get a caption in there using capt-of package
\pgfplotstabletypeset[
every first column/.style={string type,column type={@{}l|}},
    every even row/.style={before row={\rowcolor[gray]{0.9}}},
    every head row/.style={before row=\toprule,after row=\midrule\endhead}, % repeats header
    every last row/.style={after row=\bottomrule},
    ]\strtable
    % \end{longtable} % bombs
    \end{center}
    }

Best Answer

I experimented a while and found that

\pgfplotstableset{
begin table=\begin{longtable},
every first row/.append style={before row={%
    \caption{The caption}%
    \endfirsthead
    \multicolumn{3}{c}{{\bfseries \tablename\\
hetable{} -- continued from previous page}} \\
    \endhead
    }},
end table=\end{longtable}
} % similar to manual for package pgfplotstable

works well. Add debug to the option list to see the generated output listing in your log file. It seems as if the \endfirsthead and endhead macros collect the previously entered lines as templates and replicate them at the designated positions (compare the longtable manual for details).

The only special thing is to add these items to the correct place in pgfplotstable -- here, I used every first row/.append style={before row={}} as tool to inject the statements. Note that the \multicolumn statement needs the number of columns.

Related Question