Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 ?

share|improve this question
    
I've added a datatool solution to the linked question. –  Alan Munn Feb 13 '12 at 14:08
add comment

2 Answers

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: http://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}
share|improve this answer
    
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 :). –  Grzegorz Wierzowiecki Feb 12 '12 at 15:16
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.