1

I am using \csvreader to read a csv and I am trying to do some basic manipulation like counting the number of values and print that value in the document. For instance, how do I calculate the number of males from the code below, and print There are a total of \malesnum males?

\documentclass{standalone}

\usepackage{csvsimple}


\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}


\begin{document}
    \begin{tabular}{l|c}%
    \bfseries Person & \bfseries Matr.~No.% specify table head
    \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
    {\\\hline\givenname\ \name & \matriculation}% specify your coloumns here
    \end{tabular}
\end{document}
2

Here is an example with the datatool package.

\documentclass{article}

\usepackage{datatool}

\newcommand*{\thePassGrade}{2.0}

\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\DTLloaddb{grades}{grade.csv}

\begin{document}

\begin{tabular}{lccc}
Last Name & Matriculation & Gender & Grade \\ \hline
\DTLforeach*[\DTLiseq{\gn}{m}]{grades}%
    {\ln=name,\mat=matriculation,\gn=gender,\gr=grade}
    {%
        \DTLiffirstrow{}{\\}
        \ln & \mat & \gn &  \gr %
    } \\ \hline
\end{tabular}

\DTLsavelastrowcount{\nmales}

\medskip

This database has a total of \nmales{} male students.

\bigskip

\begin{tabular}{lccc}
Last Name & Matriculation & Gender & Grade \\ \hline
\DTLforeach*[\DTLisgt{\gr}{\thePassGrade}]{grades}%
    {\ln=name,\mat=matriculation,\gn=gender,\gr=grade}
    {%
        \DTLiffirstrow{}{\\}
        \ln & \mat & \gn &  \gr %
    } \\ \hline
\end{tabular}

\DTLsavelastrowcount{\pass}

\medskip

This database has a total of \pass{} students who had over \thePassGrade.

\end{document}
1

This solution uses pgfplotstable. The hard part was merging two columns into one (fullname).

\documentclass{article}

\usepackage{pgfplotstable}

\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}


\begin{document}
    \pgfplotstableread[string type,col sep=comma]{grade.csv}\mytable

    \pgfplotstablegetrowsof{\mytable}%
    number rows = \pgfplotsretval{} (header row doesn't count)

    \pgfplotstablegetcolsof{\mytable}%
    number columns = \pgfplotsretval

    \pgfplotstablecreatecol[create col/assign/.code={%
      \edef\entry{\thisrow{givenname}~\thisrow{name}}
      \pgfkeyslet{/pgfplots/table/create col/next content}\entry}]%
    {fullname}\mytable

    \pgfplotstabletypeset[string type,columns={fullname,matriculation},
      every head row/.style={before row=\hline,after row=\hline},
      columns/fullname/.style={column name=\textbf{Person},column type={l|}},
      columns/matriculation/.style={column name=\textbf{Matr.~No.},column type={c}}
      ]{\mytable}

\end{document}
1

For instance

\begin{filecontents*}{\jobname.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\documentclass{article}

\usepackage{csvsimple}

\newcounter{dambocnt}

\begin{document}

\setcounter{dambocnt}{0}
\newcommand{\ismale}{%
  \ifnum\pdfstrcmp{\gender}{m}=0
    \stepcounter{dambocnt}%
  \fi
}

\begin{tabular}{l|c}%
  \bfseries Person & \bfseries Matr.~No.% specify table head
  \csvreader[head to column names]{\jobname.csv}{}% use head of csv as column names
  {\\\hline\ismale\givenname\ \name & \matriculation}% specify your columns here
\end{tabular}

There were \thedambocnt\ males.

\end{document}

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.