# Weird error when using datatool's \DTLaddall in a \DTLforeach

Given test.csv containing:

donor,core,non-core
Norway,137.6,111.4
The Netherlands,116.6,85.8

and data-problem.tex containing:

\documentclass{article}
\usepackage{datatool}
\begin{document}

%\tracingmacros=1
\DTLforeach{contrib}{\contribC=core,\contribNC=non-core}{%

\end{document}

I get the error:

(e:\Documents\Anthropology\Paper\data-problem.aux) ( FP-EQ? ) ( FP-ADD )
Runaway argument?
\relax \fi \ifnum \@dtl@tmpcount
\par
l.11

[1] (e:\Documents\Anthropology\Paper\data-problem.aux) )
(\end occurred when \ifx on line 10 was incomplete)
(\end occurred when \iftrue on line 10 was incomplete)
(\end occurred when \ifnum on line 10 was incomplete)
(\end occurred when \ifnum on line 10 was incomplete)
(\end occurred when \ifnum on line 10 was incomplete)
(see the transcript file for additional information)
Output written on data-problem.dvi (1 page, 216 bytes).
SyncTeX written on data-problem.synctex.gz
Transcript written on data-problem.log.

LaTeX exited abnormally with code 1 at Thu Dec 16 15:33:33

This doesn't happen with \DTLadd{\contribT}{\contribC}{\contribNC}, though, nor does it happen with:

\def\foo{1.2}
\def\bar{3.4}
\DTLaddall{\baz}{\foo,\bar}

So I'm at a bit of a loss as to what is going wrong here...

-
I am not sure if you are using the fp package for calculations but the error seems to come from there. Can you post a minimal? – Yiannis Lazarides Dec 17 '10 at 12:26
@Yiannis: That's what the two files listed above were supposed to be; I trimmed out almost everything that I could see was not needed to produce the error... the only things I left that are (presumably) not needed are comments, whitespace, and the third line of the .csv file. – SamB Dec 17 '10 at 17:24
It seems I read too quickly! Sorry! I have posted an answer. – Yiannis Lazarides Dec 18 '10 at 0:09

Datatool doesn't always work perfectly.

In short, your example should work, but my guess is that \DTLaddall and \DTLgaddall don't like how the \DTLforeach loop or the datatool package is making those number. If you use a different macro with a number in it, it seems to work. No matter what I do, I can't make datatool accept a number made from the database.

I know that's not an answer, but I will give you three holiday presents:

1) I've been burned by datatool in the past. It's not remotely bug free, so from my perspective, it's likely a bug.

2) pgf's foreach and math functions can do what you want very easily and you might consider those. You can use datatool to get the csv data and use pgf to walk through it. If you want, I can post an example.

3) I think you already know this, but you can accomplish totaling everything within the database by doing the following:

\documentclass{article}
\usepackage{datatool}

\begin{document}
\def\mynum{0}

%\tracingmacros=1
\DTLforeach{contrib}{\contribD=donor,\contribC=core,\contribNC=noncore}{%
%
}

\mynum
\end{document}


Hopefully this helps. Let me know if it doesn't. Again, I understand that I'm not answering your question (other than that I think datatool is broken), but hopefully I solved your problem.

Also, btw, the author does make patches and she did fix it for me when I asked her to.

-
Really, at this point I was just wondering if it was a bug in the package, or if I had missed something in the manual that would have told me that this kind of thing could happen. So this is good. – SamB Dec 17 '10 at 23:02
@SamB: Nope. You're smart. For example, if you take the same numbers and store all of them in macros and then use the same \DTaddall macro, you won't get an error. No matter what you do, even if you copy those numbers into another macro, you'll get an error. There is something being left behind that is horking \DTaddall. – Andrew Starks Dec 18 '10 at 14:34

Seems like a conflict in the marriage between datatool and the fp package. You can try and use the fp package directly (it is loaded by datatool automatically). This works.

\documentclass{article}
\usepackage{datatool}
\begin{document}

%\tracingmacros=1

\DTLforeach{contrib}{\contribC=core,\contribNC=non-core}{%
\def\a{\contribC}
\def\b{\contribNC}

The \FPadd takes three arguments the first of which is a macro to receive the results and the next two the numbers to add. You can use any combination you wish.
Actually, even \DTLadd works -- it's only \DTLaddall that fails, and even that only on the loop macros from a \DTLforeach. But, er, what are \a and \b for? I didn't need those with \DTLadd... – SamB Dec 18 '10 at 18:42
@SamB You don't actually need the \a or \b, they were just shortcuts that I typed as I tried quite a few of the FP commands and didn't want to keep on typing long strings. For example FPmul will multiply FPdiv divide, there is also a power function.\FPadd{\result}{\a}{\a} will totalize. \result is defined on the fly, you can use any valid name. etools is a great package but it needs some better examples. – Yiannis Lazarides Dec 18 '10 at 18:49