Debian Codegraph @

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

raw
1 % This file is embedded in datatool-user.pdf version 3.4.3 2025-12-04
2 % Example 93 Loops and Alignment
3 % Label: "ex:constructtab"
4 % arara: pdflatex
5 % arara: pdfcrop
6 \documentclass[12pt]{article}
7 \pagestyle{empty}
8 % sample CSV file:
9 \begin{filecontents}[noheader,overwrite]{studentmarks.csv}
10 Surname,Forename,StudentNo,Assign1,Assign2,Assign3
11 "Smith, Jr",John,102689,68,57,72
12 "Brown",Jane,102647,75,84,80
13 "Brown",Jane,102646,64,92,79
14 "Brown",Andy,103569,42,52,54
15 "Adams",Zoƫ,105987,52,48,57
16 "Brady",Roger,106872,68,60,62
17 "Verdon",Clare,104356,45,50,48
18 \end{filecontents}
19
20 \usepackage{colortbl}
21 \usepackage{datatool}
22
23 % Load data from studentmarks.csv file:
24 \DTLsetup{store-datum,default-name=marks}
25 \DTLread{studentmarks.csv}
26 % Commands to construct the tabular content:
27 \ExplSyntaxOn
28 % define variables:
29 \int_new:N \l_my_row_int
30 \tl_new:N \l_my_content_tl
31 \tl_new:N \l_my_forename_tl
32 \tl_new:N \l_my_surname_tl
33 \tl_new:N \l_my_mean_tl
34 % command to initialise:
35 \cs_new:Nn \my_init_content:
36 {
37 \tl_set:Nn \l_my_content_tl { \begin{tabular} { lr } }
38 \int_zero:N \l_my_row_int
39 }
40 % command to finish off:
41 \cs_new:Nn \my_finish_content:
42 {
43 \tl_put_right:Nn \l_my_content_tl { \end{tabular} }
44 }
45 % command to append a row:
46 \cs_new:Nn \my_add_row:nn
47 {
48 \fp_compare:nNnF { #2 } < { 50 }
49 { \int_if_zero:nF { \l_my_row_int }
50 {
51 \tl_put_right:Nn \l_my_content_tl { \\ }
52 }
53 \int_incr:N \l_my_row_int
54 \fp_compare:nNnT { #2 } > { 70 }
55 {
56 \tl_put_right:Nn \l_my_content_tl { \rowcolor { yellow } }
57 }
58 \tl_put_right:Nx \l_my_content_tl { #1 & #2 }
59 }
60 }
61 % define document command:
62 \NewDocumentCommand { \meanscorestab } { }
63 {
64 % construct tabular:
65 \my_init_content:
66 % iterate over the default database:
67 \DTLmapdata
68 {
69 % get the surname and forename for the current row:
70 \DTLmapgetvalues
71 {
72 \l_my_surname_tl = Surname ,
73 \l_my_forename_tl = Forename
74 }
75 % calculate the mean:
76 \DTLaction
77 [
78 keys={Assign1-},
79 datum={round=1},
80 return={ \l_my_mean_tl = mean },
81 options=mean
82 ]
83 {row ~ aggregate}
84 \my_add_row:nn
85 { \l_my_forename_tl \c_space_tl \l_my_surname_tl }
86 { \l_my_mean_tl }
87 }
88 % finish construction:
89 \my_finish_content: % expand the content:
90 \l_my_content_tl }
91 \ExplSyntaxOff
92 \begin{document}
93 \meanscorestab
94 \end{document}