11

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
1   Ady 53  F
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,
header=true,    
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,
header=true,                    
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
10

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
1   Ady 53  F
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
1   Ady 53  F
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
1   Ady 53  F
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,
header=true,    
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
every head row/.style={before row=\toprule, after row=\midrule\endhead}, 
every last row/.style={after row=\bottomrule}
]{testdata.dat}

\end{document}
  • I substituted an imported file for the 'inline file', and your solution works just fine! – zun Jan 8 '12 at 22:45
  • Without an enclosing \begin{tabular} ... \end{tabular} in case of multi-page tables, where can I put the caption and label command in this code? – Christoph90 Mar 20 '16 at 16:48
  • 1
    @Christoph90: You can put it in the before row key like so: every head row/.style={before row={\caption{My Beautiful Table}\label{tab:mytable}\\\toprule} – Jake Mar 20 '16 at 17:01
  • Thanks! That did work! I went into some more problems with this and therefore posted a new question link – Christoph90 Mar 20 '16 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.