2

I have two CSV files. One has 5 columns and the other has 6 columns.

It is necessary to check the CSV file and determine the number of columns, which I managed to do. I saved the number of columns in \mycolumncount.

Furthermore, I made a condition to generate the corresponding table based on the number of columns, but for this part I can't get the output table.

It is important to note that it is necessary to use the csvsimple-l3 package.

What's wrong?

\begin{filecontents*}{file1.csv}
 a,b,c,d,e
 1,2,3,4,5
 4,5,6,7,8
\end{filecontents*}

\begin{filecontents*}{file2.csv}
a,b,c,d,e,f
1,2,3,4,5,6
4,5,6,7,8,9
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple-l3}
\usepackage{ifthen}

\ExplSyntaxOn
\NewDocumentCommand{\GetColumnCount}{m}
{
\int_zero_new:N \l_tmpa_int
\ior_open:Nn \g_tmpa_ior {#1}
\ior_str_map_inline:Nn \g_tmpa_ior
  {
    \clist_set:Nn \l_tmpa_clist {##1}
    \int_set:Nn \l_tmpa_int {\clist_count:N \l_tmpa_clist}
    \int_gset:Nn \g_tmpa_int {\l_tmpa_int}
  }
\int_use:N \g_tmpa_int
\ior_close:N \g_tmpa_ior
}
\ExplSyntaxOff

\newcommand{\customtable}[1]{%
\edef\mycolumncount{\GetColumnCount{#1}}
%
\ifthenelse{\equal{\mycolumncount}{5}}{%
  \begin{tabular}{|*{5}{c|}}
    \hline
    \csvreader[
      head to column names,
      late after line=\\\hline,
    ]{#1}{}{%
     \csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv
   }
  \end{tabular}
 }{\ifthenelse{\equal{\mycolumncount}{6}}{%
  \begin{tabular}{|*{6}{c|}}
    \hline
    \csvreader[
     head to column names,
     late after line=\\\hline,
   ]{#1}{}{%
     \csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi
   }
  \end{tabular}
  }{}
 }
 }

 \begin{document}

 \customtable{file1.csv}

 The number of columns is: \mycolumncount.

 \end{document}

I don't have a table on the output, but only information about the number of columns. I also note the important information that the columns of the table are of different widths.

enter image description here

1 Answer 1

2

The problem seems to be that \GetColumnCount is not expandable, as written. Since I am not L3 savvy, I resort to the readarray package to obtain the column count, which is provided in \ncols after a \readdef is performed.

\begin{filecontents*}{file1.csv}
 a,b,c,d,e
 1,2,3,4,5
 4,5,6,7,8
\end{filecontents*}

\begin{filecontents*}{file2.csv}
a,b,c,d,e,f
1,2,3,4,5,6
4,5,6,7,8,9
\end{filecontents*}

\documentclass{article}
\usepackage{readarray}
\readarraysepchar{,}
\usepackage{csvsimple-l3}
\usepackage{ifthen}

\newcommand{\customtable}[1]{%
\readdef{#1}\filedata
\let\mycolumncount\ncols
%
\ifthenelse{\equal{\mycolumncount}{5}}{%
  \begin{tabular}{|*{5}{c|}}
    \hline
    \csvreader[
      head to column names,
      late after line=\\\hline,
    ]{#1}{}{%
     \csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv
   }
  \end{tabular}
 }{\ifthenelse{\equal{\mycolumncount}{6}}{%
  \begin{tabular}{|*{6}{c|}}
    \hline
    \csvreader[
     head to column names,
     late after line=\\\hline,
   ]{#1}{}{%
     \csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi
   }
  \end{tabular}
  }{}
 }
 }

\begin{document}
\customtable{file1.csv}

The number of columns is: \mycolumncount.

\customtable{file2.csv}

The number of columns is: \mycolumncount.
\end{document}

enter image description here

The identical result can be obtained purely with the facilities of readarray as follows, which does not require the column count to be checked as part of the \customtable macro:

\begin{filecontents*}{file1.csv}
 a,b,c,d,e
 1,2,3,4,5
 4,5,6,7,8
\end{filecontents*}

\begin{filecontents*}{file2.csv}
a,b,c,d,e,f
1,2,3,4,5,6
4,5,6,7,8,9
\end{filecontents*}

\documentclass{article}
\usepackage{readarray}
\readarraysepchar{,}
\renewcommand\typesetrowsepchar{\\\hline}
\renewcommand\typesetcolsepchar{&}

\newcommand{\customtable}[1]{%
  \readdef{#1}\myfile
  \readarray*\myfile\myfiledata[-,\ncols]%
  \initarray\myabbrevdata[\numexpr\nrows-1\relax,\ncols]%
  \mergearray\myfiledata\myabbrevdata[0,1]%
  \begin{tabular}{|*{\ncols}{c|}}
  \hline
  \typesetarray\myabbrevdata\\
  \hline
  \end{tabular}}

\begin{document}
\customtable{file1.csv}

The number of columns is: \ncols.

\customtable{file2.csv}

The number of columns is: \ncols.
\end{document}

enter image description here

3
  • Thank you for the explanation and for the help. I managed to make the solution I needed. Commented Nov 26, 2023 at 20:28
  • @ZeljkoHrcek I found an error in my original post. I have corrected it. Commented Nov 26, 2023 at 20:28
  • 1
    Thank you for the update. That's exactly what I wanted to achieve. Commented Nov 26, 2023 at 20:47

You must log in to answer this question.

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