6

I am looking into starting to use Python and (La)TeX together.

Now I see two packages on ctan, pythontex and hybrid-latex. What are the pros and cons? What is the better-developed solution (with reasoning)? Is there an expectation of what the future will hold, i.e., are both still actively maintained/improved? Please feel free to add other solutions to combine Python and LaTeX.

9
  • Could you maybe add your use cases for combining both? That way answers will be more relevant…
    – TeXnician
    Feb 27, 2019 at 13:47
  • 2
    I'm afraid this is an opinion-based question.
    – user156344
    Feb 27, 2019 at 15:03
  • @TeXnician: I work a lot with python and use LaTeX a lot for reporting. So if I could generate data/plots with python then have them directly embedded in my LaTeX document would be lovely. Python: numpy, matplotlib. LaTeX, TikZ/pgfplots. Feb 27, 2019 at 15:53
  • @JouleV that's why I put that I'd like to see reasoning for the recommendation. Potentially the main question is ill-phrased, but I tried to make sure that I'm not looking for a mere: "I prefer A over B", but mostly about the why. So I can arrive at my own opinion. Feb 27, 2019 at 16:00
  • @MatthiasArras I understand that. Your question is opinion-based, but it doesn't deserve to be closed IMHO.
    – user156344
    Feb 27, 2019 at 16:01

2 Answers 2

8
+50

Well, eventhough the question is borderline opinion-based, I will give you rundown of my knowledge and experience.

First off, that is to say, I have experience with PythonTeX and never used hybrid-latex. I have used it to learn Python, notably numpy, matplotlib, pandas, lxml and other around data processing. Next points will give you reasons why.

  1. I have found this package as the only still maintained and actively developed package in this topic. When searching, I did not even hit hybrid-latex, but I have come across pyLaTeX that seemed abandoned at that time.

  2. PythonTeX has same author as minted and fancyvrb packages, which kinda guarantees authors skills in TeX programming.

  3. PythonTeX supports a lot of other programming languages - R, Julia, Perl, bash, and more

  4. I looked briefly at pyLaTeX manual - all the cons of PythonTeX mentioned there are not valid. Adding new language to PythonTeX is pretty easy, if it is interpreted language. Compiled languages with specific requirements might be problem. PythonTeX has enough options, environments and macros to specify which code blocks you want to execute, only typeset (only print), or both. It also allows you to reference variable values in LaTeX text seamlessly and easily. Look at the quickstart guide, its fantastic.

  5. Error messages are synchronized with TeX line numbers - therefore, in case of error in your code, you know at which line in .tex file it occured. That helps a lot.

  6. For setting up automated compilations there is an arara rule (watch out, I am its author, it would be cool to have someone else test it) and there is also guide how to setup latexmk for PythonTeX. Since both PythonTeX and latex-hybrid require 3 step compilation, this helps a lot too.

  7. Lastly, PythonTeX has an active author and community. Author is now mostly active on Github.

CONS:

Only one, actually. That is, due to pandemic and maybe other issues, author does not have currently resources to actively develop the package. Currently it is mostly bugfixing. Author has also other similar project, codebraid, that is coupled with pandoc and fixes most of PythonTeX limitations.

But, PythonTeX is in no means abandoned.

4

Because I also use Python and LaTeX intensively, I played with pythontex package for a while. At first, it was all fun and excitement. But later on, I decided to completely stay away from it. My advice is: if one is writing a serious scholarly article, try not to use pythontex.

The reasons are as follows:

  • If you are to publish your article, many publishers' editorial system (even arXiv) simply do not support pythontex. It is risky that one may have to restructure their document thoroughly for different submissions.
  • Personally, I find code highlighting and automatic indentation very helpful when I am coding in Python. That being said, if you are writing Python code in a LaTeX editor, you will not benefit from these features.
  • It is very difficult to debug your Python code if you are using pythontex.
  • If you have multiple Python executables on the machine (e.g. anaconda), it takes a lot of work to configure the right one. It gets even more complicated if you are collaborating with someone else, whose Python executable is different.

I think the bullet points above can be summarized into one sentence: coupling LaTeX and Python together tends to induce compatibility, usability and migratability issues.

Since Python is a much more comprehensive language, whenever it is necessary to combine Python and LaTeX, I think most of the workload should be done on the Python side. The task of Python should be to generate tex or pdf files that can be used by LaTeX directly. This makes your LaTeX source completely publisher-independent.

I think these problems are very meaningful when combining Python and LaTeX:

  • How to make sure the style of plots are identical to that of document? Suppose one is using matplotlib, a solution is to use pgf backend with the correct LaTeX preamble.
  • Given that LaTeX and Python are now decoupled, how to simplify the workflow? What I usually do is to encapsulate each of my plot into a Python function and call it when I need some updates. The function should not only generate the graph but also save it into the right directory. This allows you to save time if you are only rerunning one or two plots. You can even call the LaTeX build system from Python with the subprocess library, if more changes are made in the Python side than LaTeX side.
  • Some basic LaTeX programming knowledge can help reduce workload. For example, if a big figure in LaTeX is made up of 20 subfigures (in this case, pdf files generated by Python), one can simply write a loop in LaTeX to include them all with little repetitive work.
10
  • 2
    I mostly agree, I dont have much experience with scholarly publishing. But I would argument against your second bullet. I think (but I might be mistaken!) that PythonTeX allows you to import and execute python code from external file. So you can write your python code in different editor (and file) than rest of your text. Oct 6, 2020 at 6:36
  • Thanks for sharing the view on publishing with external partners. Not sure that anyone will ever allow access to their python subsystem for safety reasons. I have used python before to compile a .tex document and send it to the pdflatex compiler, but to me it feels kind of backwards, because the document is the enclosing bit, the matrix if you will and I use python to generate local bits of the document. I am writing my latex documents in Atom, because that's what I use to run python. It does comes with syntax highlighting for either python or latex, maybe would not be too hard to combine. Oct 6, 2020 at 11:34
  • 1
    In emacs you can use polymode: When you are inside of a pycode environment it changes automatically to python-mode and you get python font locking and completion. You can also send pycode blocks easily to python shells running in emacs.
    – Julia
    Oct 17, 2020 at 21:01
  • 1
    @MatthiasArras One of the tricks I used is to place special comments in my TeX source, for example %%python1, and then use Python's .replace() function in the post processing step to substitute it with Python-generated content.
    – Alan Xiang
    Oct 19, 2020 at 19:15
  • 1
    There is extension discussion in the pythontex documentation about how to prepare standalone latex files for journal submission etc. Feb 7, 2022 at 8:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .