# Csvsimple \csvautotabular and \csvautobooktabular with centered columns' content

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}



Here's a MWE:

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

\usepackage{csvsimple} % For csv importing.

% csv file from another question
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\begin{document}
\end{document}


And the MWE output.

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}

• Don't change a question this way. The update has apparently very little to do with the rest of the question; ask a new one with the relevant example. – egreg Feb 13 '16 at 23:01
• I'm sorry. I thought this way would be better. I finally figured it out, and feel kinda silly. Edited! – Calculus Knight Feb 14 '16 at 0:01

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,
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}
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}


A version with \csvautobooktabularcenter:

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

\usepackage{csvsimple} % For csv importing.

\makeatletter
\csvset{
autotabularcenter/.style={
file=#1,
late after line=\\,
table foot=\\\hline,
late after last line=\csv@tablefoot\end{tabular}\csv@posttable,
command=\csvlinetotablerow},
autobooktabularcenter/.style={
file=#1,
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}
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}


• This worked beautifully! Would it be too much to ask to have also the \csvautobooktabular version of the custom command? – Calculus Knight Feb 12 '16 at 10:55
• @CalculusKnight You're welcome! – egreg Feb 12 '16 at 11:05
• Any clue on how to add the 'respect all' option to the custom command? I tried to figure it out on my own but, to my eyes, it's wizardry. – Calculus Knight Feb 13 '16 at 21:47
• @CalculusKnight Sorry, but I don't know what you're talking about. You probably are an expert with csvsimple; I just read the necessary code and modified it. – egreg Feb 13 '16 at 22:06

I found the manual almost incomprehensible for the most part. But it mentions several times that you typically wouldn't use \csvautotabular or \csvautotabular in practice.

They suggest using \csvreader. Here is a minimal use case of \csvreader, which may be preferable for other people (as it was for me).

\csvreader[
tabular=|c|c|r|c|r|,
table head=\hline \bfseries{Name} & \bfseries{Given Name} & \bfseries{Matriculation} & \bfseries{Gender} & \bfseries{Grade} \\\hline,
late after last line=\\\hline % horizontal line at the end of the table
]{