I am writing a package that optionally uses the pythontex package. The pythontex package defines an environment pycode which allows you to execute Python code before \begin{document}. As far as I can see, putting the pycode environment within a macro is impossible, so I have to put it in regardless of whether the user wants to use Pythontex or not, and define or redefine it in case the user doesn't want to use it. In that case, I would have to define it in a way that ignores all the code written within the environment and that can be put into the preamble (or rather, can be executed at the loading of my package.)
MWE:
\RequirePackage{etoolbox}
\RequirePackage{xstring}
\RequirePackage{letltxmacro}
\newcommand{\@pythonbool}{false} %This tells us whether the python option is enabled or not.
\newcommand{\@pycodedefined}{false} %This tells us whether the environment pycode is defined at the loading of this style file or not.
\DeclareOption{python}{
\renewcommand{\@pythonbool}{true}
}
\ProcessOptions\relax
\expandafter\ifstrequal\expandafter{\@pythonbool}{false}{
\ifcsmacro{pycode}{
\renewcommand{\@pycodedefined}{true}
\LetLtxMacro{\@oldpy}{\pycode}
\LetLtxMacro{\@oldendpy}{\endpycode}
\renewenvironment{pycode}{}{} % If pycode is defined (because Pythontex is used), but the package should not use python, this will completely disregard anything in a pycode environment. We will correctly redefine pycode after the code to be disregarded. This is neccessary because we can't put the pycode environment inside a conditional. Arguably, we could also let the code execute, since it doesn't print anything, and so will not change the output, but this is cleaner since we avoid possible naming conflicts. I don't know if it also makes python run less often, which would be a plus too.
}{
\newenvironment{pycode}{}{} % If pycode is undefined (because Pythontex isn't used), this prevents errors upon Latex encountering the pycode environment, which we otherwise can't, since we can't simply put the pycode environment into a conditional.
}
}{ \RequirePackage{pythontex}
}
\begin{pycode}
some code
\end{pycode}
\expandafter\ifstrequal\expandafter{\@pycodedefined}{true}{
\LetLtxMacro{\pycode}{\@oldpy}
\LetLtxMacro{\endpycode}{\@oldendpy}}{}
The specific point I'm interested in is how I can stop my new definition of the environment from trying to print the code part at all.
Edit: The best way to do this seems to be a command that tells latex to ignore everything between it and its counterpart, but I wouldn't know how to define one.
environand\RenewEnviron{pycode}{}probably works – cgnieder Mar 30 '17 at 09:59