Highlighting python in Latex

Latex is one of the nicest tools for formatting documents, but also a rabbit hole when you want to get that one small feature to your liking. Many PhD students discover how months quickly vanish when trying to create the perfect template…and the situation does not improve with age :-). In a recent bout of rabbit chasing, I decided that I wanted to be able to have syntax highlighting for python available for a course syllabus I’m planning. Early searches looked hopefull as a simple to use package seems to be available for the job (and even suitable for other programing languages): minted. Which is promoted by the tutorial page on overleaf with regard to syntax highlighting. Installation is simple using the MikTex Console.(Though the latter first destroyed itself during an update, losing access to the Qtframework. But Once Miktex was reinstalled, installing minted is trivial.) Unfortunately, WinEdt had an issue which was sufficiently vaguely defined: it could be not installed, incorrectly installed, missing in the path, not accessible without a shell-escape, missing some environment variable…

After some searching (shell-escape switched on, added to the path, installing additional python support packages –>?seriously?) it became clear that the package was not installed entirely correctly. A .py script isn’t sufficient in windows, and an additional cmd script was needed (as well as python installation, which was a bit amazing as we are talking about latex, so there should be no need for python, but apparently minted is a python library, wrapped up for latex). So long story short, if you want to work with minted, you should add a small cmd script to the binary folder of your latex install (after “installing minted” using MikTex) which calls your python installation to execute the python script latexminted.py.

@echo off
"C:\Path\To\Python\Install\python.exe" "C:\Path\To\MikTex\Minted\latexminted.py" %*

Permanent link to this article: https://dannyvanpoucke.be/highlighting-python-in-latex/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.