# Import files with pgfplotstable and split tables across multiple pages

I am using pgfplotstable to import files with many rows and columns with which to create custom tables. Some tables are quite long, covering many pages. Is there a way to automatically assign the number of rows to display on a page? I could not find anything in the documentation.

My first attempts are included in the MWE which simply repeats code for each page required. There must be a better, more efficient way. Perhaps a Do While or For... syntax exists?

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{filecontents}

\begin{document}
\begin{filecontents}{testdata.dat}
Idx  Nam Dim Grd
2   Bar 72  C
3   Cor 83  B
4   Dar 58  D
5   Esa 68  C
6   Foo 67  C
7   Gar 74  C
8   Hur 65  D
9   Jaz 85  B
10  Ker 91  A
\end{filecontents}

{\large Table 1}

\pgfplotstabletypeset[col sep=space,
columns={Idx,Nam,Dim,Grd},      % display specified columns
columns/Idx/.style={fixed,fixed zerofill,precision=0,column type=r},
columns/Nam/.style={column type=l,string type},
columns/Dim/.style={fixed,fixed zerofill,precision=1,column type=r},
columns/Grd/.style={column type=l,string type},
skip rows between index={5}{83}, % {43}{83}
% requires booktabs to place horiz rules
every head row/.style={before row=\toprule, after row=\midrule},
every last row/.style={after row=\bottomrule}
]{testdata.dat}

\newpage
{\large Table 1 (cont.)}

\pgfplotstabletypeset[col sep=space,
columns={Idx,Nam,Dim,Grd},      % display specified columns
columns/Idx/.style={fixed,fixed zerofill,precision=0,column type=r},
columns/Nam/.style={column type=l,string type},
columns/Dim/.style={fixed,fixed zerofill,precision=1,column type=r},
columns/Grd/.style={column type=l,string type},
skip rows between index={0}{5}, % {0}{43}
% skip rows between index={10}{83},
every head row/.style={before row=\toprule, after row=\midrule},
every last row/.style={after row=\bottomrule}
]{testdata.dat}
\end{document}

-
In those cases you should switch to longtable package that allows tables on multiple pages –  Spike Jan 8 '12 at 8:16
@Zun There are two separate issues here: running the table over more than one page and picking particular rows to print. I'm not clear if you are happy to have the table 'split itself' or if you need to manually control which part of the table goes on which page. –  Joseph Wright Jan 8 '12 at 9:22
@Joseph Wright I would be happy to have the table 'split itself'. Manual control would be an added perk. I just downloaded a more recent version of pgfplotstable documentation which explicitly recommends a small number of rows (pagelength). So, I may be using the package in ways not intended. –  zun Jan 8 '12 at 9:57
@Spike I'll look more closely at longtable. If it imports files, it may be a more appropriate package. –  zun Jan 8 '12 at 9:59

You can use the longtable package with pgfplotstable by setting

\pgfplotstableset{
begin table=\begin{longtable},
end table=\end{longtable},
}


To repeat the header line, use every head row/.style={before row=\toprule, after row=\midrule\endhead} (the \endhead tells longtable that this is the end of the part that should be repeated at the top of each table part).

\documentclass[a5paper]{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{filecontents}
\usepackage{longtable}

\begin{document}
\begin{filecontents}{testdata.dat}
Idx  Nam Dim Grd
2   Bar 72  C
3   Cor 83  B
4   Dar 58  D
5   Esa 68  C
6   Foo 67  C
7   Gar 74  C
8   Hur 65  D
9   Jaz 85  B
10  Ker 91  A
2   Bar 72  C
3   Cor 83  B
4   Dar 58  D
5   Esa 68  C
6   Foo 67  C
7   Gar 74  C
8   Hur 65  D
9   Jaz 85  B
10  Ker 91  A
2   Bar 72  C
3   Cor 83  B
4   Dar 58  D
5   Esa 68  C
6   Foo 67  C
7   Gar 74  C
8   Hur 65  D
9   Jaz 85  B
10  Ker 91  A
\end{filecontents}

{\large Table 1}

\pgfplotstableset{
begin table=\begin{longtable},
end table=\end{longtable},
}

\pgfplotstabletypeset[col sep=space,
columns={Idx,Nam,Dim,Grd},      % display specified columns
columns/Idx/.style={fixed,fixed zerofill,precision=0,column type=r},
columns/Nam/.style={column type=l,string type},
columns/Dim/.style={fixed,fixed zerofill,precision=1,column type=r},
columns/Grd/.style={column type=l,string type},
% requires booktabs to place horiz rules