4

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}
  • 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
3

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

  • 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
1

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
]{
  grade.csv
}{}{\csvlinetotablerow}

Table with various alignments

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

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.