TeX - LaTeX Asked by Calculus Knight on February 7, 2021
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.
I want to obtain said output, but with the content of each column centered.
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}
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@pretablebegin{tabular}{|*{csv@columncount}{c|}}csv@tablehead,
table head=hlinecsvlinetotablerowhline,
late after line=,
table foot=hline,
late after last line=csv@tablefootend{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@pretablebegin{tabular}{|*{csv@columncount}{c|}}csv@tablehead,
table head=hlinecsvlinetotablerowhline,
late after line=,
table foot=hline,
late after last line=csv@tablefootend{tabular}csv@posttable,
command=csvlinetotablerow},
autobooktabularcenter/.style={
file=#1,
after head=csv@pretablebegin{tabular}{*{csv@columncount}{c}}csv@tablehead,
table head=toprulecsvlinetotablerowmidrule,
late after line=,
table foot=bottomrule,
late after last line=csv@tablefootend{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}
Answered by egreg on February 7, 2021
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:
Cons vs egreg's solution:
Answered by Multihunter on February 7, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP