2

I am using PythonTex with MacTex, TexShop and anaconda3 : all versions are up-to-date, and pythontex-gallery is compiled without any problem.

Now, as I am a teacher, I want to use beamer (I use the [fragile] option for frames) and specially the \pause or \overlay commands of beamer, with PythonTex in console mode.

Here is the code of what I'd like to see, using the PythonTex commands and the \pause commands, and not a lot of verbatims !

\documentclass{beamer}



\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}


\begin{document}
\begin{frame}[fragile]
\frametitle{Exercises}
What is the result in Python 2 of:
\begin{verbatim}
>>> 7 / 2
\end{verbatim}
\pause
\begin{verbatim}
 3
 \end{verbatim}
\pause
\begin{verbatim}
>>> 7./2.
\end{verbatim}
\pause
\begin{verbatim}
3.5
\end{verbatim}

\pause
What will be the last output?
\begin{verbatim}
>>> L = [5, 3, 42, 1]
>>> L[1:4]
\end{verbatim}
 \pause
 \begin{verbatim}
  [3, 42, 1]
 \end{verbatim}


\end{frame}
\end{document}

Thanks a lot

G.

5

I've provided an example below that will reveal one console line at a time. This is probably the best that can be done, at least without a lot of work. Getting this to work required patching beamer in the preamble.

Automatically inserting a \pause after each console command, before its output, would be complicated. You would have to create a wrapper for Python's code module (which is what PythonTeX uses internally) that would highlight each command and output separately, inserting \pause at appropriate points. And then you would have to connect this custom module into PythonTeX.

However, if simply displaying one line of a console session at a time is sufficient (which is what your example did) the following will work. This relies on redefining \FancyVerbFormatLine from fancyvrb, so that a \pause is inserted at the end of each line of console content.

Just doing that alone gives you the result you want, but you would have to run PythonTeX and then recompile the document for each line of console content, which is obviously unacceptable. This is due to the way that beamer deals with verbatim content; it keeps re-reading verbatim content for each overlay, and each re-read makes PythonTeX think that there is more code to execute. Fixing this requires resetting the PythonTeX counter that governs console code before each re-read. Doing that requires hacking the beamer internals. I've patched the beamer internals in the example below, so that PythonTeX only needs to be run a single time.

\documentclass{beamer}

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

\makeatletter
\AtBeginEnvironment{pyconsole}{%
  \renewcommand{\FancyVerbFormatLine}[1]{#1\pause}}

% Need to go ahead and create a counter for each console session
% Proceed assuming only the default
\newcounter{pytx@pycon@default@default}
% Need temp counter for resetting console counter, since the counter
% is incremented each time beamer re-reads verbatim content
\newcounter{pythontex@temp}
\BeforeBeginEnvironment{frame}{%
  \setcounter{pythontex@temp}{\value{pytx@pycon@default@default}}}

% Need a custom version of beamer's verbatim input routine
% The original is from beamerbaseframe.sty
\def\beamer@@@frameinput<#1>[#2][#3]{%
  \global\advance\c@framenumber by-1\relax%
  \edef\beamer@temp{\noexpand\frame<#1>[#2][{#3,fragile=false}]%
    {\begingroup
      % Reset counter
      \setcounter{pytx@pycon@default@default}{\value{pythontex@temp}}%
      \noexpand\input{\beamer@verbfilename}\endgroup}}%
  \beamer@temp%
  \ifx\beamer@frameenvironmentsubst\beamer@frametext%
  \else%
    \expandafter\endgroup%
  \fi%
  \beamer@reseteecodes}
\makeatother

\begin{document}

\begin{frame}[fragile]
\frametitle{Exercises}
What is the result in Python 2 of:
\begin{pyconsole}
7 / 2
7./2.
\end{pyconsole}

What will be the last output?
\begin{pyconsole}
L = [5, 3, 42, 1]
L[1:4]
\end{pyconsole}
\end{frame}

\end{document}
  • Thanks for your quick answer : despite the power of beamer and PythonTex, it is still a bit too long (and too complicated for me), as you say, to do it that way... So I will use verbatim a lot ! – guillaume Aug 6 '14 at 15:56
  • @guillaume What part is too long and complicated? The example seems to do what you want--the only real limitation is that you have to reveal a single line at a time, instead of a whole chunk of output in cases with multiline output. If there are other issues, it may be possible to fix them. – G. Poore Aug 6 '14 at 16:13
  • Ok, thanks, I hadn't understood everything at first, but it seems to be ok : thanks again a lot ! – guillaume Aug 7 '14 at 7:25
  • @guillaume Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. – jub0bs Aug 7 '14 at 21:55
  • @G.Poore Would it be possible to wrap all this inside an environment (say \begin{FrameWithPauseOnPyconsole} \frametitle{Exercises} What is the result in Python 2 of: \begin{pyconsole} 7 / 2 7./2. \end{pyconsole} \end{FrameWithPauseOnPyconsole})? – cjorssen Sep 4 '15 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.