Private Homepage von Hans-Georg Eßer



Links / Social


Blaue Links: intern
🔗 Rote Links: extern


Integrating LaTeX and SQL databases

Download version 0.3 [2006/04/24] now

This is LaTeXDB. The purpose of this project is to bring together LaTeX and an SQL database. By using LaTeXDB you can use SQL queries in your LaTeX document and loop over the result sets, creating tables, serial letters and other stuff. From version 0.2 on, LaTeXDB allows nesting of SQL queries. LaTeXDB is released under the GPL.


Download version 0.3 / Release Notes   
First try

Workshop (new page)
Reference (n.a.)

     Dear ##Title ##Lastname,

     thanks for ordering ##Quant items of ##Product. 
     I'll ship it to your address in ##Town when I 
     find the time.

     Best regards,


[2012/09/03] Problems on Mac OS? Try the following: change

install -o root -g root -m 755 -v code/* $TARGET
install -o root -g root -m 644 -v doc/latexdb.1 ${MANTARGET}man1/
in the "install" script to
install -o 0 -g 0 -m 755 -v code/* $TARGET
install -o 0 -g 0 -m 644 -v doc/latexdb.1 ${MANTARGET}man1/
-- it seems that the "install" command on Mac OS does not recognize user and group names, only their IDs.

[2007/11/29] Robin Höns picked up the LaTeXDB concept and created a similar program called ratexdb that runs on Windows and connects to ODBC data sources. He wrote his program in Ruby.

[2007/06/14] LaTeXDB has been mentioned in a German book (Anselm Lingnau: "LaTeX Hacks", O'Reilly, May 2007, ISBN 978-3-89721-477-4). On five pages the LaTeXDB section describes all there is to know about the tool, including nested SQL queries.
Get the book from (Germany) and sponsor me that way ;)

[2007/03/05] Starting a new development cycle. Upcoming version 0.4 will have a few patches and some new features.

  • So far the used SQL variables (##xyz) must be prefix-free, i.e. you cannot have two SQL variables ##x and ##xy, because x is a prefix of xy. This is annoying. I consider two approaches, both of which would remain (mostly) compatibility with older LaTeXDB documents:
    - a) ##{xy} where curly brackets designate the beginning and end of the variable name (cf. bash usage of $xy vs. ${xy}) and
    - b) ##xy! where ! is an optional variable name terminator
  • Options -- allowing users to state what latexdb is to do, e.g.
    - only create tex file (don't actually run latex)
    - handle different encodings on all levels (data in SQL DB, latex code) including UTF8 support
    - interactively query user/pwd data and/or database address if \texdbconnection contains null arguments or options suggest to overwrite the values given there
  • Support for primitive databases (server-less; SQlite)
  • introducing a ~/.latexdbrc file for some personal standards
  • Remove "obsolete latex code" badnesses (sorry, I grew up with latex 2.09, anyone remember \documentstyle? ;-)
Part of the work will be getting rid of the bash wrapper script which is not really editable... Thanks to Jasper Knockaert for a lot of suggestions!
Any other suggestions are very welcome, also code contributions.

[2006/04/24] Version 0.3 released which fixes the bug described in the next entry.

[2006/03/26] Some users noted a bug where LaTeXDB will terminate with an error (and no output) when the size of a \texdbfor{} loop is too big. This is actually a stupid programming mistake causing Python to recurse too deeply. I'll exchange the recursive code with an iterative equivalent. Won't be long now ;)

[2005/10/27] There's a spanish introduction to LaTeXDB at (site has been offline for many months; here's the Google Cache).


LaTeXDB is pretty simple to use, there are only three new commands to extend your LaTeX files: With them you define a database connection, create queries and access the result sets.

You include commands such as

to setup a database connection,
\texdbdef{##query}{select var1,var2,... from table where...}{##VAR1,##VAR2,...}
to define a query, and
\texdbfor{##query}{... some LaTeX stuff with ##VAR1, ...}
to use the results in your LaTeX code.


This release of LaTeXDB supports MySQL and PostgreSQL. You need Python (tested with version 2.2.2) and the Python MySQLdb or psycopg module for MySQL or PostgreSQL support, respectively. The database need not be running on the local machine, since you can use any server that you could also access from the mysql or similar command line client.

Download / Release Notes

Version 0.3 [2006/04/24]: latexdb-0.3.tgz
- A bug within the preparser that caused latexdb to break in texdbfor loops with too big arguments was fixed (contributed by Francois Meyer)
- New script pdflatexdb which is the same as latexdb but calls pdflatex instead of latex, so it generates PDF, not DVI files (user request)

Version 0.2 [2003/08/20]: latexdb-0.2.tgz
Queries can now be nested (refering to data from the upper query).
Using MySQLdb modules instead of _mysql
Extended to use PostgreSQL as well, code cleanup. (contributed by Gerhard Kirchmann)

Version 0.1 [2003/08/15]: latexdb-0.1.tgz
Initial release, MySQL support


This is easy too: Grab the archive, unpack it, and run install as root, or for minimum functionality copy latexdb, and from the code directory to /usr/local/bin or some other directory that can be found in $PATH. Only put them in the same directory, because latexdb expects and in its own directory.

First try

For a first try of LaTeXDB check the tiny tutorial in workshop.html. A more thorough description can be found in reference.html (not available yet). There are some examples in the examples folder of the package.


This is a quick hack, more a proof of concept. One of the special commands introduced with LaTeXDB (the database connection) needs to sit on a line of its own (and only one line), only the commands for defining queries and using the result set can use as many lines as you like and have other stuff in front and behind.

The Python code is strictly procedural. Some people won't like that since you're supposed to do objects in Python ;) But I couldn't be bothered. If anyone likes to write up this code and make it follow standard Python programming more properly, I'd be happy to see that code.

I have not included proper error condition checking. Do something wrong, and the tool will break. It won't kill any of your files, and it will even give some hint as to what was wrong while it dies.


There's no point in trying to list all developers who have created the software that I used. However, this program is here thanks to the existence of Linux, MySQL, Python, TeX, LaTeX, and lots of other stuff -- we tend to forget.

PostgreSQL support was contributed by Gerhard Kirchmann.
Francois Meyer fixed a bug in the preparser.
Damon Hart also fixed the preparser bug with a complete rewrite, but I never found the time to understand the new version ;)

The man page was created using manedit,

Copyright and Feedback

LaTeXDB is Copyright © 2003-2007 Hans-Georg Eßer (h.g.esser [at]

It is released under the GNU GPL, a copy of which you find in the package after downloading. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

If you have suggestions how to improve this software, or if you have successfully used it for something a lot better than the trivial examples given in the documentation -- please drop me a note.

Copyright © 1997-2024 Hans-Georg Eßer; Server: Debian Linux, Apache Web Server, letzte Änderung: Thursday, 17-Oct-2019 15:22:45 CEST
Theme: Hazard Area 1.6 (modified), created by Bryan Bell, Copyright © 2000-2006