[Tex/LaTex] csvsimple and longtable (automatic headerline)

csvsimpledatatoollongtable

Helping a other communitymember (see using csvreader to read from csv giving invalid output) on a csvreading task I have tried to solve a problem, like the following…

\begin{filecontents*}{test-table.csv}
Name,Value,Text
Test 1,1,A
Test 2,2,B
Test 3,3,A
Test 4,4,B
Test 5,5,A
Test 6,6,B
Test 7,7,A
Test 8,8,B
Test 9,9,A
Test 10,10,B
Test 11,11,A
Test 12,12,B
Test 13,13,A
Test 14,14,B
Test 15,15,A
Test 16,16,B
Test 17,17,A
Test 18,18,B
Test 19,19,A
Test 21,21,B
Test 22,22,B
Test 23,23,B
Test 24,24,B
Test 25,25,B
Test 26,26,B
Test 27,27,B
Test 1,1,A
Test 2,2,B
Test 3,3,A
Test 4,4,B
Test 5,5,A
Test 6,6,B
Test 7,7,A
Test 8,8,B
Test 9,9,A
Test 10,10,B
Test 11,11,A
Test 12,12,B
Test 13,13,A
Test 14,14,B
Test 15,15,A
Test 16,16,B
Test 17,17,A
Test 18,18,B
Test 19,19,A
Test 21,21,B
Test 22,22,B
Test 23,23,B
Test 24,24,B
Test 25,25,B
Test 26,26,B
Test 27,27,B
\end{filecontents*}

\documentclass{article}
\usepackage{longtable}
\usepackage{csvsimple}
\begin{document}
\csvreader[
  longtable=lll,
  head,
  table head={%
    \bfseries Name & \bfseries Value & \bfseries Text\\\hline\endhead
    \caption{Example}\endlastfoot
  },
]{test-table.csv}{
  Name=\colname,
  Value=\colvalue,
  Text=\coltext
}{%
  \colname & \colvalue & \coltext
}
\end{document}

Have taken this example from Import multi-page table from PDF as table

Question: Why do I have to include the header manually, the text ist part of the csv input first row. Is there any way to inject the values of the first row to all headers of a longtable?

Best Answer

The header line is seen as sole administration information in csvsimple. It is uses to count the columns and it can be taken to address columns by their header name. But it is not used as data. The example you gave is the way it is meant to be.

To treat the header line as normal data entry, there is the option no head. Then, the first line is just a data line and is not used to determine the column count or to give columns their names. But you may access it by \csviffirstrow to do something special with it like using it for a longtable header.

Your example can be transformed in this way. I used \csvloop for it, because it is more basic and you may see better what happens:

\begin{filecontents*}{test-table.csv}
Name,Value,Text
Test 1,1,A
Test 2,2,B
Test 3,3,A
Test 4,4,B
Test 5,5,A
Test 6,6,B
Test 7,7,A
Test 8,8,B
Test 9,9,A
Test 10,10,B
Test 11,11,A
Test 12,12,B
Test 13,13,A
Test 14,14,B
Test 15,15,A
Test 16,16,B
Test 17,17,A
Test 18,18,B
Test 19,19,A
Test 21,21,B
Test 22,22,B
Test 23,23,B
Test 24,24,B
Test 25,25,B
Test 26,26,B
Test 27,27,B
Test 1,1,A
Test 2,2,B
Test 3,3,A
Test 4,4,B
Test 5,5,A
Test 6,6,B
Test 7,7,A
Test 8,8,B
Test 9,9,A
Test 10,10,B
Test 11,11,A
Test 12,12,B
Test 13,13,A
Test 14,14,B
Test 15,15,A
Test 16,16,B
Test 17,17,A
Test 18,18,B
Test 19,19,A
Test 21,21,B
Test 22,22,B
Test 23,23,B
Test 24,24,B
Test 25,25,B
Test 26,26,B
Test 27,27,B
\end{filecontents*}

\documentclass{article}
\usepackage{longtable}
\usepackage{csvsimple}
\begin{document}
\csvloop{
  file=test-table.csv,
  no head,
  column count=3,% could may removed for 10 or less columns
  before reading=\begin{longtable}{lll}\caption{Example}\endlastfoot,
  command={\csviffirstrow%
    {\bfseries\csvcoli & \bfseries\csvcolii & \bfseries\csvcoliii}
    {\csvcoli & \csvcolii & \csvcoliii}%
  },
  late after line=\\,
  late after first line=\\\hline\endhead,
  late after last line=,
  after reading=\end{longtable}
}
\end{document}

Alternatively, the columns could get names by adding

%...
column names={1=\colname,2=\colvalue,3=\coltext},
%...

But, I think, the idea of the example is that we don't know the column names in advance ...

Related Question