Debian Codegraph @

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

raw
1 % This file is embedded in datatool-user.pdf version 3.4.3 2025-12-04
2 % Example 85 Displaying Data with Calculations, Filtering and Row Highlighting
3 % Label: "ex:displaydbaverages"
4 % arara: pdflatex
5 % arara: pdfcrop
6 \documentclass[12pt]{article}
7 \pagestyle{empty}
8 \begin{filecontents}[noheader,overwrite]{studentmarks.csv}
9 Surname,Forename,StudentNo,Assign1,Assign2,Assign3
10 "Smith, Jr",John,102689,68,57,72
11 "Brown",Jane,102647,75,84,80
12 "Brown",Jane,102646,64,92,79
13 "Brown",Andy,103569,42,52,54
14 "Adams",Zoƫ,105987,52,48,57
15 "Brady",Roger,106872,68,60,62
16 "Verdon",Clare,104356,45,50,48
17 \end{filecontents}
18
19 \usepackage{colortbl}
20 \usepackage{datatool}
21 \DTLsetup{store-datum,default-name=marks}
22 \DTLread{studentmarks.csv}
23 \ExplSyntaxOn
24 \RenewDocumentCommand \DTLdisplaydbAddItem
25 { m m m m m m m m }
26 {
27 \int_compare:nNnTF
28 { #7 } = { \c_one_int }
29 {
30 % insert highlight if average greater than 70
31 \fp_compare:nNnT
32 { \DTLdatumvalue \AverageScore } > { 70 }
33 {
34 \tl_put_right:Nn #1 { \rowcolor {yellow} }
35 }
36 \DTLaction[ return={\l_tmpa_tl=Forename} ]
37 { current ~ row ~ values }
38 \datatool_if_null_or_empty:NTF \l_tmpa_tl
39 {
40 \tl_put_right:Nn #1 { #3 { #2 } }
41 }
42 {
43 \tl_put_right:Nx #1
44 { \exp_not:N #3 { \exp_not:n { #2 }, ~ \exp_not:V \l_tmpa_tl } }
45 }
46 }
47 {
48 \tl_put_right:Nn #1 { #3 { #2 } }
49 }
50 }
51 \ExplSyntaxOff
52
53 \newcommand{\rowfilter}[3]{%
54 \DTLaction[
55 options={mean},datum={round=1},
56 keys={Assign1-},
57 return={\AverageScore=mean}
58 ]{current row aggregate}% calculate average
59 \DTLifnumlt{\AverageScore}{50}%
60 {}% skip if average less than 50
61 {#3}% include this row otherwise
62 }
63
64 \newcommand{\appendaverage}[2]{%
65 \appto#1{&}%
66 \eappto#1{\expandonce\AverageScore}%
67 }
68 \begin{document}
69 \DTLaction[
70 options={
71 only-keys={Surname,StudentNo},
72 align-specs={lrr},
73 post-row-function={\appendaverage},
74 row-condition-function={\rowfilter},
75 header-row={Name & Reg.\ No. & Average}
76 }
77 ]{display}
78 \end{document}