My CSV file has two headers in the first two rows (first row has static keywords, second row is the header to display) and I am displaying it using csvreader. I want to use the second row header as a header row of the table to display. The first page of generated table always has that header, but I also want it to be repeated on the second page of my longtable.

Code:

\def\tablecaption{Table caption}
longtable={|c|c|c|},
\label{tab:mytable}
\caption*{Table~\thetable Continued \newline \tablecaption}
\\\hline
%%%%%%%%%% I want to repeat the second row from csv file here %%%%%%%%%%
\\\hline
late after line={\\\hline},
table foot={},
separator=tab,
]
{mytable.csv}
{}
{\csvlinetotablerow}


You can process the csv file with a first reader to store the expanded second line into a macro \mysecondline. The second reader (table) can use this for the repeated header.

\documentclass[12pt]{article}
\usepackage{csvsimple,filecontents,longtable}

\begin{filecontents*}{mytable.csv}
staticA,staticB,staticC
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
A,B,C
a,b,c
1,2,3
\end{filecontents*}

\begin{document}

filter test=\ifnumequal{\thecsvinputline}{2},
]
{mytable.csv}
{}
{\edef\mysecondline{\csvcoli & \csvcolii & \csvcoliii}}

\def\tablecaption{Table caption}
longtable={|c|c|c|},
\label{tab:mytable}
\caption*{Table~\thetable: Continued \tablecaption}
\\\hline
%%%%%%%%%% I want to repeat the second row from csv file here %%%%%%%%%%
\mysecondline
\\\hline
late after line={\\\hline},
table foot={},
%separator=tab,
]
{mytable.csv}
{}
{\csvlinetotablerow}

\end{document}

• Thanks! Can I do that with \csvlinetotablerow instead of \csvcoli & \csvcolii & \csvcoliii? I'm getting a TeX capacity exceeded, sorry [input stack size=5000]. [...\edef\tableheaderrow{\csvlinetotablerow}}] error... – nagimov Aug 4 '16 at 4:22
• @nagimov No, I do not think so, because \csvlinetotablerow is somewhat too special to use it like that. – Thomas F. Sturm Aug 4 '16 at 8:25

So based on Thomas' answer I ended up with the following snippet:

\def\tablecaption{caption goes here}
\def\tablefile{csv file name goes here.csv}
\def\tablelabel{tab:label-goes-here}
\csvreader[separator=semicolon,filter test=\ifnumequal{\thecsvinputline}{2}]{\tablefile}{}{\edef\mysecondline{\csvcoli & \csvcolii & \csvcoliii & \csvcoliv}}