About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Smile for the Camera: a new cybercrime short story ebook.

Exercise 2: Iterating Through Data (Solution)

This is a solution to Exercise 2. You need the sample file booklist.csv. Alternatively, if you want to use data from the samples SQL database, remember to use datatooltk to import the data to a .dbtex file and load it using \DTLloaddbtex as described in Loading Data From a .dbtex File.

Note that two LaTeX runs are required for the cross-references to be up-to-date:

% arara: pdflatex
% arara: pdflatex
\documentclass[captions=tableabove]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{datatool}

% Load data from booklist.csv:
\DTLloaddb{books}{booklist.csv}

\begin{document}
Hardback books are listed in Table~\ref{tab:hardback}, paperback
books are listed in Table~\ref{tab:paperback} and ebooks are listed
in Table~\ref{tab:ebook}.

\begin{table}[htbp]
 \caption{Hardback Books}
 \label{tab:hardback}
 \centering
 \begin{tabular}{rll}
 \multicolumn{1}{c}{\bfseries Id} &
 \multicolumn{1}{c}{\bfseries Author} &
 \multicolumn{1}{c}{\bfseries Title}%
 \DTLforeach*[\equal{\Format}{hardback}]{books}%
 {\Id=id,\Format=format,\Title=title,\Author=author}%
 {\\\Id & \Author & \Title}%
 \end{tabular}
\end{table}

\begin{table}[htbp]
 \caption{Paperback Books}
 \label{tab:paperback}
 \centering
 \begin{tabular}{rll}
 \multicolumn{1}{c}{\bfseries Id} &
 \multicolumn{1}{c}{\bfseries Author} &
 \multicolumn{1}{c}{\bfseries Title}%
 \DTLforeach*[\equal{\Format}{paperback}]{books}%
 {\Id=id,\Format=format,\Title=title,\Author=author}%
 {\\\Id & \Author & \Title}%
 \end{tabular}
\end{table}

\begin{table}[htbp]
 \caption{Ebooks}
 \label{tab:ebook}
 \centering
 \begin{tabular}{rll}
 \multicolumn{1}{c}{\bfseries Id} &
 \multicolumn{1}{c}{\bfseries Author} &
 \multicolumn{1}{c}{\bfseries Title}%
 \DTLforeach*[\equal{\Format}{ebook}]{books}%
 {\Id=id,\Format=format,\Title=title,\Author=author}%
 {\\\Id & \Author & \Title}%
 \end{tabular}
\end{table}

\end{document}

Download dtlforeach.tex or dtlforeach.pdf.

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs