修改现有的 csv 简单解决方案。仅第一列左对齐

修改现有的 csv 简单解决方案。仅第一列左对齐

有人能帮我修改下面的代码吗发布以便只有第一列左对齐?

谢谢。

\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} 

答案1

正如我在评论中所说,您必须重新定义您的tabular标题。

\begin{tabular}{|*{\csv@columncount}{c}}将创建一个表格,其中的列数已从您的 csv 文件中确定,并且所有列都居中。如果您按照我的评论操作,我建议将第一列添加为左对齐列:\begin{tabular}{|l|*{\csv@columncount}{c}}但现在,标题中声明的列比 csv 文件中的多一个。这就是为什么您必须添加一个新的计数器,其值等于列数减 1,它与其他居中的列相对应:

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

以下是我的 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} 

注意:列计数器上的操作来自这里

相关内容