I want to use the datatool
package to read csv files and parse them to LaTeX tables. Furthermore I want to use siunitx
to format the numbers from the csv file.
Have a look at the following example:
\documentclass{article}
\usepackage{datatool}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\usepackage{colortbl}
%% table data
\begin{filecontents*}{scientists.csv}
name,surname,age,IQ
Albert,Einstein,133,210.12
Marie,Curie,145,220.12
\end{filecontents*}
%%% table design
\colorlet{tableheadcolor}{black!60}
\newcommand\tableheadfont{
\sffamily\bfseries
\slshape
\color{white}
}
\begin{document}
\DTLloaddb{table}{scientists.csv}
\sisetup{round-mode=places,
table-number-alignment = center-decimal-marker
}
\rowcolors{1}{gray!15}{white!100}
\begin{table}
\begin{tabular}{l
l
S[table-format = 3.0 ,round-precision=0]
S[table-format = 3.2 ,round-precision=2]
@{}l}
\rowcolor{tableheadcolor}
\tableheadfont name & \tableheadfont surname & \tableheadfont age & \tableheadfont iq & \tabularnewline
\hline
\DTLforeach*{table}%
{\name=name, \surname=surname, \age=age, \iq=IQ}%
{\DTLiffirstrow{}{\tabularnewline}%
\name & \surname & \age & \iq &
}
\end{tabular}
\end{table}
\end{document}
The last column contains a numerical value which shall be formatted with siunitx
(S-column). That's why I had to append an empty row (have a look at tabular, siunitx and input - `Extra }, or forgotten $.` for details).
here is the result:
There are two problems with this solution:
- due to the colored header the last header cell isn't complete (the q is missing)
- the "Albert" in the first data cell is shifted to the right.
Does anybody know how I can solve these problems?
%
after\DTLiffirstrow{}{\tabularnewline}
. I don't see an incomplete header, can you add a picture?