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?
UPDATE 10APR2013:
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
andISO-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 hd
and md
and print the output into a table.
The Code
\documentclass[a4]{article}
\usepackage{fontspec}
\texdbconnection{MySQL,localhost,root,password,mydatabase}
\texdbdef{##query1}{select hd,md from mytable}{##hd,##md}
\begin{document}
This is a header for the page.
\begin{tabular}{|l|l|}
\hline
Hochdeutsch & Mantakisch \\
\texdbfor{##query1}{\hline \textit{##hd} & \textbf{##md}\\}
\hline
\end{tabular}\\
\end{document}
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>
_main_()
File "/opt/local/bin/latexdb.py", line 262, in _main_
texDBparseForLoop(l)
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
cursor=db.cursor()
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
(./database.tex
LaTeX2e <2011/06/27>
Babel <v3.8m> and hyphenation patterns for english, dumylang, nohyphenation, lo
aded.
(/opt/local/share/texmf-texlive-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/opt/local/share/texmf-texlive-dist/tex/latex/base/size10.clo))
(/opt/local/share/texmf-texlive-dist/tex/latex/fontspec/fontspec.sty
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/expl3.sty
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3names.sty
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3bootstrap.sty))
(/opt/local/share/texmf-texlive-dist/tex/latex/etex-pkg/etex.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/graphics/color.sty
(/opt/local/share/texmf-texlive-dist/tex/latex/latexconfig/color.cfg)
(/opt/local/share/texmf-texlive-dist/tex/xelatex/xetex-def/xetex.def))
(/opt/local/share/texmf-texlive-dist/tex/latex/graphics/graphics.sty
(/opt/local/share/texmf-texlive-dist/tex/latex/graphics/trig.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/latexconfig/graphics.cfg))
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3basics.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3expan.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3tl.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3seq.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3int.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3quark.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3prg.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3clist.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3token.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3prop.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3msg.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3file.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3skip.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3keys.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3fp.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3box.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3coffins.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3color.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/l3kernel/l3luatex.sty))
(/opt/local/share/texmf-texlive-dist/tex/latex/l3packages/xparse/xparse.sty)
(/opt/local/share/texmf-texlive-dist/tex/latex/fontspec/fontspec-patches.sty
(/opt/local/share/texmf-texlive-dist/tex/latex/base/fixltx2e.sty)
*************************************************
* 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
(/opt/local/share/texmf-texlive-dist/tex/latex/base/fontenc.sty
(/opt/local/share/texmf-texlive-dist/tex/latex/euenc/eu1enc.def)
(/opt/local/share/texmf-texlive-dist/tex/latex/euenc/eu1lmr.fd))
(/opt/local/share/texmf-texlive-dist/tex/xelatex/xunicode/xunicode.sty
(/opt/local/share/texmf-texlive-dist/tex/latex/tipa/t3enc.def
(/opt/local/share/texmf-texlive-dist/tex/latex/euenc/eu1lmss.fd))
(/opt/local/share/texmf-texlive-dist/tex/latex/graphics/graphicx.sty
(/opt/local/share/texmf-texlive-dist/tex/latex/graphics/keyval.sty)))
(/opt/local/share/texmf-texlive-dist/tex/latex/fontspec/fontspec.cfg)))
LaTeX Warning: Unused global option(s):
[a4].
(./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
- http://www.ctan.org/pkg/ratexdb (Ruby based)
- 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....
QUICK FIX
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
.
- Save the following code as
database.py
. - Run
python database.py > ~/Desktop/database.txt
- 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 thelongtable
(orlongtabu
) environment from the python code directly into the master document for better control.
Python Code:
#!/usr/bin/python
# a simple script to pull some data from MySQL
import MySQLdb
db = MySQLdb.connect(host="localhost", user="root", passwd="yourpassword", db="databasename")
db.set_character_set('utf8')
#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} |}"
##Iterate
for row in cur.fetchall() :
#data from rows
column1_name = str(row[0])
column2_name = str(row[1])
#print
print "" + column1_name + " & " + column2_name + "\\\\" + "\n" + "\\hline"
# close the cursor
cur.close()
# close the connection
db.close ()
print "\\end{longtable}"