# loop for table generation with pgfplotstable

I am using pgfplotstable to enter my CSV tables into LaTeX. The issue is that I am using two lines as the header, in the first line introducing my variables and second line their units. The following MWE works perfectly fine for my goal, but what annoys me is that I have to repeat every row #1 column 0/.append style={#2} so many times to assure that with entering a table with an arbitrary number of columns This code will still work. I was trying to put this line in a loop, but even with checking questions here and other sources I failed every time.

\documentclass[a4paper]{article}

\usepackage{filecontents}

\begin{filecontents*}{mytable.csv}
Number,$\mathrm{f}$,$\Delta\mathrm{f}$
,\si{\kHz},\si{\kHz}
1,1000,0.1
2,3000,0.2
3,5000,0.3
\end{filecontents*}
\usepackage{pgfplotstable,booktabs,array,pgfgantt,
pgfplots,colortbl,gensymb,siunitx}
\pgfplotsset{plot coordinates/math parser=false}
\pgfplotsset{compat=1.12}
\pgfplotstableset{
rowstyle/.style 2 args={
every row #1 column 0/.append style={#2},
every row #1 column 1/.append style={#2},
every row #1 column 2/.append style={#2},
every row #1 column 3/.append style={#2},
every row #1 column 4/.append style={#2},
},
colstyle/.style 2 args={
every col no #1/.append style={#2},
},
col sep = comma,
every head row/.style={output empty row},
every first row/.style={before row=\toprule},
every row no 1/.style ={after row=\midrule},
every even row/.style={before row=\rowcolor{lightgray}},
rowstyle={0}{string type},
rowstyle={1}{string type},
}

\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset[
fixed, fixed zerofill, precision=3,
columns/0/.append style={precision=0},
]
{mytable.csv}
\caption{Caption of my table}
\end{table}
\end{document}


So, in the end, I have two questions:

1. Assuming I am entering a large number as maximum columns number(for example 50) how can I put it in a loop that I do not have to repeat it 50 times?

2. Is there any way to have a macro which goes through the CSV file, checks the number of columns and makes a loop for them?

How about an option, which sets the first 2 rows as string? But it must be used after every thing, which sets the cell type with string type, numeric type etc., or it will be overwritten. The advantage: no need to know how many columns there are.

This uses .add code to wrap the original code of assign cell content into the \else part of an \ifcase, which checks the row number (\pgfplotstablerow) and sets the cell content for the first 2 rows to the raw input instead of the formated number.

\documentclass[a4paper]{article}

\usepackage{filecontents}

\begin{filecontents*}{mytable.csv}
Number,$\mathrm{f}$,$\Delta\mathrm{f}$
,\si{\kHz},\si{\kHz}
1,1000,0.1
2,3000,0.2
3,5000,0.3
\end{filecontents*}
\usepackage{pgfplotstable,booktabs,array,pgfgantt,
pgfplots,colortbl,gensymb,siunitx}
\pgfplotsset{plot coordinates/math parser=false}
\pgfplotsset{compat=1.12}
\pgfplotstableset{
% must be used after everything setting type (string type, numeric type, etc.),
% otherwise it will be overwritten
first 2 rows as string/.style={%
assign cell content/.add code={%
% prepend to original code
\ifcase\pgfplotstablerow
% row 0: take raw contents
\pgfkeyssetvalue{/pgfplots/table/@cell content}{##1}%
\or
% row 1: take raw contents
\pgfkeyssetvalue{/pgfplots/table/@cell content}{##1}%
\else
% other rows: original code
}{%
% append to original code
\fi
}%
},
colstyle/.style 2 args={
every col no #1/.append style={#2},
},
col sep = comma,
every head row/.style={output empty row},
every first row/.style={before row=\toprule},
every row no 1/.style ={after row=\midrule},
every even row/.style={before row=\rowcolor{lightgray}},
}

\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset[
fixed, fixed zerofill,precision=3,
columns/0/.append style={precision=0},
first 2 rows as string
]
{mytable.csv}
\caption{Caption of my table}
\end{table}
\end{document}