Using the LaTeX-package pythontex I one can insert python code into the latex document which is executed (and printed to your latex file if you want) if you run latex. I use auctex for editing latex files, however editing the python code inside the latex document is a bit painful since the buffer is in latex mode and not in python mode, so there is no help for proper indentation of the python code and no syntax highlighting.
So is there any reasonable way to mix the python mode with the latex mode? For example that the buffer automatically jumps to python mode if the point is inside a python code snipped? What do I have to add to my .emacs file to make it work? Any other ideas?
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pythontex}
\begin{document}
\begin{pycode}
print('test1')
def f( ):
print('test2')
f()
\end{pycode}
\pyc{f()}
\end{document}
Note that pythontex
has multiple environments and commands which can contain pythoncode, for example pycode
or pyverbatim
as environments or \pyc
or \py
as commands.
org-mode
source blocks (it also generates TeX), but this adds another layer of indirection on top of LaTeX, and sometimes makes it difficult to affect the TeX code being generated.