14

What are differences, which to use for including .csv, .tsv files ? Datatool ? pgfplotstable ?

I've seen at question : How typeset a numeric table from a CSV file

how person asked about fixing datatool issue, and answer was to use pgfplotstable

Are there other options for including tabular data into documents ?

What are advantages and disadvantages of possible choices ?

1
  • I've added a datatool solution to the linked question.
    – Alan Munn
    Commented Feb 13, 2012 at 14:08

2 Answers 2

10

Both datatool and pgfplotstable can do similar things, but datatool is designed as a more general tool and therefore can in principle do more things (e.g. form letters and the like). So if your requirements are simply pretty printing tables directly from CSV files I might prefer pgfplotstables since it is especially designed for that, and if you are independently used to using TiKZ, has a nice key-value syntax that is very easy to use. If you have more complicated things you might need to do with your CSV data, then datatool might be a better choice.

9

Maybe you want to give LuaLaTeX a try. It's very easy to write a script to read external files and format LaTeX commands. It is good practice to write the lua functions in a separate file with the extension .lua. For this MWE I use the filecontents environment instead to provide an extra file for the lua script and the datafile.

Here is an other example for reading csv files with LuaLaTeX: https://tex.stackexchange.com/a/41499/10570.

\documentclass{book}
\usepackage{filecontents}

%create a datafile
\begin{filecontents*}{datafile.csv}
30.0,   0.0,    0.0
60.0,   1.9098, 5.8779
90.0,   6.9098, 9.5106
120.0,  13.09,  9.5106
150.0,  18.09,  5.8779
180.0,  20.0,   0.0
\end{filecontents*}

%create a lua script file
\begin{filecontents*}{luaFunctions.lua}
 function readDataFile()

    local input = io.open('datafile.csv', 'r')
    dataTable = {} --global table for storing the read values

    for line in input:lines() do

        --split the line with the comma delimiter 
        local split = string.explode(line, ",")

        --save the arguments in variables
        tableItem = {}
        tableItem.arg1 = split[1]
        tableItem.arg2 = split[2]
        tableItem.arg3 = split[3]

        --insert the arguments of one line in the table
        table.insert(dataTable, tableItem)
    end

    input:close()
 end


function printTable()
    tex.print(string.format("\\begin{tabular}{c|c|c}"))
    tex.print(string.format("Column 1 & Column 2 & Column 3\\\\\\hline"))

    --create a latex string for every table entry
    for i,p in ipairs(dataTable) do
        tex.print(string.format(" {%s} & {%s} & {%s} \\\\",p.arg1, p.arg2, p.arg3))
    end

    tex.print(string.format("\\end{tabular}"))
end
\end{filecontents*}


% read the external lua file to declare the defined functions,
% but without execute the Lua commands and functions
\directlua{dofile("luaFunctions.lua")}

% latex commands to execute the lua functions
\def\readDataFile{\directlua{readDataFile()}}
\def\printTable{\directlua{printTable()}}

\begin{document}
\readDataFile
\printTable
\end{document}
1
  • In future I am planning to try LuaLaTeX as I've programmed in past in Lua and liked this scripting language a lot ! Currently I am working with book, with many custom typesettings tailored for pdflatex. So not now, but in future - I'd love to. Your example will be one of my starting points :). Commented Feb 12, 2012 at 15:16

You must log in to answer this question.

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