Latex can be used to create tables from external files such as MS-Excel files saved as comma separated values or similarly many other formats. Recently, I received a comment where a reader suggested that I should post a tutorial on how to create tables in Latex from external files. I searched around and found multiple ways to achieve this. I am going to talk about two of the most popular ways of doing it. First one is using pgfplotstable and the second one is using datatool package.
Specifiacally, we are going to learn the following in this tutorial:
- Using datatool package to load external files
- Usng MS-Excel files to create tables in Latex
- Using \DTLloaddb to set keys and use external files
- Using \DTLforeach to iterate the external file
- Using \pgfplotstable package
- Using \booktabs package
- Using pgfplotstabletypeset
- Styling each column of table
- Using style rules from booktabs package
The following video tutorial explains both methods of creating tables in Latex using external database files.
The code for both of the methods is given below for your reference:
The first method - suing datatool package
\documentclass{article}\usepackage{datatool}\DTLloaddb[keys={c1,c2,c3}]{ctext}{csvtext.csv}\begin{document}\begin{table}\begin{tabular}{clr}\textbf{Software} & \textbf{Manufacturer} & \textbf{Malware}\DTLforeach{ctext}{\cola=c1,\colb=c2,\colc=c3}{\\\cola & \colb & \colc}\end{tabular}\end{table}\end{document}
The second method - using pgfplotstable package
\documentclass{article}\usepackage{pgfplotstable}\usepackage{booktabs}\begin{document}\pgfplotstabletypeset[col sep=comma,columns/Software/.style={string type},columns/num1/.style={string type},columns/num2/.style={string type},every head row/.style={before row=\toprule,after row =\midrule},every last row/.style={after row=\bottomrule}]{csvtext.csv}\end{document}
I assume you can also format the table cells using pgfplotstable (e.x. ccc) and put them in table environment. Is this right?
ReplyDeleteHello! Ur videos r quite interesting. I am facing the following errors. Kindly help
ReplyDelete! Package pgfplots Error: Could not read table file 'csvtext.csv'. In case you
intended to provide inline data: maybe TeX screwed up your end-of-lines? Try `r
ow sep=crcr' and terminate your lines with `\\' (refer to the pgfplotstable man
ual for details).
See the pgfplots package documentation for explanation.
Type H for immediate help.
...
l.19 ]{csvtext.csv}
?