3

With some help here I was able to import tables read from a csv file using this code:

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

However, now I'm trying to make a little change by centering and "bolding" the text of a whole row or column but I haven't had any luck yet. Can anyone help me modify the code above to do so?

EDIT: And I would also like to label and add a caption to the tables, which I'm also struggling with.

CarLaTeX
  • 62,716

1 Answers1

3

You could use \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}

enter image description here

CarLaTeX
  • 62,716