Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 a caption above it and I would like for the table 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 minimal example, 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}
share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
add comment

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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