4

I want to display an interactive Python session inside a box in BEAMER.

Expected result

As my presentation has many slides like this I thought it would be convenient to create a new environment, but when I try to compile the following code:

\documentclass{beamer}

\usepackage{pythontex} \usepackage{tcolorbox}

\newenvironment{boxsession} { \begin{tcolorbox}[colframe=blue,colback=blue!5!white,notitle,size=small] \begin{pyconsole} } { \end{pyconsole} \end{tcolorbox} }

\begin{document}

\begin{frame}[fragile] \frametitle{Lists} \begin{boxsession} foo = [1, 2, 3] bar = foo baz = foo[:] foo[-1] = -99 foo bar baz \end{boxsession} \end{frame}

\end{document}

I get:

Runaway argument?
! File ended while scanning use of \FancyVerbGetLine.
<inserted text> 
                \par 
l.32 \end{frame}

?

I also tried to use environ to no avail:

\usepackage{environ}

\NewEnviron{boxsession} {\begin{tcolorbox}[colframe=blue,colback=blue!5!white,notitle,size=small] \begin{pyconsole} \BODY \end{pyconsole} \end{tcolorbox}}

Any ideas how to get around this error?

Tonechas
  • 976

1 Answers1

4

Since you are processing text, that contains special characters, you need to use \VerbatimEnvironment:

\documentclass{beamer}

\usepackage{pythontex} \usepackage{tcolorbox}

\newenvironment{boxsession} {\VerbatimEnvironment\begin{tcolorbox}[colframe=blue,colback=blue!5!white,notitle,size=small]\begin{pyconsole}} {\end{pyconsole}\end{tcolorbox}}

\begin{document}

\begin{frame}[fragile] \frametitle{Lists} \begin{boxsession} foo = [1, 2, 3] bar = foo baz = foo[:] foo[-1] = -99 foo bar baz \end{boxsession} \end{frame}

\end{document}

enter image description here


Update: Changing the font

pyconsole uses fancyvrb internally (see sec. 10.4.6 of the pythontex manual), so you have to pass through font settings that are understood by fancyvrb:

\documentclass{beamer}

\usepackage{pythontex} \usepackage{tcolorbox}

\newenvironment{boxsession}[1][fontsize=\normalsize] {\VerbatimEnvironment\begin{tcolorbox}[colframe=blue,colback=blue!5!white,notitle,size=small]\begin{pyconsole}[][#1]} {\end{pyconsole}\end{tcolorbox}}

\begin{document}

\begin{frame}[fragile] \frametitle{Lists} \begin{boxsession}[fontsize=\tiny] foo = [1, 2, 3] bar = foo baz = foo[:] foo[-1] = -99 foo bar baz \end{boxsession} \end{frame}

\end{document}

enter image description here

DG'
  • 21,727
  • Would it be possible to change the font size through an optional parameter? I tried this \newenvironment{boxsession}[1][\normalsize] but got an error: \FV@Error ... {FancyVerb Error: \space \space #1 } – Tonechas Jan 29 '21 at 18:21
  • @Tonechas – See update – DG' Jan 29 '21 at 21:00
  • 1
    You made my day! I wish I could upvote more than once. – Tonechas Jan 29 '21 at 21:30