7

I am using csvreader from the csvsimple package to generate a table in my document. I am struggling to get a top border on the table. Here is the code, including writing the .csv

\documentclass{article}
\usepackage{csvsimple}

% Make csv in question
\begin{filecontents*}{scientists2.csv}
name,surname,
Albert,Einstein,
Marie,Curie,
Thomas,Edison
\end{filecontents*}

\begin{document}

\begin{center}
\csvreader[tabular=|l|l|,
    table head=\textbf{ First Name} & \textbf{Last Nmae} \\\hline,
    late after line = \\\hline]%
{scientists2.csv}{name=\name,surname=\surname}%
{\name & \surname}%

\end{center}


\end{document} 

What argument can I pass (to tabular?) to see the top border? I am using TexWorks to compile. Thanks,

2

1 Answer 1

7

1. csvsimple:

You can simply adjust the table head to start with an \hline:

enter image description here

2. booktabs:

However, the booktabs package is usually recommened to produce more professional looking tables. So, I have added a second example that uses this along with the datatool package:

enter image description here

Much thanks to Nicola Talbot, who helped with proper use of booktabs here.

3. pgfplotstable:

A solution using pgfplotstable is provided at Bold one cell in table using CSV reader.

Notes:

  • Not sure why I needed the extra \\ before the \bottomrule which is producing the additional spacing at the bottom. To fix this I added a manual \vspace*{-12pt}, which is more of a hack.

  • It appears the the csvsimple package was discarding the last row as it did not have a trailing ,.

Code:

\documentclass{article}

\usepackage{datatool}
\usepackage{booktabs}
\usepackage{csvsimple}

% Make csv in question
%\usepackage{filecontents}
\begin{filecontents*}{scientists2.csv}
name,surname,
Albert,Einstein,
Marie,Curie,
Thomas,Edison,
\end{filecontents*}

\begin{document}

\begin{center}
    \csvreader[tabular=|l|l|,
        table head=\hline \textbf{ First Name} & \textbf{Last Name} \\\hline,
        late after line = \\\hline]%
    {scientists2.csv}{name=\name,surname=\surname}%
    {\name & \surname}%
\end{center}


\DTLloaddb{myDB}{scientists2.csv}
%\DTLdisplaydb{myDB}% Useful for debugging.

\begin{center}
\begin{tabular}{ll}\toprule
    \textbf{First Name} & \textbf{Last Name}%
    \DTLforeach*{myDB}{\Name=name,\Surname=surname}{%
        \DTLiffirstrow{\\\cmidrule{1-2}}{\\}%
        \Name & \Surname
    }%
    \\\bottomrule
\end{tabular}
\end{center}
\end{document} 
3
  • Also, why does Thomas Edison not show up when this code is run? Thanks for help with a new user!
    – mike
    Nov 11, 2012 at 0:35
  • Hmmm... Don't know as I have never used the csvsimple package, just really guessed at the solution (bad habit I picked up from David Carlisle :-)). I was about to do a datatool solution for you using booktabs as I know that will work, and you can get a "better" table. As the missing data seems unrelated to displaying a top border, I would recommend you should post a separate question. Nov 11, 2012 at 0:41
  • @mike: Found issue with last entry: Need a trailing , for some reason. See updated answer. Nov 11, 2012 at 1:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .