1

I want to use the same code in several pythontex environments:

\documentclass{article}
\usepackage{pythontex}

\begin{document}

\begin{pyconsole}
print('Hello \\LaTeX{} World!')
\end{pyconsole}

\begin{pycode}
print('Hello \\LaTeX{} World!')
\end{pycode} 

\begin{pyverbatim}
print('Hello \\LaTeX{} World!')
\end{pyverbatim}

\end{document}

Is it possible to use a savebox or something like that avoiding errors in correcting the code or simply make LaTeX code more readable?

1 Answer 1

1

There are many ways this could be done. At the cost of a few extra compiles beyond those normally needed for pythontex, here's an approach that uses pythontex environments to generate pythontex environments.

\documentclass{article}
\usepackage{pythontex}

\begin{document}

\begin{pycode}[template]
code = "print('Hello \\LaTeX{} World!')"
\end{pycode}

\begin{pysub}[template]
\begin{pyconsole}
!{code}
\end{pyconsole}

\begin{pycode}
!{code}
\end{pycode} 

\begin{pyverbatim}
!{code}
\end{pyverbatim}
\end{pysub}

\end{document}
1
  • Thank you @g-poore. I guess I should use triple-quotes for multilines code.
    – david
    Commented Jan 16, 2019 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .