# How can I use LaTeX from Python.

I need to generate documents from a web application and would like to do this using the Python language and LaTeX, are there any tools that will help me?

Edit
This Application will be hosted on Linux, we can run any external commands using popen, there is currently no defined input document format, nor any storage format, but output to the end user should be PDF.

Edit 2
These documents will have complex tables, graphs, and require typeset equations - hence the reason to use LaTeX. We would also prefer not to use intermediate files such as xml->html->pdf

Ideally I would like something like pyTeX or plasTeX that could render directly to PDF.

-
Could you be a bit more specific about your requirements? In particular, what format will the documents be written in? What format would you like the output to be? Can you call external commands from your web application? –  Loop Space Aug 3 '10 at 12:31
(responding to edit): So can you say: input format should be LaTeX and then call pdflatex on the documents; somewhat in the spirit of what the arXiv does? –  Loop Space Aug 3 '10 at 12:58
I would only go for LaTeX if you want more text (or math) than graphics. I say that, having done custom PDF output (tables, images, some text) for a desktop application. –  Jared Updike Aug 3 '10 at 15:24
Do you really want python to do the conversion? What are the disadvantages, in your view, of simply calling pdflatex as an external command? –  Loop Space Aug 6 '10 at 12:25
If LaTeX and python are not strict requirements, you can look at ConTeXt lua documents which allows you to write directly compile lua programs to pdf (using luatex and the ConTeXt format). –  Aditya Jun 27 '11 at 22:24

PyTeX is an Open Source project allowing to use TeX from within Python.

-
A little out of date as the last download available is 2005! :) –  Frozenskys Aug 3 '10 at 12:49

This question should be closed because it actually has nothing to do with LaTeX and is more suited for StackOverflow. In any case the answer is that just like with HTML the best way is to use a templating system like Jinja2 and just output a LaTeX file. Once you have a LaTeX file simply use the subprocess module to run pdflatex (obviously you need it installed on your server). Don't forget to use the "-interaction nonstopmode" flag. I could go into technical details but again it's really more suited for a different site.

-
I appreciate why you think that. But as a third party observer, I can tell you that I just searched for python and latex and found this. I was happy about that! –  macmadness86 Jul 3 '12 at 19:53
I think this is perfectly suited to the Tex stack. –  stuckintheshuck Sep 16 '13 at 20:54
Great answer, I would just add a note that some chars should be escaped. –  neutrinus Sep 12 at 6:07

Depending on what you want to do, Sphinx may suit you. I think its the best Python-based tool for technical documentation, and it supports restructured text.

-

Bit late for an answer, but would like to share my experience. I had a similar problem. Basically needed to get output from Python application in pdf form. Had a look at various alternatives

1. Jinja2
2. Reportlab
3. Pollyreports
4. As well as the options listed above

Eventually I settled on using Latex. Basically just wrote a small class that assembled my elements into a tex file and then ran pdflatex on the generated tex file. Other options had a lot of control, but tex already has great formatting predefined and I just needed a professional container for my figures and tables.

-

While I love LaTeX, you probably don't want or need TeX to solve your problem. Check out ReportLab Toolkit

-

Depending on exactly what you want to do, you may want to take a look at plasTeX. It's a python version of the TeX engine. It's not a true LaTeX interpreter, but if you have control over the input format of the documents then it could be possible to write them in such a manner that plasTeX can render them. At present, it renders the document to XHTML.

So if you wanted web-viewable copies, you could have it so that your documents were sufficiently simple that plasTeX can read them, then use plasTeX for XHTML-rendering and call pdflatex externally for PDF.

-

Is this what you're asking about? ( I used TeX to save code-space, but LaTeX is the same). If it is the what you're asking about, and you cut and paste this example, make sure the indents are correct after the paste.

#!/usr/bin/env python
import sys, os, os.path
filename = sys.argv[1]
opfile = sys.argv[1] + '.tex'
outfile = open(opfile, 'w')
pageAry = []
def a_tex_file(title):
global pageAry
pageAry.append('\\vskip2em\n\\font\\titlefont=cmr12 at 14.4pt\n\\font\\default=cmr12\n')
pageAry.append('\\def\\today{January 21, 2011}\n')
pageAry.append('\\centerline{\\titlefont ' + title + '}\n\\vskip5pt\n\\vskip5pt\\centerline{\\default blahblahblah}\n')
pageAry.append('\n\\bye')
return 1

a_tex_file("blunk")

for i in pageAry:
outfile.writelines(i)
outfile.close()
os.system('tex '+ opfile)
os.system('xdvi ' + filename + '.dvi & ')

-
Do you really need a global variable here? –  Mohan Nov 24 '12 at 17:48
LOL. No, guess not. Don't know what I was thinking. Thanks for pointing that out. –  bev Nov 24 '12 at 23:46

Recently I've written a library exactly for this purpose. It supports tables, plots, matrices and more. https://github.com/JelteF/PyLaTeX

-

PyX is a useful package if you want graphs and charts.

-

If you want to convert a web page into a pdf, maybe the better way is using the python-pisa package, perform a direct conversion, I used it in a django projects for this purpose.

-

Where does the data for the PDF you want to generate come from? A database?

I ask because (despite being a big Python fan) I once used PHP to generate a latex file with data populated from a database (this was for a very small conference proceedings). It's a bit messy, but works reasonably well; you can easily intermingle PHP code which pulls from the database with latex source, in the same way that you can mix PHP with HTML. Then just compile the resulting latex file to get a PDF.

-

check latexmk.py. It allows easily to create your pdf files with python.

-