43

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?

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
robintw
  • 7,362
  • 17
    Have you considered writing a script (e.g., in Python or Perl) that reads the database and outputs Latex source code? – Jukka Suomela Oct 02 '10 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. – Faheem Mitha Sep 04 '21 at 07:35

10 Answers10

17

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

14

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

Lev Bishop
  • 45,462
8

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

Robin
  • 196
5

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.

Charles Stewart
  • 21,014
  • 5
  • 65
  • 121
5

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

topskip
  • 37,020
  • I made a simple xml database. (See here [link] http://tex.stackexchange.com/questions/45114/could-i-insert-the-elements-of-a-mdb-or-csv-base-in-latex). 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
  • 1
    @kornaros you should ask a new question. – topskip Feb 24 '12 at 16:04
5

Check out Sweave and R

Jeromy Anglim
  • 7,450
  • 6
  • 46
  • 63
3

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.

TH.
  • 62,639
2

You can check out DataTeX. A short description.

mac
  • 1,479
  • 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 07 '22 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 07 '22 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 07 '22 at 18:38
  • Παρακαλώ (You're welcome)! – mac Sep 07 '22 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 09 '22 at 19:16
  • It looks very interesting! I will have to take a look at it. – mac Sep 09 '22 at 21:33
1

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

  • 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 04 '15 at 09:56
0

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.

Keks Dose
  • 30,892