# 2. ⁑Managing Data

Many of the topics covered in this book have examples that fetch row-and-column style data from an external source, such as in a spreadsheet or structured query language (SQL) database. For example, in §3 Correspondence you may want to pull the recipient's details from a database or you may want to send a template letter to everyone listed in a spreadsheet (mail merging).

This chapter covers accessing data from a CSV file or spreadsheet or from a MySQL database (the most popular open source database [61]). The MySQL Community Edition is freely downloadable, but there are also commercial versions available. The free version can be fetched from http://dev.mysql.com/downloads/mysql/. In addition, this chapter also includes sections on topics that aren't specific to databases, but describe useful utility commands that are used in some of the examples and exercises throughout this book.

Some of the topics covered in this chapter are quite advanced. If you don't need to fetch data from an external source — for example if you want to write a letter to someone but intend to explicitly write the recipient's address in your .tex file — then you can omit this chapter and skip ahead to §3 Correspondence.

There are a number of packages and applications available on CTAN that can be used to fetch data, see the “Data Import” topic. This book covers the datatool bundle (available on modern TeX distributions such as TeX Live and MiKTeX1) and the datatooltk Java application (the installer and source are available on CTAN). This book assumes you have the latest version of datatool installed (version 2.23 at time of writing). If you want to try any of the datatooltk examples here, ensure you are using the latest version (currently 1.6).

If you are used to writing programming languages, take care with TeX. Although TeX is Turing-complete, it is very different to most programming languages. (In fact, it's a document formatting language rather than a programming language.) In, say, C or Java the source code is a series of assignments or function calls or commands. Data is explicitly assigned to variables. In TeX the source code consists of data interspersed with control sequences.2 Whitespace forms part of the data (except at the start of lines or after control words). In most programming languages foo( bar ) is equivalent to foo(bar) but in TeX (and LaTeX) \foo{ bar } is usually not the same as \foo{bar}.3 This is because the entire contents of the argument is data not a variable or list of variables. You may not like or agree with TeX's unusual syntax, but no one has so far produced a viable alternative to TeX that has the typesetting power and flexibility of TeX but the structured coding of popular programming languages (although LuaTeX comes close). Unless told otherwise, assume that spaces aren't ignored.

#### Footnotes

...TeX1
TeX Live is included in the MacTeX distribution, and MiKTeX is included in the ProTeXt distribution.
... sequences.2
Although LaTeX adds the restriction that document text is only permitted within the document environment.
... \foo{bar}.3
Try doing \begin{ document } instead of \begin{document} in a LaTeX document.

This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-07-4).

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