Texcaller
|
import texcaller texcaller.convert(source, source_format, result_format, max_runs) # returns a pair (result, info) texcaller.escape_latex(s)
These Python functions are simple wrappers around the Texcaller C interface library functions, making TeX typesetting easily accessible from Python.
# coding: UTF-8 import texcaller latex = ur'''\documentclass{article} \begin{document} Hello world! \end{document}''' pdf, info = texcaller.convert(latex, 'LaTeX', 'PDF', 5) print 'PDF size: %.1f KB' % (len(pdf) / 1024.0) print 'PDF content: %s ... %s' % (pdf[:5], pdf[-6:]) s = u'Téxt → "with" $peciäl <characters>' print 'Original: %r' % s print 'Escaped: %r' % texcaller.escape_latex(s)
\u
Unfortunately, Python always interprets \uXXXX
sequences in unicode strings (even in raw unicode strings), which interferes badly with common LaTeX commands such as \usepackage{}
. This means every backslash \
which is followed by the character u
needs to be escaped via \u005c
, which looks really strange:
latex = ur''' \documentclass{article} \u005cusepackage{amsmath} \begin{document} Hello math! \end{document} '''
Fortunately, this problem is easily solved by using raw byte strings, and converting those to unicode strings via the decode()
method:
latex = r''' \documentclass{article} \usepackage{amsmath} \begin{document} Hello math! \end{document} '''.decode('utf-8')