Csvsimple \csvautotabular and \csvautobooktabular with centered columns' content
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}
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}
I found the manual almost incomprehensible for the most part. But it mentions several times that you typically wouldn't use \csvautotabular
or \csvautobooktabular
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
]{
grade.csv
}{}{\csvlinetotablerow}
Pros vs egreg's solution:
- More control over table header and column alignments
- Less overall code for a single table
Cons vs egreg's solution:
- More overall code if you're going to use it for many tables