| 1 |
% This file is embedded in datatool-user.pdf version 3.4.3 2025-12-04 |
| 2 |
% Example 78 Display Data in a Table with an Extra Column |
| 3 |
% Label: "ex:displaydbpostrow" |
| 4 |
% arara: pdflatex |
| 5 |
% arara: pdfcrop |
| 6 |
\documentclass[12pt]{article} |
| 7 |
\pagestyle{empty} |
| 8 |
\usepackage{booktabs} |
| 9 |
\usepackage{datatool} |
| 10 |
\DTLsetup{store-datum,default-name=products} |
| 11 |
% define database: |
| 12 |
\DTLaction{new} |
| 13 |
% add columns in desired order: |
| 14 |
\DTLaction[key=Title]{add column} |
| 15 |
\DTLaction[key=Author]{add column} |
| 16 |
\DTLaction[key=Format]{add column} |
| 17 |
\DTLaction[key=Quantity]{add column} |
| 18 |
\DTLaction[key=Price,value={Price (\$)}]{add column} |
| 19 |
% 1st row: |
| 20 |
\DTLaction[ |
| 21 |
assign={ |
| 22 |
Title = {The Adventures of Duck and Goose}, |
| 23 |
Author = {Sir Quackalot}, |
| 24 |
Format = paperback, |
| 25 |
Quantity = 3, Price = {10.99} |
| 26 |
} |
| 27 |
]{new row} |
| 28 |
% 2nd row: |
| 29 |
\DTLaction[ |
| 30 |
assign={ |
| 31 |
Title = {The Return of Duck and Goose}, |
| 32 |
Author = {Sir Quackalot}, |
| 33 |
Format = paperback, |
| 34 |
Quantity = 5, Price = {19.99} |
| 35 |
} |
| 36 |
]{new row} |
| 37 |
% 3rd row: |
| 38 |
\DTLaction[ |
| 39 |
assign={ |
| 40 |
Title = {More Fun with Duck and Goose}, |
| 41 |
Author = {Sir Quackalot}, |
| 42 |
Format = paperback, |
| 43 |
Quantity = 1, Price = {12.99} |
| 44 |
} |
| 45 |
]{new row} |
| 46 |
% 4th row: |
| 47 |
\DTLaction[ |
| 48 |
assign={ |
| 49 |
Title = {Duck and Goose on Holiday}, |
| 50 |
Author = {Sir Quackalot}, |
| 51 |
Format = paperback, |
| 52 |
Quantity = 3, Price = {11.99} |
| 53 |
} |
| 54 |
]{new row} |
| 55 |
% 5th row: |
| 56 |
\DTLaction[ |
| 57 |
assign={ |
| 58 |
Title = {The Return of Duck and Goose}, |
| 59 |
Author = {Sir Quackalot}, |
| 60 |
Format = hardback, |
| 61 |
Quantity = 3, Price = {19.99} |
| 62 |
} |
| 63 |
]{new row} |
| 64 |
% 6th row: |
| 65 |
\DTLaction[ |
| 66 |
assign={ |
| 67 |
Title = {The Adventures of Duck and Goose}, |
| 68 |
Author = {Sir Quackalot}, |
| 69 |
Format = hardback, |
| 70 |
Quantity = 9, Price = {18.99} |
| 71 |
} |
| 72 |
]{new row} |
| 73 |
% 7th row: |
| 74 |
\DTLaction[ |
| 75 |
assign={ |
| 76 |
Title = {My Friend is a Duck}, |
| 77 |
Author = {A. Parrot}, |
| 78 |
Format = paperback, |
| 79 |
Quantity = 20, Price = {14.99} |
| 80 |
} |
| 81 |
]{new row} |
| 82 |
% 8th row: |
| 83 |
\DTLaction[ |
| 84 |
assign={ |
| 85 |
Title = {Annotated Notes on the ‘Duck and Goose’ chronicles}, |
| 86 |
Author = {Prof Macaw}, |
| 87 |
Format = ebook, |
| 88 |
Quantity = 10, Price = {8.99} |
| 89 |
} |
| 90 |
]{new row} |
| 91 |
% 9th row: |
| 92 |
\DTLaction[ |
| 93 |
assign={ |
| 94 |
Title = {‘Duck and Goose’ Cheat Sheet for Students}, |
| 95 |
Author = {Polly Parrot}, |
| 96 |
Format = ebook, |
| 97 |
Quantity = 50, Price = {5.99} |
| 98 |
} |
| 99 |
]{new row} |
| 100 |
% 10th row: |
| 101 |
\DTLaction[ |
| 102 |
assign={ |
| 103 |
Title = {‘Duck and Goose’: an allegory for modern times?}, |
| 104 |
Author = {Bor Ing}, |
| 105 |
Format = hardback, |
| 106 |
Quantity = 0, Price = {59.99} |
| 107 |
} |
| 108 |
]{new row} |
| 109 |
% 11th row: |
| 110 |
\DTLaction[ |
| 111 |
assign={ |
| 112 |
Title = {Oh No! The Chickens have Escaped!}, |
| 113 |
Author = {Dickie Duck}, |
| 114 |
Format = ebook, |
| 115 |
Quantity = 11, Price = {2.0} |
| 116 |
} |
| 117 |
]{new row} |
| 118 |
|
| 119 |
\newcommand{\productappendtotal}[2]{% |
| 120 |
\DTLaction[ |
| 121 |
return={ \theQuantity=Quantity, \thePrice=Price } |
| 122 |
]{current row values}% |
| 123 |
\DTLmul{\theTotal}{\theQuantity}{\thePrice}% |
| 124 |
\DTLround{\theTotal}{\theTotal}{2}% |
| 125 |
\appto#1{&}% |
| 126 |
\eappto#1{\expandonce\theTotal}% |
| 127 |
\DTLadd{\runningtotal}{\runningtotal}{\theTotal}% |
| 128 |
} |
| 129 |
\begin{document} |
| 130 |
\DTLaction[ |
| 131 |
options={ |
| 132 |
only-keys={Title,Quantity,Price}, |
| 133 |
pre-head={\toprule}, |
| 134 |
after-head={\midrule}, |
| 135 |
align-specs={lrrr}, |
| 136 |
post-head={& \dtlcolumnheader{c}{Total}}, |
| 137 |
init={\def\runningtotal{0}}, |
| 138 |
post-row-function=\productappendtotal, |
| 139 |
foot={\midrule & & & \runningtotal} |
| 140 |
} |
| 141 |
]{display} |
| 142 |
\end{document} |