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?

  • 17
    Have you considered writing a script (e.g., in Python or Perl) that reads the database and outputs Latex source code? Oct 2, 2010 at 23:01
  • See detailed walkthrough for Lua and SQLite here.This approach still works as of September 2021, but may not be futureproof. This example should be trivially adaptable for other DBs that LuaSQL supports. Sep 4, 2021 at 7:35

10 Answers 10


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

  • How does it work with MariaDB?
    – 71GA
    Aug 8, 2017 at 17:18

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.


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, 2012 at 14:45
  • 1
    @kornaros you should ask a new question.
    – topskip
    Feb 24, 2012 at 16:04

Check out Sweave and R


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.


You can check out DataTeX. A short description.

  • Great idea. Any update regarding Windows users ? I have the same (idea...) type of file management system but on Excel :) your DataTeX looks stunning
    – JeT
    Sep 7, 2022 at 14:09
  • @JeT I can say that DataTex is 99% ready for windows use. The only things that keeps me back is that I can't compile the project on windows due to some bug on the pdf viewer. I'm working on it and I hope to fix it soon. Thanks.
    – mac
    Sep 7, 2022 at 16:16
  • I'll use it for sure. From what I see (screenshots) we reach the same conclusions about how indicng things on top of the latex file organisation. ευχαριστώ !
    – JeT
    Sep 7, 2022 at 18:38
  • Παρακαλώ (You're welcome)!
    – mac
    Sep 7, 2022 at 21:35
  • If I may, take a look at this answer and this one. It enables to use pgfkeys based on your document naming to compile selectively group of documents. It seems consistent with your system.
    – JeT
    Sep 9, 2022 at 19:16

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

  • 1
    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, 2015 at 9:56

See here as well: https://tex.stackexchange.com/a/40728/4736

The answer refers to a template engine called jinja, nowadays jinja2. There are some answers in the realms of the internet about LaTeX and jinja2, see here https://tug.org/tug2019/slides/slides-ziegenhagen-python.pdf e.g.

I recently used python and jinja2 to produce lots of *.tex files.

Hey, this is my first answer to a question more than a decade later than the question has been posted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .