inpytex
is a Python-based pre-processor for (La)TeX files. It
executes Python snippets in comments and inserts their output under
the comment.
For example, the following LaTeX file:
\begin{itemize}
%%% inpytex My first snippet
% >>> for i in range(3):
% ... emit('\item %s' % i)
%%% end
\end{itemize}
inpytex
recognizes Python snippets that are enclosed between %%% inpytex
and %%% end
, and written in the doctest syntax (so that
there is no indentation ambiguity). A label (as My first snippet
above) can be added after %%% inpytex
to recognize the snippet in
the script log.
By running inpytex
on the file, the snippet gets expanded into the
following:
\begin{itemize}
%%% inpytex My first snippet
% >>> for i in range(3):
% ... emit('\item %s' % i)
%%% AUTOGENERATED, DO NOT EDIT UNTIL "%%% end"
\item 0
\item 1
\item 2
%%% end 99490253c65b69eb7112445388fb1c21
\end{itemize}
Note that the file is modified in-place, so you may want to commit
the file to a VCS before running inpytex
. The rationale behind this
behavior is the following:
-
The Python snippet serves as a comment describing the auto-generated code below it. Also, it is less likely that the Python snippet and the generated code will lose sync.
-
Once
inpytex
is run on the file, it is not needed anymore unless the snippets are changed. This is useful when working with other people.
If the python snippets are modified, the generated code is
overwritten. However, if the generated code is modified manually,
inpytex
detects it by checking the checksum after end
, and it
refuses to overwrite the code. To force overwriting, just remove the
checksum.
This script was written while working on my master's thesis to automate the creation of some TiKz figures and tables. It has not been used or maintained since then (except minor cosmetic changes). Use it at your own risk.