11

I would like to have a TeX-Snippet that takes a filename as an argument and outputs a table with toprule and bottomrule:

\begin{tabular}{lrr} 
    \toprule
    Fach & Dauer & Einkommen\\ 
    \midrule 
    Info & 2 & 12,75 \\
    MST & 6 & 8,20 \\
    VWL & 14 & 10,00\\ 
    \bottomrule
\end{tabular}

enter image description here

Source: http://www.namsu.de/Extra/pakete/Booktabs.html

Messreihe.csv

This is an example csv file:

Messreihe #,Winkel,Zähler 1,Zähler 2,Koinzidenzen
1,90,35404,41016,155
,90,31999,38359,155
,135,30618,36095,126
,135,30232,36000,120
,180,30243,33931,118
,180,29762,34080,129
2,90,28979,36801,118
,90,29009,37035,144
,135,29035,35828,115
,135,28330,35599,122
,180,28866,33316,118
,180,28716,33774,99
3,90,29075,37602,139
,90,28793,37495,133
,135,28368,35938,116
,135,28570,35419,129
,180,28632,34177,114
,180,28467,34076,111

What I've tried

pgfplotstable

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{pgfplotstable} % Generates table from .csv

% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}

\begin{table}[h!]
  \begin{center}
    \caption{Autogenerated table from .csv file.}
    \label{table1}
    \pgfplotstabletypeset[
      multicolumn names, % allows to have multicolumn names
      col sep=comma, % the seperator in our .csv file
      display columns/0/.style={
        column name=$Value 1$, % name of first column
        column type={S},string type},  % use siunitx for formatting
      display columns/1/.style={
        column name=$Value 2$,
        column type={S},string type},
      display columns/2/.style={
        column name=$Value 3$,
        column type={S},string type},
      display columns/3/.style={
        column name=$Value 4$,
        column type={S},string type},
      display columns/4/.style={
        column name=$Value 5$,
        column type={S},string type},
      every head row/.style={
        before row={\toprule}, % have a rule at top
        after row={
            Messreihe & Winkel & Zähler 1 & Zähler 2 & Koinzidenzen\\ % the units seperated by &
            \midrule} % rule under units
            },
        every last row/.style={after row=\bottomrule}, % rule at bottom
    ]{messergebnisse.csv} % filename/path to file
  \end{center}
\end{table}

\end{document}

This gives me:

./csv-physik.tex:47: Illegal parameter number in definition of \pgfplotstable@loc@TMPa. [    ]{messergebnisse.csv}]

csvsimple - try 1

This works only when I remove the sharp (#) within the CSV file and the formatting is not as desired:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{csvsimple}

% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}
\csvautotabular{messergebnisse.csv}
\end{document}

enter image description here

csvsimple - try 2

This one is not very convenient, because I had to adjust the headers. Another drawback of this solution is that bottomrule is missing:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{csvsimple}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}
\csvreader[tabular=lrrrr,
    table head=\toprule Messwert & Winkel & Zähler 1 & Zähler 2 & Koinzidenzen\\\midrule,
    late after line=\\]%
{messergebnisse.csv}{a=\a,b=\b,c=\c,d=\d,e=\e}%
{\a & \b & \c & \d & \e}%
\end{document}

enter image description here

datatool - first try

This is easy to use, but the style is not correct. And it's not possible to have the sharp # within the CSV:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{datatool}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}
\DTLloaddb{stores}{messergebnisse.csv}
\DTLdisplaydb{stores}
\end{document}

enter image description here

datatool - second try

This one is almost what I would like to have. The only thing I miss is the possibility to write a sharp # within the table header.

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{datatool}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% start every dtl table with \toprule from booktabs
\renewcommand{\dtldisplaystarttab}{\toprule}

% likewise for \midrule and \bottomrule from booktabs 
\renewcommand{\dtldisplayafterhead}{\midrule}
\renewcommand{\dtldisplayendtab}{\\\bottomrule}

% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}
\DTLloaddb{stores}{messergebnisse.csv}
\DTLdisplaydb{stores}
\end{document}

