0

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}
2

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}
  • 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
0

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}}
\csvreader[longtable={|C{3cm}|C{3cm}|C{3cm}|C{4cm}|},
% **** table setup ****
  table head=\caption{\tablecaption}
  \label{\tablelabel} \\\hline \endfirsthead
  \caption*{\raggedleft\hfill Table~\thetable continues. \newline \centering \tablecaption}
  \\\hline \mysecondline \\\hline \endhead,
  late after line={\\\hline}, table foot={}, separator=semicolon,
]{\tablefile}{}{\csvlinetotablerow}

When I want to create a new table, I only need to edit the five lines on top (I decided it's not worth wrapping to a macro).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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