Besides various online services and scripts, there are several LaTeX packages that generate tables directly from CSV (comma separated value) files. The advantage is everything is in the tex-file in one place and one doesn’t have to switch back and forth between website/script and LaTeX when changes are made. The drawback clearly is their limited flexibility or high complexity for sophisticated tables.
Note, I used the following few lines of code to generate a simple CSV file using LaTeX. All examples assume the existence of the file “scientists.csv”. Just copy the code into a tex-file and typeset.
\documentclass{minimal} \begin{filecontents*}{scientists.csv} name,surname,age Albert,Einstein,133 Marie,Curie,145 Thomas,Edison,165 \end{filecontents*}
The star suppresses additional information on the file creation from being added.
Let’s start with a simple package.
Package csvsimple
Here is a very basic example. Elements of the first row are considered column titles. To separate them from the table content, a horizontal line is automatically added in between.
\documentclass{article} \usepackage{csvsimple} \begin{document} \csvautotabular{scientists.csv} \end{document}
The command csvreader
provides a better control of the column style, titles and content, but makes things slightly more complicated.
Here is an example:
\documentclass{article} \usepackage{csvsimple} \begin{document} \csvreader[tabular=|l|l|c|, table head=\hline & Name & Age\\\hline, late after line=\\\hline]% {scientists.csv}{name=\name,surname=\surname,age=\age}% {\thecsvrow & \surname~\name & \age}% \end{document}
The first part of the optional argument controls the alignment of the content per column (tabular
). The second sets the column titles and adds a horizontal line before and after them (table head
). Finally, another horizontal line is added to the end of the table (late after line
). Furthermore, “commands” are defined for every column using the column title from the CSV file (name, surname and age). These commands allow reordering and combining column content. The command thecsvrow
is a row counter and therefore an easy way to enumerate the rows.
Here is the complete package documentation.
Package pgfplotstable
A more flexible package is pgfplotstable (package documentation). It allows generating tables from different data files types. However, we will only consider CSV files here.
Again, an optional argument serves to customize what the table looks like, using key-value-pairs. Here is a minimal example with the file generated earlier.
\documentclass{article} \usepackage{pgfplotstable} \begin{document} \pgfplotstabletypeset[ col sep=comma, string type, columns/name/.style={column name=Name, column type={|l}}, columns/surname/.style={column name=Surname, column type={|l}}, columns/age/.style={column name=Age, column type={|c|}}, every head row/.style={before row=\hline,after row=\hline}, every last row/.style={after row=\hline}, ]{scientists.csv} \end{document}
One of the things that makes this package interesting is, it supports multicolumn
.
\documentclass{article} \usepackage{pgfplotstable} \begin{document} \pgfplotstabletypeset[ col sep=comma, string type, every head row/.style={% before row={\hline \multicolumn{2}{c}{Full Name} & \\ }, after row=\hline }, every last row/.style={after row=\hline}, columns/name/.style={column name=Name, column type=l}, columns/surname/.style={column name=Surname, column type=l}, columns/age/.style={column name=Age, column type=c}, ]{scientists.csv} \end{document}
Furthermore, the package allows generation of multi-page tables with longtable (package documentation) with pgfplotstable
(see the package documentation for more details).
The csvsimple as well as the pgfplotstable package documentations are both comprehensive and very nicely formatted. It seems, the authors put in quite a bit of effort writing them. Check it out!
Further packages and other approaches
Other packages that I will not discuss here include:
- datatool
- csvtools (obsolete, replaced by datatool)
- Excel and LaTeX by Gregor Gorjanc
- R and the xtable package
Rafael
Thanks, I will probably use this soon. I guess you did not find anything that supports booktabs tables?
tom
Have a look at the pgfplotstable documentation, they are using booktabs for almost all their examples.
Diaa
Great work as usual !
However, what about making a Facebook page containing all the new topics you frequently update the blog with ?
I think it will make your efforts more renowned.
Thanks
tom
Thanks for the tip. That’s actually a good idea. I’ll try to set something up. Best, Tom.
davclark (@davclark)
I think you have much more complexity than needed for a basic pgfplotstable example… refer to the documentation of the package for a one-liner.
tom
Thanks for your comment. The code could be simplified, I agree. I wanted to show how to produce a reasonable
pgfplotstable
(column alignment, horizontal lines, etc.). It’s unfortunate I wrote a simple example is complicated. Besides that, it is a one-liner :-).hannarud
I found the article very useful, thank you! It’s my first time to see texblog.org, but I like it already!
Just one small contribution: concerning 2nd example for csvsimple – there’s a mistake
you probably need to choose: either \surename or \firstname or you get a mistake. It took me 20 minutes to understand the matter cause I’m not familiar with csvsimple so far. So probably it would be better to correct it.
Thank you very much for your work!
tom
Hi Hanna,
Thanks very much for your comment, you are right. Sorry for that, it is fixed now.
Tom.
Felipo
the link to datatool is broken, this is the correct one:
http://www.ctan.org/pkg/datatool
tom
Hi Felipo,
Thanks for your comment. I fixed it.
Best, Tom
Chandan Gautam
Thanks for thhis post… it has saved my day…