enter image description here

  • 1
    Have a look on the datatool package by N. Talbot, please – user31729 May 5 '14 at 13:30
  • @ChristianHupfer Thank you! My last try was almost successful. The only thing I miss is the sharp within the header. When I add a sharp (or \#), I get ! Illegal parameter number in definition of \@dtl@trmstr.. Do you have an idea how to fix it? – Martin Thoma May 5 '14 at 13:45
  • You can alias a column later but don't use # just because you need to say it's no. # is a big deal to worry about. – percusse May 5 '14 at 13:49
8

The datatool package also comes with the command \DTLloadrawdb which is like \DTLloaddb but maps special characters, such as #, to a command, such as \#. However, there's an added problem here. The column labels by default are set to the corresponding header title in the CSV file, but as these labels are used to construct control sequences (via \csname...\endcsname), they can't contain any active characters. This means that not only does the # cause a problem, but also the ä characters are a problem as diacritics are converted into active characters by the inputenc package.

The solution is to not only use \DTLloadrawdb to fix the # in the header text, but also use the keys option to override the default column labels:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{datatool}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% start every dtl table with \toprule from booktabs
\renewcommand{\dtldisplaystarttab}{\toprule}

% likewise for \midrule and \bottomrule from booktabs 
\renewcommand{\dtldisplayafterhead}{\midrule}
\renewcommand{\dtldisplayendtab}{\\\bottomrule}

% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\DTLloadrawdb[keys={Messreihe,Winkel,Zahler1,Zahler2,Koinzidenzen}]{stores}{messergebnisse.csv}

\begin{document}
\DTLdisplaydb{stores}
\end{document}

This produces:

Image of table

3

This works for me (note that I removed inputenc and friends for XeTeX, you can put them back):

\documentclass[a4paper]{scrartcl}

\usepackage[locale=DE]{siunitx}
\sisetup{
  round-mode          = places,
  round-precision     = 2,
}

\usepackage{booktabs}
\usepackage{pgfplotstable}

\begin{document}
\pgfplotstabletypeset[
col sep=comma,
every head row/.style={
  before row={\toprule},
  after row={\midrule}},
every last row/.style={after row=\bottomrule},
]{data.csv}
\end{document}

output

Interestingly enough, it seems that somewhere down the line the non-ASCII characters were stripped. Very annoying, but seems like a separate Q. I don't know what would be causing this.

  • The problem of that solution is that you have to adjust the header all the time. I ask this question for my girlfriend who studies physics. They (physics students) have to create documents with many tables (from excel files) every week. Currently, they use an Excel macro to directly create the table. I thought that a better (and faster) solution might exist by using csv files. The csv files do only contain data they want to show. – Martin Thoma May 5 '14 at 13:50
  • @moose If you remove column name completely, the headers are drawn from the CSV file. – Sean Allred May 5 '14 at 13:51
  • I got that error, too; I needed to remove the 'manual header' you specified in the pgfplots call. – Sean Allred May 5 '14 at 14:07
3

There is also a csvsimple solution without removing the sharp # symbol from your Messreihe.csv. Therefore, the catcode has to be changed locally. Additionally, the column names are set manually to cope with the umlauts, spaces and numbers.

With csvsimple version 1.11 (2014/07/08), the special character # is made normal by using the option respect sharp:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{csvsimple}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}

\csvloop{file=messergebnisse.csv,
  tabular=lrrrr,
  respect sharp,
  table head=\toprule\bfseries Messwert \#
             &\bfseries Winkel &\bfseries Zähler 1
             &\bfseries Zähler 2 &\bfseries Koinzidenzen\\\midrule,
  command=\csvlinetotablerow,
  table foot=\bottomrule}

\end{document}

The second approach does also work for older version of csvsimple. Additionally, I use explicit naming of the columns which would be needed if not all columns should appear in the resulting table:

\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage[locale=DE]{siunitx} % Formats the units and values
\usepackage{csvsimple}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
% Setup siunitx:
\sisetup{
  round-mode          = places, % Rounds numbers
  round-precision     = 2, % to 2 places
}

\begin{document}

\csvreader[tabular=lrrrr,
    table head=\toprule\bfseries Messwert \#
             &\bfseries Winkel &\bfseries Zähler 1
             &\bfseries Zähler 2 &\bfseries Koinzidenzen\\\midrule,
    late after line=\\,late after last line=\\\bottomrule,
    before reading={\catcode`\#=12},after reading={\catcode`\#=6}]%
{messergebnisse.csv}{1=\Messreihe,2=\Winkel,3=\ZaehlerI,4=\ZaehlerII,5=\Koinzidenzen}%
{\Messreihe & \Winkel & \ZaehlerI & \ZaehlerII & \Koinzidenzen}%

\end{document}

Both source codes compile to the same result:

enter image description here

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.