# csvsimple, siunitx and tabularx

In the MWE

\documentclass{article}

\usepackage{filecontents}
\usepackage{csvsimple}
\usepackage{xcolor}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{siunitx}

\definecolor{ColourTable}{gray}{0.3}
\definecolor{ColourHeader}{gray}{0.7}
\definecolor{ColourCells}{gray}{0.95}

\makeatletter

\newcommand{\DBTable}[2]
{
\csvloop
{
file = #1.csv,
before reading =
\begingroup
\ttfamily
\renewcommand{\arraystretch}{1.07}
\setlength{\arrayrulewidth}{1pt}
\arrayrulecolor{white}
\sisetup{detect-all=true},
after head =
\begin{tabular}{#2}
\rowcolor{ColourTable}
\multicolumn{\csv@columncount}{l}{\color{white}\bfseries #1} \\ \hline
\rowcolor{ColourHeader} \bfseries
\csvlinetotablerow,
before line = \\ \hline \rowcolor{ColourCells},
late after last line = \end{tabular},
after reading = \endgroup,
command = \csvlinetotablerow
}
}

\makeatother

\begin{filecontents*}{Articles.csv}
ID,Bezeichnung,Price,Stock
A1273,HDMI cable 1.8 m,8.95,45
A1367,VGA cable 3.0 m,4.95,10
\end{filecontents*}

\begin{document}

\DBTable{Articles}{l|l|l|l}

% \DBTable{Articles}{l|l|S[table-format=3.2]|S[table-format=2.0]}

\end{document}


which results in the output

I would like to use

1. the S column type of the siunitx package;
2. the tabularx instead of the tabular environment.

The first problem is related to the fact that the column headers need to be braced. I do not know, however, how to accomplish this in the \csvloop command.

As for the second problem, when I replace tabular by tabularx adding a certain width and a stretchable column type, I get an error.

Edit

My ultimate goal is to generate the code

\documentclass{article}

\usepackage{filecontents}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\usepackage{siunitx}

\definecolor{ColourTable}{gray}{0.3}
\definecolor{ColourHeader}{gray}{0.7}
\definecolor{ColourCells}{gray}{0.95}

\begin{filecontents*}{Articles.csv}
ID,Bezeichnung,Price,Stock
A1273,HDMI cable 1.8 m,8.95,45
A1367,VGA cable 3.0 m,4.95,10
\end{filecontents*}

\begin{document}

% \DBTable{Articles}{10cm}{l|X|S[table-format=3.2]|S[table-format=2.0]}

\begingroup
\ttfamily
\renewcommand{\arraystretch}{1.07}
\setlength{\arrayrulewidth}{1pt}
\arrayrulecolor{white}
\sisetup{detect-all=true},
\begin{tabularx}{10cm}{l|X|S[table-format=3.2]|S[table-format=2.0]}
\rowcolor{ColourTable} \multicolumn{4}{l}{\color{white}\bfseries Articles} \\ \hline
\rowcolor{ColourHeader} ID & Bezeichnung & {Price} & {Stock} \\ \hline
\rowcolor{ColourCells} A1273 & HDMI cable 1.8 m & 8.95 & 45 \\
\rowcolor{ColourCells} A1367 & VGA cable 3.0 m & 4.95 & 10
\end{tabularx}
\endgroup

\end{document}


automatically from the CSV file using the \DBTable command exhibiting paramaters for (1) file name, (2) table width and (3) column specifications.

Is this possible at all based on the csvsimple package? Would it be better to parse the CSV file, e.g., using LaTeX3 commands?

• Off-topic: You shouldn't load the xcolor and colortbl packages independently of each other. To maximize mutual compatibility, write \usepackage[table]{xcolor}. – Mico Jun 26 '18 at 20:43
• Relevant: tex.stackexchange.com/a/12665/134641 – Coby Viner Jun 26 '18 at 20:59