[Tex/LaTex] pgfplotstable; longtable with caption and repeating header

longtablepgfplotstabletables

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}

Best Answer

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.

Related Question