2

I'm trying to build a table with some calculations from an external file. data.csv:

From,To,Task
10:00,11:00,Task1
11:00,13:13,Task2

I'm using datatool to read the csv and it's foreach to output the data. However I'm stuck when it comes to calculate the time difference 10:00 - 11:00 (=60min). I have the macro \dur but it does not work within foreach -.-

here is my tex code:

\documentclass{article}

\usepackage{datatool}
\usepackage{fp}

\DTLloaddb{data}{data.csv}

\def\dur#1#2{\the\numexpr(\xdur#2\relax)-(\xdur#1\relax)\relax\ }
\def\xdur#1:#2\relax{(#1)*60+#2}

\begin{document}

Here dur works:
\dur{10:00}{13:30} \par

Here dur does not work:
\DTLforeach{data}{\from=From,\to=To}{\from--\to=\dur{\from}{\to} \par}

\end{document}

So my question is how to create a macro to compute the time difference within the foreach. And i'd like also to sum up the result.

(I already succeeded summing up read in values by using fp like this: \DTLforeach{scores}{\score=Score}{\FPeval\result{clip(\result+\score)}}, but I need the duration as varable to pass it in there)

2

Calling \xdur\from won't work, because TeX doesn't expand tokens when the arguments to \xdur are looked for.

You have to expand the argument before \xdur is expanded. When the times are given explicitly, \expandafter will do nothing, but no harm either.

\begin{filecontents*}{\jobname.csv}
From,To,Task
10:00,11:00,Task1
11:00,13:13,Task2
\end{filecontents*}

\documentclass{article}

\usepackage{datatool}

\DTLloaddb{data}{\jobname.csv}

\def\dur#1#2{%
  \the\numexpr\expandafter\xdur#2\relax-\expandafter\xdur#1\relax\relax
}
\def\xdur#1:#2\relax{(#1)*60+#2}

\begin{document}

Here dur works:
\dur{10:00}{13:30} \par

Here too:
\DTLforeach{data}{\from=From,\to=To}{\from--\to=\dur{\from}{\to} \par}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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