Related question

Using the sample code on this site, http://hgesser.com/software/latexdb/workshop.html, I would like to

  • query my MySQL database and typeset using XeLaTeX.

This is because all of my documents are set up using UTF8 and \usepackage{fontspec}. I have checked out the documentation and I cannot seem to find anything on it. The typesetting python script latexdb.py may have the answer, but I am not a pro on the internal code in there. Does anyone have insight on this?


I have contacted the maker of LaTeXDB and he responded:

I believe that the specific TeX dialect you use should be irrelevant, you'd only have to modify the LaTeXDB script since it makes explicit calls to pdfLaTeX or latex [...] This assumes that xelatex uses input files with an encoding that my script can cope with; UTF-8 and ISO-Latin-* should be fine.

Based on this information, I just modified the command at the bottom of LaTeXDB, changing the LaTeX command to XeLaTeX like

# call latex with all options from the dblatex command line
 xelatex $@
 mv -f $pretex.backup $pretex

In the following code you've got your database connection variables:

  • Database: mydatabase
  • Table: mytable
  • column hd
  • column md

Now loop over the rows (records) in mytable for columns hdand md and print the output into a table.

The Code

\texdbdef{##query1}{select hd,md from mytable}{##hd,##md}

This is a header for the page.

Hochdeutsch & Mantakisch \\
\texdbfor{##query1}{\hline \textit{##hd} & \textbf{##md}\\}


Why do I keep getting this error when typesetting with LaTeXDB?

(It seems like it isn't really typesetting with XeLaTeX)

latexdb database.tex
Pre-Compiling database.tex (loop 1)
Traceback (most recent call last):
  File "/opt/local/bin/latexdb.py", line 274, in <module>
  File "/opt/local/bin/latexdb.py", line 262, in _main_
  File "/opt/local/bin/latexdb.py", line 151, in texDBparseForLoop
    for qR in texDBresult(queryString):
  File "/opt/local/bin/latexdb.py", line 68, in texDBresult
NameError: global name 'db' is not defined
This is XeTeX, Version 3.1415926-2.4-0.9998 (TeX Live 2012/MacPorts 2012_7)
 restricted \write18 enabled.
entering extended mode
LaTeX2e <2011/06/27>
Babel <v3.8m> and hyphenation patterns for english, dumylang, nohyphenation, lo
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
* LaTeX warning: "xparse/redefine-command"
* Redefining document command \oldstylenums with arg. spec. 'm' on line 107.
) (/opt/local/share/texmf-texlive-dist/tex/latex/fontspec/fontspec-xetex.sty

LaTeX Warning: Unused global option(s):

(./database.aux) (/opt/local/share/texmf-texlive-dist/tex/latex/tipa/t3cmr.fd))


Mr. Eßer also noted that there are two newer reimplementations of his code

  1. http://www.ctan.org/pkg/ratexdb (Ruby based)
  2. https://sourceforge.net/projects/nlatexdb/ (C based)

...It should be noted that I have been awarded the Tumbleweed badge, yoohoo! (with sarcasm) for this question....


If anybody reading this needs a solution NOW like me and you don't care how it gets done, you could setup MySQL-python (MySQLdb) and just generate the code necessary for a XeLaTeX longtable.

  1. Save the following code as database.py.
  2. Run

    python database.py > ~/Desktop/database.txt

  3. Then copy and paste that data into your TeX document. Compile as you normally would. (Don't forget to include \usepackage{longtable}.) Or even better, use \include{./database.txt}. You move the longtable (or longtabu) environment from the python code directly into the master document for better control.

Python Code:

# a simple script to pull some data from MySQL

import MySQLdb

db = MySQLdb.connect(host="localhost", user="root", passwd="yourpassword", db="databasename")


#create a cursor for the select
cur = db.cursor()

cur.execute('SET NAMES utf8;')
cur.execute('SET CHARACTER SET utf8;')
cur.execute('SET character_set_connection=utf8;')

#execute an sql query
cur.execute("SELECT column1,column2 FROM table")

print "\\begin{longtable}{| p{.20\textwidth} | p{.80\textwidth} |}"

for row in cur.fetchall() :
      #data from rows
        column1_name = str(row[0])
        column2_name = str(row[1])

        print "" + column1_name + " & " + column2_name +  "\\\\" + "\n" + "\\hline"

# close the cursor

# close the connection
db.close ()

print "\\end{longtable}"
  • 1
    And the question is?
    – egreg
    Apr 10, 2013 at 9:41
  • hold your horses I am actively working on formulating it now... adding specifics. Thanks though. I am trying to clarify this. Ok I posted my error message Apr 10, 2013 at 9:44
  • I don't see any error in the stuff posted: really not sure waht we are being asked!
    – Joseph Wright
    Aug 11, 2013 at 11:44


Browse other questions tagged .