0

Can anybody help me modify the code below, from this post in order to have only the first column as left-aligned?

Thank you.

\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} 
  • 3
    Replace after head=\csv@pretable\begin{tabular}{|*{\csv@columncount} {c|}}\csv@tablehead, by after head=\csv@pretable\begin{tabular}{|l|*{\csv@columncount} {c|}}\csv@tablehead, do the job, but I'm not sure it's correct to declare one more column in the tabular environment as there is in the datafile. – Jérôme Dequeker Sep 27 '16 at 12:28
2

As I said as a comment, you have to redefine your tabular header.

\begin{tabular}{|*{\csv@columncount}{c}} will make a table where the number of columns has been determined from your csv file, and all the columns are centered. If you follow my comment, I suggested to add the first column as a left aligned one : \begin{tabular}{|l|*{\csv@columncount}{c}} but now, you have one more declared column in your header than in your csv file. That's why you have to add a new counter wich s equal to the number of columns minus 1, which correspond to the other centered columns :

\usepackage{etoolbox} % In the preambule
\newcommand\docolumncount[2]{%
  \csvloop{
    file=#1,
    command=,
    after reading={\numdef\mycolumncount{\csv@columncount-1}},
  }%
}

So, here is my MWE :

\documentclass[11pt,a4paper,oldfontcommands]{memoir}
\usepackage{babel}
\usepackage{csvsimple} % For csv importing.
\usepackage{etoolbox}
\makeatletter
\newcommand\docolumncount[2]{%
  \csvloop{
    file=#1,
    command=,
    after reading={\numdef\mycolumncount{\csv@columncount-1}},
  }%
}
\csvset{
  autotabularcenter/.style={
    file=#1,
    after head=\csv@pretable\begin{tabular}{|l|*{\mycolumncount}{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}
\docolumncount{\jobname.csv}{\mycolumncount}

\csvautotabularcenter{\jobname.csv}

\bigskip

\csvautobooktabularcenter{\jobname.csv}
\end{document} 

Note : The operation on the column counter comes from here

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.