As of today, what is the best package for embedding functional Python code in LaTeX? I've searched this site and others, which recommend the python, pythontex, and sympy packages, amongst others, but the information is somewhat dated. I want to know what users suggest as the best alternative now.

  • 5
    The abstract and the introductory section of the user guide of the pythontex package pretty much address the questions and concerns you're expressing. Type "texdoc pythontex" at a command prompt to bring up the user guide document. – Mico Apr 11 '15 at 3:38
  • 3
    Don't forget sagetex if you happen to want a fully equipped mathematical engine. That said, I swear by pythontex. Incidentally, its author @GPoore is a member of tex.sx and gives quick and sound support. – Koji Apr 13 '15 at 13:57

You might want to try out Pylatex (https://github.com/JelteF/PyLaTeX), the documentation is not the best you can find out there, but once you get a hang of it, it is pretty neat. Personally, I love the fact that you can do your analysis(using Python) and the the documentation of the analysis together. And as it always goes, do split your programs into lots of small functions to make your work easier!

  • 2
    Isn't PyLaTeX the opposite of what NVaughan is asking about? This is a python package to create a LaTeX document not a way to embed python code into LaTeX. – Alexander Apr 21 '16 at 20:57
  • Well, something like pythontex, lets you call python from within the latex, which then runs your python script and you can display whatever output it generates in the latex document. Whereas using PyLatex, you write a python script which generates the latex document using the Pylatex. the script you write, can have your own code and you can send the output of that code into the latex document. So yeah it is different, but if what you aim is to eventually fasten your workflow by combine the coding and the documenting part together, either of these work fine. – Ashwin Parambath Apr 21 '16 at 21:15
  • Let me introduce you to my good friend...sorry our good friend 'Google'! – Ashwin Parambath Apr 21 '16 at 21:30

Disclaimer: This answer is referring to a github repository that I wrote.

I have recently been working on a github repository that does what you are asking. It can be found here: https://github.com/QuantumSpaghettification/QS-TEX and works as follows (you need to be able to run python2.7):

  1. Download the FUNC and LANG directories along with editor.py and place them into a directory e.g. QS.
  2. Download QS.config and place into the directory of you .tex script.
  3. Edit the QS.config file to include the path to the directory QS and if you want to use the Listings package define a style else empty all other variables.
  4. In your tex document use the following notation:

    < script=python2.7:action={}\>

    Your script here.

    output(string you want to output in place of script)

    < /script>

(there should be no space after the open angle bracket)

  1. Using python2.7 run the script editor.py on your tex file and then your standard LaTeX program on your standard compiling program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.