While preparing the slides for EuroPython, I decided to come back to Latex after more than 10 years from my thesis. The reason is that latex modules provide code syntax highlight – something unmanageable with libreoffice.
The `minted` package provides all that with just
\begin{minted}{python} def your_python(code): return here \end{python}
You can shorten all that with a macro
\begin{pycode} # and you can even use # math formulas $\sum_{x=0}^{n}x in_comments = sum(range(n+1)) \end{pycode}
The hard issue I faced was to mark code (eg. like highlighter marking) because minted allows formatting only into comments. The following didn’t work.
# we wanted the append method to be emphasized, a = [1, 2] a.\emph{append}(3) # but the output was verbatim!
After some wandering I started from the ground. Minted package uses the python script pygment to convert code into a syntax-highlighted latex page:
#pygmentize -o out.tex -f latex -l python -O full=true out.py
So it was pygment to refuse interpreting latex formatting into the code. Luckily the latest development branch
Moreover I found in the pygment development branch, a patch allowing escape sequences into pygment.
# setting pipe (|) as an escapeinside character def now_this(works): works.|\emph{append}|(1) # run with #pygmentize -o out.tex -f latex -l python -O full=true,escapeinside="||" out.py
After installing the last pygmentize from bitbucket, I just patched minted and enjoy the new syntax directly from latex
% set the pygmentize option in the % latex page! \begin{pycode*}{escapeinside=||} def now_this(works): works.|\colorbox{yellow}{append}|(1) \end{pycode}