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}
2
  • 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, 2016 at 23:01
  • I'm sorry. I thought this way would be better. I finally figured it out, and feel kinda silly. Edited! Feb 14, 2016 at 0:01

2 Answers 2

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

4
  • This worked beautifully! Would it be too much to ask to have also the \csvautobooktabular version of the custom command? Feb 12, 2016 at 10:55
  • @CalculusKnight You're welcome!
    – egreg
    Feb 12, 2016 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. Feb 13, 2016 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, 2016 at 22:06
2

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}

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
2
  • And how to center all columns without writing out explicitly the string for tabular when one, for example, doesn't know how many columns there are or if the number of in the input csv file can change.? Thank you
    – Confounded
    Nov 11, 2020 at 16:48
  • If you want that, then use egreg's solution. However, if you want number columns to be right-aligned and the rest centred - as I've shown here - then that won't work (which is why I posted this answer). The only way I know how to include different alignments is to do it is explicitly. PS. I'm not a TeXpert, so there may be a way, but I don't know it. Nov 12, 2020 at 0:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .