2

I would like to use the spreadtab package in combination with datatool to make calculations within a table. The data is coming from an external file; here is an example (scores.csv):

FirstName,Surname,Score
John,"Smith, Jr",68
Jane,Brown,75
Andy,Brown,42
Z\"oe,Adams,52

This is the tex file:

\documentclass[a4paper, 11pt]{scrartcl}

\usepackage{datatool}
\usepackage[utf8]{inputenc}
\usepackage{spreadtab,numprint,fp}

\DTLloaddb[keys={Firstname,Lastname,Score}]{scores}{scores.csv}

\begin{document}
    \begin{spreadtab}{{tabular}{llr}}
        \hline
            @First Name & @Surname & @Score (\%) \\\hline%
            \DTLforeach{scores}{%
                \firstname=Firstname,\surname=Lastname,\score=Score}{%
                \firstname & \surname & \score \\}
        @Average & & sum(c1) \\
        \hline
    \end{spreadtab}
\end{document}

Unfortunately, the calculations do not work. When I try to change sum(c1) to sum(c1:c5), the compilation results in an error. When I use STdebug it shows that the resulting table has only two rows: the first containing the DTLforeach code and the second is the one used for calculating the average score. My guess is that spreadtab tries to evaluate too early and therefore stumbles over code that has not run yet. Not sure though, I'm a novice in LaTeX.

I am aware that datatool can make calculations, too, but spreadtab is more flexible and provides what I need in my use case (i.e. referencing values from other columns and rows etc.).

Any help is greatly appreciated.

1
  • 1
    Same issue with csvsimple. :-( Seems like spradtab misses some expand-before-evaluation macro.
    – Morty
    Commented Jun 9, 2020 at 8:53

1 Answer 1

1

I worked around this issue by using a script that reads the data from the external file and builds the .tex file from the data. I was not able to make datatool and spreadtab work together.

You must log in to answer this question.

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