[Tex/LaTex] Csvsimple \csvautotabular and \csvautobooktabular with centered columns’ content

csvsimpletables

I'm using csvsimple for making tables, and csvautotabular and csvautobooktabular are working great. However, I'd like the content of each column to be centered instead of aligned to the left.

An answer using csvautotabular and/or csvautobooktabular simply adding an option would be preferred, if it's possible.

I tried this, but it returns an error.

\csvautotabular[tabular=c]{grade.csv}

\csvautobooktabular[tabular=c]{grade.csv}

Here's a MWE:

\documentclass[11pt,a4paper,oldfontcommands]{memoir}

\usepackage{csvsimple} % For csv importing.

% csv file from another question
\begin{filecontents*}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

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

And the MWE output.

enter image description here

I want to obtain said output, but with the content of each column centered.


Note

csvautotabular gave me trouble trying to display special characters, but can be solved using the 'respect all' option.

e.g.

\csvautotabular[respect all]{table.csv}

Or with @egreg's custom command.

\csvautotabularcenter[respect all]{table.csv}

Best Answer

As far as I can see, there's no provision for changing the column alignment in \csvautotabular; you can generate a different command by mimicking what csvsimple does for the stock command:

\documentclass[11pt,a4paper]{memoir}

\usepackage{csvsimple} % For csv importing.

\makeatletter
\csvset{
  autotabularcenter/.style={
    file=#1,
    after head=\csv@pretable\begin{tabular}{|*{\csv@columncount}{c|}}\csv@tablehead,
    table head=\hline\csvlinetotablerow\\\hline,
    late after line=\\,
    table foot=\\\hline,
    late after last line=\csv@tablefoot\end{tabular}\csv@posttable,
    command=\csvlinetotablerow},
}
\makeatother
\newcommand{\csvautotabularcenter}[2][]{\csvloop{autotabularcenter={#2},#1}}

% csv file from another question
\begin{filecontents*}{\jobname.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\begin{document}
\csvautotabularcenter{\jobname.csv}
\end{document}

enter image description here

A version with \csvautobooktabularcenter:

\documentclass[11pt,a4paper,oldfontcommands]{memoir}

\usepackage{csvsimple} % For csv importing.

\makeatletter
\csvset{
  autotabularcenter/.style={
    file=#1,
    after head=\csv@pretable\begin{tabular}{|*{\csv@columncount}{c|}}\csv@tablehead,
    table head=\hline\csvlinetotablerow\\\hline,
    late after line=\\,
    table foot=\\\hline,
    late after last line=\csv@tablefoot\end{tabular}\csv@posttable,
    command=\csvlinetotablerow},
  autobooktabularcenter/.style={
    file=#1,
    after head=\csv@pretable\begin{tabular}{*{\csv@columncount}{c}}\csv@tablehead,
    table head=\toprule\csvlinetotablerow\\\midrule,
    late after line=\\,
    table foot=\\\bottomrule,
    late after last line=\csv@tablefoot\end{tabular}\csv@posttable,
    command=\csvlinetotablerow},
}
\makeatother
\newcommand{\csvautotabularcenter}[2][]{\csvloop{autotabularcenter={#2},#1}}
\newcommand{\csvautobooktabularcenter}[2][]{\csvloop{autobooktabularcenter={#2},#1}}

% csv file from another question
\begin{filecontents*}{\jobname.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\begin{document}

\csvautotabularcenter{\jobname.csv}

\bigskip

\csvautobooktabularcenter{\jobname.csv}

\end{document}

enter image description here

Related Question