Debian Codegraph @

texmf-dist/doc/latex/datatool/datatool-user-examples/datatool-user-example079.tex

raw
1 % This file is embedded in datatool-user.pdf version 3.4.3 2025-12-04
2 % Example 79 Adjusting the Item Hook to Calculate Totals and Show Negative Numbers in Red
3 % Label: "ex:balancesheet"
4 % arara: pdflatex
5 % arara: pdfcrop
6 \documentclass[12pt]{article}
7 \pagestyle{empty}
8 \begin{filecontents}[noheader,overwrite]{balance.csv}
9 Description,In,Out,Balance
10 Travel expenses,,230,-230
11 Conference fees,,400,-630
12 Grant,700,,70
13 Train fare,,70,0
14 \end{filecontents}
15 \usepackage{booktabs}
16 \usepackage{color}
17 \usepackage{datatool}
18
19 \DTLsetup{store-datum,default-name=balance}
20
21 \DTLread[
22 headers={
23 Description,
24 in (\pounds),
25 Out (\pounds),
26 Balance (\pounds)
27 }
28 ]{balance.csv}
29
30 \newcommand{\theInTotal}{0}
31 \newcommand{\theOutTotal}{0}
32 \RenewDocumentCommand \DTLdisplaydbAddItem { m m m m m m m m }
33 {%
34 \DTLifnullorempty{#2}{}% skip null or empty
35 {%
36 \dtlifnumeq{#8}{4}% balance column
37 {%
38 \DTLifnumlt{#2}{0}{\appto#1{\color{red}}}{}%
39 }%
40 {%
41 \dtlifnumeq{#8}{2}% in column
42 {\DTLadd{\theInTotal}{\theInTotal}{#2}}%
43 {%
44 \dtlifnumeq{#8}{3}{\DTLadd{\theOutTotal}{\theOutTotal}{#2}}{}% out column
45 }%
46 }%
47 \appto#1{#3{#2}}%
48 }%
49 }
50 \begin{document}
51 \DTLaction[
52 options={
53 after-head={\midrule},
54 foot={\midrule Totals & \theInTotal & \theOutTotal &}
55 }
56 ]{display}
57 \end{document}