Sign up ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm starting to write a LaTeX-based reference manual, and have realised that a lot of my needs could be solved by writing a database and then producing the reference manual from that. Lots of my pages will be exactly the same, with slightly different information in them.

Is there a way to link LaTeX to a database, in a similar way to Database Report engines do - so that I can, for example, create a page per record in the database, including each field in different locations on the page?

share|improve this question
Have you considered writing a script (e.g., in Python or Perl) that reads the database and outputs Latex source code? – Jukka Suomela Oct 2 '10 at 23:01

8 Answers 8

up vote 14 down vote accepted

You may try latexdb. It connects LaTeX to a MySQL database.

share|improve this answer

You might want to check the answers to these questions:

Depending on what you want to do, you could look into the datatool package or pgfplotstable. Or if your needs are more complex (tex is not the most efficient language for doing sophisticated database manipulation), you should write a script to produce a latex formatted report directly from the database (as Jukka suggested).

share|improve this answer

Latexdb is a great idea, but works only with MySQL.

I have written a clone, first in Ruby, recently in .Net (works with Mono, too), with some nice improvements. See

share|improve this answer

You can load dynamic libraries into Luatex, like the Lua-SQL database bridge. I've never done this kind of thing, mind, but there are people here who would know how to do this.

You can invoke Lua from Tex with appropriate directive, such as from Lualatex, to pull and process data from the database bridge.

share|improve this answer

Check out Sweave and R

share|improve this answer

I'd write some script to extract the data in XML format (perhaps the database already offers that?) and process the file directly with ConTeXt. ConTeXt is well suited for a) reading XML input (directly) and b) for generating datasheets or alike. For example ConTeXt now offers \asciimode that makes most funny characters normal (&,#,~,$), so don't have to deal with escaping characters beyond % { } and \.

share|improve this answer
I made a simple xml database. (See here [link]…). Is it possible to write a simple ConTeXt(LuaLaTex) or LuaLaTeX code for this xml to illustrate your answer? – kornaros Feb 24 '12 at 14:45
@kornaros you should ask a new question. – topskip Feb 24 '12 at 16:04

Assuming each record has at most 9 fields, then one possibility would be to define a macro like


and then write a script as Jukka Suomela suggested that basically writes out the preamble and then a bunch of \handlerecord{field1}{field2}... and ends it off with an \end{document}.

If you have more than 9 fields, you can use any of the answers to this question to define the \handlerecord macro.

share|improve this answer

you can make a .tex file with only data and keyword where your data is stored

"database.tex" for instance, contains the assignation of the variable \DataKeyWord and \MultipleDataKeyWord:

\DataKeyWord{my data}

In a layout.sty file, you declare your variable:


when you want to use this in your layout.sty file, use the command:

My Data is \@DataKeyWord
I wish you a "\@FirstDataKeyWord \@SecondDataKeyWord"}

then in your MySection.tex file used to write your report, you will use the command:


and it will execute the printing of the command \CommandNameLikeWriteMySection to write your pdf

share|improve this answer
The problem of this method is that it hard to compare 2 database files and make a table to compare data back to back because the value will be assign first and then print. – Stephane Aug 4 at 9:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.