0

I want to display a snippet of Python code as a colored framed box in a beamer presentation. Besides, I'd like to save the snippet in a file. The code below gets the job done:

\documentclass{beamer}

\usepackage{xcolor}
\usepackage{minted}
\usepackage{tcolorbox}
\tcbuselibrary{minted}

\newtcblisting{pythoncode}[4][\normalsize]{%
  listing engine=minted,minted style=colourful, minted language=python,
  oversize, listing only,left=1mm,bottom=1mm,right=1mm,top=1mm,
  minted options={fontsize=#1},
  listing file={snippets/#2},
  colframe=#3,
  title={\color {#4} \texttt{#2}}}

\begin{document}

\begin{frame}[fragile]
\frametitle{Test}
\begin{pythoncode}[\small]{script.tex}{blue}{yellow}
def add(x, y):
    '''Add two numbers'''
    return x + y 

result = add(x, y)
print(result)
\end{pythoncode}
\end{frame}

\end{document}

output

However, if I try to change the extension of the output file from .tex to .py:

\begin{pythoncode}[\small]{myscript.py}{blue}{yellow}

I get the following error:

! I can't write on file `snippets/myscript.py'.
\tcbverbatimwrite ...mediate \openout \tcb@out #1 
                                                  \tcb@verbatim@begin@hook \...
l.2 ...honcode}[\small]{myscript.py}{blue}{yellow}

Please type another output file name
! Emergency stop.
\tcbverbatimwrite ...mediate \openout \tcb@out #1 
                                                  \tcb@verbatim@begin@hook \...
l.2 ...honcode}[\small]{myscript.py}{blue}{yellow}

!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on mwe.log.

How can I go about this?


EDIT

I tried other file extensions such as .c, .cpp, .m, and .java and the code works like a charm. It's only .py that produces the error.

I'm using MiKTeX 2.9 on a Windows 10 machine.

7
  • It works for me... Could you please show the output of \listfiles? Also, make sure you have a snippets folder. Dec 8, 2019 at 17:38
  • The snippets folder does exists and snippets/myscript.tex is saved without problem. You can download the full .log file from here.
    – Tonechas
    Dec 8, 2019 at 17:56
  • I don't see anything wrong with your log... But since you're using Windows, check if snippets/myscript.py is open in another program which might be locking the file (just guessing). Dec 8, 2019 at 18:08
  • snippets/myscript.py doesn't even exist. This code worked for me a couple of months ago. I've been struggling desperately to fix it for two days but I can't get my head around this :( Any other pointers/suggestions would be greatly appreciated.
    – Tonechas
    Dec 8, 2019 at 18:21
  • Usually ! I can't write on file means that something outside of TeX's control is preventing it from writing to the file. For example, if I change the onwership of the snippets folder to root (in Linux, I guess it's equivalent to Admin in Windows), then TeX can't write on it and that error shows up. To reduce variables, please try this code and tell me what happens: \newwrite\test\immediate\openout\test snippets/script.py\relax \csname stop\endcsname\bye Dec 8, 2019 at 19:01

0

Browse other questions tagged .