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.

5
  • What package provides pyverbatim?
    – egreg
    Commented Mar 16, 2018 at 20:49
  • 2
    Why not simply \verbatiminput{file_1.py}?
    – egreg
    Commented Mar 16, 2018 at 21:40
  • Excuse me: It's with Pythontex. Commented Mar 17, 2018 at 6:48
  • "\begin{pyverbatim} \VerbatimInput{contenus/fichier_1.py} \end{pyverbatim}" doesn't work : It produces : "\VerbatimInput{contenus/fichier_1.py}" Commented Mar 17, 2018 at 7:10
  • Are you sure you don't want minted?
    – egreg
    Commented Mar 17, 2018 at 9:42

2 Answers 2

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

0
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}

You must log in to answer this question.

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