TransWikia.com

How can I use LaTeX from Python.

TeX - LaTeX Asked by Frozenskys on October 21, 2020

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.

13 Answers

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

Answered by Stefan Kottwitz on October 21, 2020

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

Answered by ptman on October 21, 2020

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

Answered by Jared Updike on October 21, 2020

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.

Answered by Neil Olver on October 21, 2020

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.

Answered by Jonathan Fine on October 21, 2020

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.

Answered by Andrew Stacey on October 21, 2020

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.

Answered by user573 on October 21, 2020

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('vskip2emnfonttitlefont=cmr12 at 14.4ptnfontdefault=cmr12n')
    pageAry.append('deftoday{January 21, 2011}n')
    pageAry.append('centerline{titlefont ' + title + '}nvskip5ptnvskip5ptcenterline{default blahblahblah}n')
    pageAry.append('nbye')
return 1

a_tex_file("blunk")

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

Answered by bev on October 21, 2020

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

Answered by pmav99 on October 21, 2020

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.

Answered by Joop on October 21, 2020

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

Answered by JelteF on October 21, 2020

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.

Answered by Evys Ancede Gallardo on October 21, 2020

For folks interested in a newer solution, I also ran into this problem and coded up a workaround. Repo has more information: (py2tex)

Basically, you surround your Python code with ? tags. Anything you print inside these tags is inserted into your final tex file! For example:

documentclass{article}
begin{document}

?
print('Python textrightarrow LaTeX!')
?

end{document}

becomes:

documentclass{article}
begin{document}

Python textrightarrow LaTeX!


end{document}

All you need to do is run the py2tex.py utility from the linked GitHub repo. Alternatively, you can hook into it from latexmk using a custom dependency (instruction for this also in the git repo).

This tool saved me a lot of time and heartache. I hope you like it!

Answered by mrplants on October 21, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP