Goal: I want to create a command that executes pythontex code blocks from within a macro.
Application: I have a macro that typesets some equations. I'd like for a side-effect of their use to be the execution of static code within a specified environment.
This problem boils down to addressing "why doesn't verbatim work within...?" for this particular MWE. In this question Geoffrey Poore points out that these environments cannot be used inline, but this doesn't stop me from needing a solution. (e.g., place filecontents*
adjacent to the macro, pass the verbatim content of that into a comand that executes it within a session).
\documentclass{minimal}
\usepackage{pythontex}
\newcommand\runsSomeCode[0]{
\begin{sympycode}
j = 8
\end{sympycode}
}
\begin{document}
\runsSomeCode{}
\begin{equation}
j = \sympy{j}
\end{equation}
\end{document}
Regardless of whether or not a Verbatim
or sympycode
block is used, the result is the expected:
! FancyVerb Error:
Extraneous input `j = 8 \end {sympycode} {}' between \begin{sympycode}[<key=val
ue>] and line end
.
\FV@Error ... {FancyVerb Error:
\space \space #1
}
l.11 \runsSomeCode{}
desiderata for this solution
Runs the code: I am not looking for a text-formatting solution. I'm not looking for a mono-spaced font or for a code listing. I don't need python output at all.
Session-friendly: I have multiple sessions that may or may not require the same code to be executed in their context. Without this requirement, I'd simply create a python function in a
sympyblock
outside of my macro and invoke it via\sympy{}
or\py{}
.Must be invoked by a macro: I do not have the luxury of creating a custom environment in the top-level structure of my documents.
Must function in math mode
Avoid external input files: I build my documents using
.latexmkrc
, so solutions that remove temporary files at end of compilation are acceptable. usingfilecontents
to generate output is also fair game.
I have no concerns whatsoever if this causes my document to not be able to be processed by depythontex, at this time.