[Tex/LaTex] Duplicate caption and table header with csvsimple and csvreader

csvcsvsimplelongtable

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}
\csvreader[
longtable={|c|c|c|},
table head=\caption{\tablecaption}
\label{tab:mytable}
\\\hline \endfirsthead
\caption*{Table~\thetable Continued \newline \tablecaption}
\\\hline
%%%%%%%%%% I want to repeat the second row from csv file here %%%%%%%%%%
\\\hline
\endhead,
late after line={\\\hline},
table foot={},
separator=tab,
]
{mytable.csv}
{}
{\csvlinetotablerow}

Best Answer

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
headA,headB,headC
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}

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


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

\end{document}
Related Question