% This file is embedded in datatool-user.pdf version 3.4.2 2025-11-06
% Example 91 Using \DTLforeach to Display Data in a Table with a Running Total Column
% Label: "ex:foreachextracol"
% arara: pdflatex
% arara: pdfcrop
\documentclass[12pt]{article}
\pagestyle{empty}
\usepackage{booktabs}
\usepackage{datatool}
\DTLsetup{store-datum,default-name=products}
% define database:
\DTLaction{new}
% add columns in desired order:
\DTLaction[key=Title]{add column}
\DTLaction[key=Author]{add column}
\DTLaction[key=Format]{add column}
\DTLaction[key=Quantity]{add column}
\DTLaction[key=Price,value={Price (\$)}]{add column}
% 1st row:
\DTLaction[
 assign={
  Title = {The Adventures of Duck and Goose},
  Author = {Sir Quackalot},
  Format = paperback,
  Quantity = 3, Price = {10.99}
 }
]{new row}
% 2nd row:
\DTLaction[
 assign={
  Title = {The Return of Duck and Goose},
  Author = {Sir Quackalot},
  Format = paperback,
  Quantity = 5, Price = {19.99}
 }
]{new row}
% 3rd row:
\DTLaction[
 assign={
  Title = {More Fun with Duck and Goose},
  Author = {Sir Quackalot},
  Format = paperback,
  Quantity = 1, Price = {12.99}
 }
]{new row}
% 4th row:
\DTLaction[
 assign={
  Title = {Duck and Goose on Holiday},
  Author = {Sir Quackalot},
  Format = paperback,
  Quantity = 3, Price = {11.99}
 }
]{new row}
% 5th row:
\DTLaction[
 assign={
  Title = {The Return of Duck and Goose},
  Author = {Sir Quackalot},
  Format = hardback,
  Quantity = 3, Price = {19.99}
 }
]{new row}
% 6th row:
\DTLaction[
 assign={
  Title = {The Adventures of Duck and Goose},
  Author = {Sir Quackalot},
  Format = hardback,
  Quantity = 9, Price = {18.99}
 }
]{new row}
% 7th row:
\DTLaction[
 assign={
  Title = {My Friend is a Duck},
  Author = {A. Parrot},
  Format = paperback,
  Quantity = 20, Price = {14.99}
 }
]{new row}
% 8th row:
\DTLaction[
 assign={
  Title = {Annotated Notes on the ‘Duck and Goose’ chronicles},
  Author = {Prof Macaw},
  Format = ebook,
  Quantity = 10, Price = {8.99}
 }
]{new row}
% 9th row:
\DTLaction[
 assign={
  Title = {‘Duck and Goose’ Cheat Sheet for Students},
  Author = {Polly Parrot},
  Format = ebook,
  Quantity = 50, Price = {5.99}
 }
]{new row}
% 10th row:
\DTLaction[
 assign={
  Title = {‘Duck and Goose’: an allegory for modern times?},
  Author = {Bor Ing},
  Format = hardback,
  Quantity = 0, Price = {59.99}
 }
]{new row}
% 11th row:
\DTLaction[
 assign={
  Title = {Oh No! The Chickens have Escaped!},
  Author = {Dickie Duck},
  Format = ebook,
  Quantity = 11, Price = {2.0}
 }
]{new row} 
\begin{document}
\newcommand{\runningtotal}{0}
\begin{tabular}{lrrr}
\toprule
\bfseries Title &
\bfseries Quantity &
\bfseries Price &
\bfseries Total% 
 \DTLforeach*{products}% database
{\theTitle=Title,\theQuantity=Quantity,\thePrice=Price}% assignments
{% 
 \DTLiffirstrow{\\\midrule}{\\}\theTitle & \theQuantity & \thePrice & \DTLmul{\theTotal}{\theQuantity}{\thePrice}% 
 \DTLround{\theTotal}{\theTotal}{2}% 
 \DTLgadd{\runningtotal}{\runningtotal}{\theTotal}% 
 \theTotal }% 
\\\midrule & & & \runningtotal
\end{tabular} 
\end{document}
