Python and Latex

Posted on 2011-10-18
Last Modified: 2012-05-12

I'm trying to write a python program to generate a pdf document with LaTeX type equations.

At the moment, I am simply creating a tex file with all the commands in place e.g.

outfile.write( "\\documentclass{article}\n" )
outfile.write( "\\usepackage{graphicx}\n" )
outfile.write( "\\begin{document}\n" )
outfile.write( "\\end{document}\n" )

I have two questions:

(1) Is there an easier way to do this? That is, using this approach I will have to process the resulting file using LaTeX and get it to create a PDF document (possibly using pdflatex). Is there a way to do this using purely Python modules?
(2) If I do create the latex file, is there a way for me to get the python code to process the resulting file to create the pdf document automatically?

Thanks in advance

Question by:IssacJones
    LVL 6

    Accepted Solution

    I recently had a search around for a module to create tex files within python for a project I was assigned, and I couldn't find anything.  Hopefully someone else will have some better news for you.

    You can always create the PDF directly from python by calling the command line pdflatex utility from within python using os.command("pdflatex [OPTION...] [COMMAND...]"

    Author Comment

    Hi Lee

    Yes, I did some searching. I came across PyTeX but the download only seemed to give .tex files. The homepage seems promising saying that this type of code can be used:

        from tex import tex, plain
        document = 'My beautiful \\TeX\ document.\n'
        (dvi, log) = tex(plain, document)

    to generate a dvi file.

    However, if you download from the link given I can't see any python modules
    LVL 6

    Expert Comment

    I think I found PyTex and thought it was incomplete (as you say, it is lacking any python modules).  I didn't try contacting the developer though, that might be worthwhile.

    Author Closing Comment

    I've emailed PyTeX but not had a response so it looks like the project is on hold.

    I wonder if there was a way for me to TeX source code and I could try and write something myself i.e. I can program in C++.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
    Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now