1

I want to get more tables from one CSV file so that the tables have a header and only one row from the input CSV file.

The first table has the header and the first row of the CSV file, the second will have the header and the second row of the CSV file, and so on until the last row of the CSV file. The number of tables will be equal to the number of rows in the CSV file.

I made an example and managed to generate the number of tables equal to the number of lines of the CSV file, but unfortunately, I only have the header. I am missing data for all tables (one row each) from the CSV file.

It is important that the csvsimple-l3 package is used in the given example

 \documentclass{article}
 \begin{filecontents*}{data9.txt}
 1,A,6,Euploid,XX
 2,B,7,Euploid,XX
 3,C,8,Euploid,XX
 4,D,9,Euploid,XXY
 5,E,4,Euploid,XX
 6,F,3,Euploid,XX
 \end{filecontents*}

\usepackage{readarray}
\readarraysepchar{,}
\usepackage{csvsimple-l3}
\usepackage{tabularray}
\usepackage{pgffor}

\begin{document}

\newcommand{\customtable}[1]{%
\readdef{#1}\filedata
\let\myrowcount\nrows
%
\foreach \i in {1,...,\myrowcount}{%
\csvreader[%
generic collected table = longtblr,
generic table options = {[
    label = none
]{
colspec={*{5}{X[1,c]}},
vlines,
hlines,
width=\textwidth,
}},
no head,
filter test={\ifnum\thecsvrow=\i\fi},
table head ={Col 1&Col 2&Col 3&Col 4 &Col 5\\}
]%
{#1}{}%
{\ifnum\thecsvrow=\i
 \csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv
  \else \fi}
   }
}

\customtable{data9.txt}

The number of columns is: \myrowcount.

\end{document}

enter image description here

If someone can help and give an example, it would be useful and I would be very grateful.

1 Answer 1

2

While this answer may not be of direct use to the OP, since it was specified that "It is important that the csvsimple-l3 package is used in the given example", I nonetheless show other readers how this may be accomplished directly with the readarray package.

\begin{filecontents*}[overwrite]{data9.txt}
1,A,6,Euploid,XX
2,B,7,Euploid,XX
3,C,8,Euploid,XX
4,D,9,Euploid,XXY
5,E,4,Euploid,XX
6,F,3,Euploid,XX
\end{filecontents*}
\documentclass{article}
\usepackage{readarray,pgffor}
\readarraysepchar{,}
\parskip 12pt
\begin{document}
\def\mheaderdat{Col1,Col2,Col3,Col4,Col5}
\readarray*\mheaderdat\mheader[-,5]
\typesetarray\mheader

\readdef{data9.txt}\mdatadat
\readarray*\mdatadat\mdata[-,\ncols]
\typesetarray\mdata

\renewcommand\typesetrowsepchar{\\\hline}
\renewcommand\typesetcolsepchar{&}
\initarray\mtable[2,5]
\foreach\z in{1,...,\nrows}{%
  \mergearray\mdata\mtable[\numexpr3-\z,1]
  \mergearray\mheader\mtable[1,1]
  \begin{tabular}{|c|c|c|c|c|}
    \hline
    \typesetarray\mtable\\
    \hline
  \end{tabular}\par
}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .