# Is it possible to connect a database to LaTeX to produce data-driven documents?

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?

-
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

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

-

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).

-

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 http://www.hoens.net/robin/nlatexdb

-

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.

-

Check out Sweave and R

-

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 \.

-
I made a simple xml database. (See here [link] tex.stackexchange.com/questions/45114/…). 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

\newcommand\handlerecord[8]{...}


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.

-

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}
\MultipleDataKeyWord{Belle}{journ\'ee}


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

\def\DataKeyWord#1{\gdef\@DataKeyWord{#1}}
\def\MultipleDataKeyWord#1#2{\gdef\@FirstDataKeyWord{#1}\gdef\@SecondDataKeyWord{#2}}


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

\newcommand{\CommandNameLikeWriteMySection}{
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:

\CommandNameLikeWriteMySection


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

-
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 '15 at 9:56