2

With the package Pythontex, I want to input a Python file into a pyverbatim environment :

For example, I tried the code :

\begin{pyverbatim}
\input{file_1.py}
\end{pyverbatim}

where file_1.py is a Python file .

But I obtained as result:

\input{file_1.py}

and not the file_1.py's content.

How can I do it?

Update. I tried:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pythontex}
\usepackage{verbatim}

\begin{document}

\begin{pyverbatim}
\VerbatimInput{contenus/fichier_1.py}
\end{pyverbatim}

\end{document}

and I obtained

\VerbatimInput{contenus/fichier_1.py}

as result.

egreg
  • 1,121,712
Xavier ANSIAUX
  • 107
  • 1
  • 8

2 Answers2

3

I guess you want \inputpygments:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pythontex}

\begin{document}

Here's the code in the document
\begin{pyverbatim}
def SI(var, unit):
        return '\\SI{' + str(var) + '}{' + unit + '}'
\end{pyverbatim}
and here we load it from a file
\inputpygments{python}{file_1.py}

\end{document}

enter image description here

egreg
  • 1,121,712
1

You can use a file handler to read code in source and then use it with pyverbatim :

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pythontex}

\begin{document}

\begin{pycode}[temp] 
fh = open("file_1.py")
code = fh.read() 
fh.close()
\end{pycode}

\begin{pysub}[temp] 
  \begin{pyverbatim}
!{code}    
  \end{pyverbatim}
\end{pysub}

\end{document}
david
  • 215