9

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: enter image description here

There are two problems with this solution:

  1. due to the colored header the last header cell isn't complete (the q is missing)
  2. the "Albert" in the first data cell is shifted to the right.

Does anybody know how I can solve these problems?

  • 1
    Welcome to TeX.SX! I have taken the liberty of doing minor cosmetic edits to your post, like inline code marking, I hope you don't mind.. – Corentin Jul 11 '13 at 17:18
  • The shift of ‘Albert’ is due to a missing % after \DTLiffirstrow{}{\tabularnewline}. I don't see an incomplete header, can you add a picture? – egreg Jul 11 '13 at 17:29
  • Same here, No incomplete header. May be you should update! – user11232 Jul 11 '13 at 22:19
  • Hi,thanks i corrected the shifted albert. and i added a picture. The "q" if iq is missing in the header. – Jonas Jul 12 '13 at 7:56
7

Both simple problems. The misplacement of 'Albert' is as mentioned in a comment due to a missing %:

\DTLiffirstrow{}{\tabularnewline}%

The odd business with the q is caused by having & \tabularnewline at the end of the header row, which mis-places things. Try instead just \\:

\documentclass{article}

\usepackage{datatool}
\usepackage{siunitx}
\usepackage{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
         }

\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 the long iq  \\
    \hline
    \DTLforeach*{table}%
    {\name=name, \surname=surname, \age=age, \iq=IQ}%
    {\DTLiffirstrow{}{\tabularnewline}%
     \name & \surname & \age & \iq &
    }
\end{tabular}

\end{document}
  • Hi. Thanks for your answer. Yes that works, but there is another problem: in my original table i have alternating rowcolors (with \usepackage[table]{xcolor} and \rowcolors{1}{gray!15}{white!100}). In this case there is also the end of each line missing and not only the header. This can't be resolved with your solution because datatool throws an error if the line is not complete. (i added this to the first post) – Jonas Jul 12 '13 at 8:47
  • @Jonas Add \rowcolor{gray!15} after the \hline and add \DTLifoddrow{\rowcolor{gray!15}}{\rowcolor{white!100}} after \tabularnewline (before the closing brace) and that should do it. – Nicola Talbot Jul 14 '13 at 10:16

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.