使用本网站上的示例代码,http://hgesser.com/software/latexdb/workshop.html, 我想
- 查询我的 MySQL 数据库并使用 XeLaTeX 排版。
这是因为我的所有文档都是使用UTF8
和设置的\usepackage{fontspec}
。我查看了文档,但似乎找不到任何内容。排版 Python 脚本latexdb.py
可能有答案,但我对其中的内部代码并不熟悉。有人对此有见解吗?
2013年4月10日更新:
我联系了 LaTeXDB 的制造商,他回复说:
我相信您使用的特定 TeX 方言应该无关紧要,您只需修改 LaTeXDB 脚本,因为它明确调用 pdfLaTeX 或 latex [...] 这假设 xelatex 使用我的脚本可以处理的编码的输入文件;
UTF-8
并且ISO-Latin-*
应该没问题。
根据这些信息,我只是修改了 LaTeXDB 底部的命令,将 LaTeX 命令更改为 XeLaTeX,例如
# call latex with all options from the dblatex command line
xelatex $@
mv -f $pretex.backup $pretex
在下面的代码中您获得了数据库连接变量:
- 数据库:
mydatabase
- 桌子:
mytable
- 柱子
hd
- 柱子
md
现在循环遍历列中的行(记录)mytable
并将hd
输出md
打印到表中。
代码
\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}
为什么使用 LaTeXDB 排版时总是出现此错误?
(好像不是真的用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))
*
Eßer 先生还指出,他的代码有两种较新的重新实现
...值得注意的是,我因为这个问题被授予了 Tumbleweed 徽章,唷!(带有讽刺意味)....
快速解决
如果任何读到这篇文章的人需要解决方案现在像我一样,如果你不关心它是如何完成的,你可以设置MySQL python 教程(MySQLdb)并仅生成 XeLaTeX 所需的代码longtable
。
- 将以下代码保存为
database.py
。 - 跑步
python 数据库.py > ~/Desktop/database.txt
- 然后将这些数据复制并粘贴到 TeX 文档中。像平常一样进行编译。(不要忘记包含
\usepackage{longtable}
。)或者更好的是,使用\include{./database.txt}
。您可以将longtable
(或longtabu
)环境从 python 代码直接移动到主文档中,以便更好地控制。
Python代码:
#!/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}"