使文本成为单行或单列并居中或加粗(csvsimple)

使文本成为单行或单列并居中或加粗(csvsimple)

在一些帮助下这里我可以csv使用此代码导入从文件读取的表:

\documentclass{article}
\usepackage{array,booktabs,longtable,csvsimple}

\usepackage{filecontents}

\begin{filecontents*}{NewTextDocument.csv}
name,job,age,profile
John,student,21,John has always been a very diligent student his marks always being among the best
Frederik,student,18,Frederik has not been a very diligent student
Johnson,professor,49,Johnson is just a professor ...
\end{filecontents*}

\makeatletter
\csvset{
 my first column width/.style={after head=\csv@pretable\begin{longtable}{*{\csv@columncount}{p{#1}}}\csv@tablehead},
 my second column width/.style={after head=\csv@pretable\begin{longtable}{|*{\csv@columncount}{p{#1}|}}\csv@tablehead},
}
\makeatother

\begin{document}

\csvautobooklongtable[
 separator=comma, 
 my first column width=3cm, 
 late after line={\\\midrule},
 late after last line={\end{longtable}}
]{NewTextDocument.csv}

\csvautobooklongtable[
 separator=comma, 
 my second column width=3cm, 
 table head={\hline\csvlinetotablerow\\\hline},
 late after line={\\\hline},
 late after last line={\\\hline\end{longtable}}
]{NewTextDocument.csv}

\end{document}

但是,现在我试图通过居中和“加粗”整行或整列的文本来做一些小改动,但还没有成功。有人能帮我修改上面的代码来做到这一点吗?

编辑:我还想给表格添加标签和标题,这也是我遇到的困难。

答案1

您可以使用\csvreader

\documentclass{article}
\usepackage{array,booktabs,longtable,csvsimple}
\renewcommand{\arraystretch}{1.1}

\usepackage{filecontents}
\begin{filecontents*}{NewTextDocument.csv}
    name,job,age,profile
    John,student,21,John has always been a very diligent student his marks always being among the best
    Frederik,student,18,Frederik has not been a very diligent student
    Johnson,professor,49,Johnson is just a professor ...
\end{filecontents*}

\begin{document}
    Table~\ref{myfirsttable} is with \texttt{booktabs}.

    \csvreader[
    longtable={*3{c}p{19em}},
    table head={\caption{With \texttt{booktabs}\label{myfirsttable}}\\\toprule
        \textbf{Name} & \textbf{Job} & \textbf{Age} & \multicolumn{1}{c}{\textbf{Profile}} \\ 
        \midrule
        \endfirsthead
        \toprule
        \textbf{Name} & \textbf{Job} & \textbf{Age} & \multicolumn{1}{c}{\textbf{Profile}} \\ 
        \midrule
        \endhead},
    late after line=\\\midrule,
    late after last line=\\\bottomrule,
    ]{NewTextDocument.csv}{1=\name,2=\job,3=\age,4=\profile}{\name & \job  & \age & \profile} 

    Table~\ref{mysecondtable} is without \texttt{booktabs}.

    \csvreader[
    longtable={|*3{c|}p{19em}|},
    table head={\caption{Without \texttt{booktabs}\label{mysecondtable}}\\\hline
        \textbf{Name} & \textbf{Job} & \textbf{Age} & \multicolumn{1}{c|}{\textbf{Profile}} \\ 
        \hline
        \endfirsthead
        \hline
        \textbf{Name} & \textbf{Job} & \textbf{Age} & \multicolumn{1}{c|}{\textbf{Profile}} \\ 
        \hline
        \endhead},
    late after line=\\\hline,
    ]{NewTextDocument.csv}{1=\name,2=\job,3=\age,4=\profile}{\name & \job  & \age & \profile} 

    Table~\ref{mythirdtable} is my suggestion.

    \csvreader[
    longtable={l*2{c}p{19em}},
    table head={\caption{With few lines\label{mythirdtable}}\\\toprule
        \textbf{Name} & \textbf{Job} & \textbf{Age} & \multicolumn{1}{c}{\textbf{Profile}} \\ 
        \midrule
        \endfirsthead
        \toprule
        \textbf{Name} & \textbf{Job} & \textbf{Age} & \multicolumn{1}{c}{\textbf{Profile}} \\ 
        \midrule
        \endhead},
    late after line=\\,
    late after last line=\\\bottomrule,
    ]{NewTextDocument.csv}{1=\name,2=\job,3=\age,4=\profile}{\name & \job  & \age & \profile}

\end{document}

在此处输入图片描述

相关内容