13

I would like to use the pgfplotstable package in order to import data from a .csv file into a multi-page table.

I would like for the table to have:

  1. a caption above it and
  2. its header to repeat at the top of every page

It seems that I'm only able to have one or the other. I can import a table and have a caption or I can import a table and have the header repeat on every page. I can't get both to work at the same time. Here is my MWE, where data.csv is a 2 column csv file with enough data to fill multiple pages:

\documentclass{minimal}

\usepackage{pgfplotstable}
\usepackage{longtable}
\begin{document}

\pgfplotstabletypeset[
    begin table=\begin{longtable},
    every head row/.style={before row={%
    \caption{This is a Table with Data}%
    \label{tab:DataTable}    
    \endfirsthead
    \hline
    }, after row=\hline},
  every nth row={1}{before row=\hline},
    every last row/.style={before row=\hline, after row=\hline},
    col sep=comma,
    string type,
    columns/A/.style={column name=\textbf{Column 1}, column type={|c}},
    columns/B/.style={column name=\textbf{Column 2}, column type={|c}},
    end table=\end{longtable}
    ]{data.csv}

\end{document}
0

3 Answers 3

14

For some reasons, if you want to keep using pgfplotstable, this is the solution. Otherwise you may adopt @David's answer above. I have given some dummy data in the file.

\documentclass{article}

\usepackage{pgfplotstable}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{array}
\usepackage[a4paper,top=4in,bottom=4in,right=1in,left=1in]{geometry} %%% change the margins in your file suitably.
\usepackage{filecontents}
\begin{filecontents}{data.csv}
column1,column2
5001,102
5002,75
5003,115
5004,45
5005,97
5036,110
5037,77
5038,147
5039,89
5040,62
5041,160
5042,102
5043,56
5044,86
5100,74
5101,65
5102,131
5103,90
5104,99
\end{filecontents}%

\begin{document}

%%% Code from Dr. Christian ------ for not using headers.----------------------
\pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{
    % upcoming releases offer this more convenient option:
    \pgfplotstableset{
        empty header/.style={
          every head row/.style={output empty row},
        }
    }
}{
    % versions up to and including 1.5.1 need this:
    \pgfplotstableset{
        empty header/.style={
            typeset cell/.append code={%
                \ifnum\pgfplotstablerow=-1 %
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
                \fi
            }
        }
    }
}
%%%-----------------------------------------------

\pgfplotstabletypeset[
    empty header,
    begin table=\begin{longtable},
    every first row/.append style={before row={%
    \caption{This is a Table with Data}%
    \label{tab:DataTable}\\\toprule
    \textbf{column 1} &\textbf{column 2} \\ \toprule    
    \endfirsthead
    %
    \multicolumn{2}{c}%
    {{\bfseries Table \thetable\ Continued from previous page}} \\
    \toprule 
    %
    \textbf{column 1} &\textbf{column 2} \\ \toprule  
    \endhead
    %
    \midrule \multicolumn{2}{r}{{Continued on next page}} \\ \bottomrule
    \endfoot
    %
    \midrule
    \multicolumn{2}{r}{{Concluded}} \\ \bottomrule
    \endlastfoot
    }},%
    %
    end table=\end{longtable},
    col sep=comma,
    string type,
    ]{data.csv}

\end{document}

enter image description here

I used booktabs for lines and avoided putting a line after each row as it improves readability.

2

I think the package is making it seem more complicated than it need be.

I'd just input the csv file into a longtable

enter image description here


1,2
3,4
5,6
7,8
9,10
11,12

\documentclass{article}

\textheight5\baselineskip
\usepackage{longtable}
\begin{document}

\begingroup
\obeylines%
\catcode`\,=4 %
\def^^M{\\}%
\makeatletter%
\begin{longtable}{rr}%
\caption{a table of data}
\textbf{Column 1}&\textbf{Column 2}\endfirsthead%
\textbf{Column 1}&\textbf{Column 2}\endhead%
\@@input data.csv %
\end{longtable}%
\endgroup%

\end{document}
1

EDIT: This will produce the result that you want. The only caveat being that the second row in the file sometable.dat has to be manually entered, which forces the string type style of all the columns.

\documentclass{article}

\usepackage{pgfplotstable}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{array}
\usepackage{siunitx}
\usepackage{filecontents}
\begin{filecontents}{sometable.dat}
temperature value
Temperature(\si{\kelvin}) Value(\si{m^3})
15.00 1.000342E+00
16.00 1.000038E+00
18.00 9.991696E-01
20.00 9.976573E-02
22.00 9.954760E-02
24.00 9.926016E-02
25.00 9.908970E-02
26.00 9.890101E-02
27.00 9.869378E-03
28.00 9.846772E-03
29.00 9.822252E-03
30.00 9.793544E-03
31.00 9.757798E-04
32.00 9.714558E-04
33.00 9.663367E-04
34.00 9.603773E-04
35.00 9.535319E-04
36.00 9.457552E-04
37.00 9.370016E-04
38.00 9.272257E-05
39.00 9.164286E-05
40.00 9.053571E-05
42.00 8.832143E-05
44.00 8.610714E-05
45.00 8.500000E-05
46.00 8.389286E-05
48.00 8.167857E-05
50.00 7.946429E-05
55.00 7.392857E-05
60.00 6.839286E-05
65.00 6.285714E-02
70.00 5.730197E-02
75.00 5.095251E-02
80.00 4.558582E-02
85.00 4.192582E-02
90.00 3.825244E-02
95.00 3.396510E-02
98.15 3.151784E-02
100.00 3.035714E-02
103.15 2.861339E-02
113.15 2.307768E-02
123.15 1.767093E-02
125.00 1.680902E-02
138.15 1.222368E-02
150.00 8.928571E-03
153.15 8.056696E-04
173.15 4.209043E-03
175.00 3.996861E-04
198.15 1.887255E-04
200.00 1.785714E-04
223.15 6.901849E-04
225.00 5.786391E-04
\end{filecontents}
\begin{document}
    \pgfplotstableset{
        begin table=\begin{longtable},
        end table=\end{longtable},
    }
    \pgfplotstabletypeset[
        columns/temperature/.style={
            string type,
            column name=Temperature(K)
        },
        columns/value/.style={
            string type,
            column name=Value($m^3$)
        },
        every head row/.append style={before row={\caption{Some caption}\label{tab:sometable}\\\toprule},after row=\midrule\endfirsthead},
        every first row/.append style={before row={\multicolumn{2}{c}{}\\ \caption[]{Some caption}\\\toprule},after row=\midrule\endhead},
        every last row/.style={after row=\bottomrule},
    ]{sometable.dat}

    Refer to the table here \ref{tab:sometable}.
\end{document}

Page 1 Page 2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .