latexbeamer

Displaying neat python code in beamer LaTeX


I've recently struggled on how to display pretty code in LaTeX, one which I found useful was using 'minted' library with this syntax:

\documentclass[13pt]{beamer}
\usetheme{Copenhagen}

\setbeamertemplate{page number in head/foot}[totalframenumber]

\usepackage{tcolorbox}
\tcbuselibrary{minted,breakable,xparse,skins}



\definecolor{bg}{gray}{0.95}
\DeclareTCBListing{mintedbox}{O{}m!O{}}{%
  breakable=true,
  listing engine=minted,
  listing only,
  minted language=#2,
  minted style=default,
  minted options={%
    linenos,
    gobble=0,
    breaklines=true,
    breakafter=,,
    fontsize=\small,
    numbersep=8pt,
    #1},
  boxsep=0pt,
  left skip=0pt,
  right skip=0pt,
  left=25pt,
  right=0pt,
  top=3pt,
  bottom=3pt,
  arc=5pt,
  leftrule=0pt,
  rightrule=0pt,
  bottomrule=2pt,
  toprule=2pt,
  colback=bg,
  colframe=orange!70,
  enhanced,
  overlay={%
    \begin{tcbclipinterior}
    \fill[orange!20!white] (frame.south west) rectangle ([xshift=20pt]frame.north west);
    \end{tcbclipinterior}},
  #3}



\begin{document}


\begin{mintedbox}{python}

import matplotlib.pyplot as plt
import numpy as np
import math
import random

#Parametres generales

nombrePietons = 20
largeurPiece = 5 
longeurPiece = 5 
k=1.2*1e2  # coeff de repulsion
h=10**(-2) # pas du temps
taux = 0.5 #temps de relaxation

#parametres de l'interaction psychologique du pieton avec son entourage
A=0.5 # permet de considerer le caractere anisotropique du pieton
B=1   # la portee de l'interaction repulsive

#parametres de la force de formation des sous-groupe:

B1=B2=B3=4 # les amplitudes des forces d'interactions
q1=q2=1    # q1=q2=0 s'il n'y'a pas un sous-groupe a former
ai=0       # decrit l'orientation de la tete du pieton par rapport a la direction de son mouvement
\end{mintedbox}
\end{document}

which gave the next snippet:

enter image description here

the result was nearly satisfying but yet came up with a crucial problem: In this way of displaying code, the enumeration in footline stops counting, as shown in the snippet above.

i hope if anyone can help with enumeration, and i will be very grateful


Solution

  • You should never place any content outside of a frame environment in beamer. If you use a frame environment, the frame number in the footline will work as expected:

    % !TeX program = txs:///arara
    % arara: pdflatex: {synctex: on, interaction: nonstopmode, shell: yes}
    
    \documentclass[13pt]{beamer}
    \usetheme{Copenhagen}
    
    \setbeamertemplate{page number in head/foot}[totalframenumber]
    
    \usepackage{tcolorbox}
    \tcbuselibrary{minted,breakable,xparse,skins}
    
    
    
    \definecolor{bg}{gray}{0.95}
    \DeclareTCBListing{mintedbox}{O{}m!O{}}{%
      breakable=true,
      listing engine=minted,
      listing only,
      minted language=#2,
      minted style=default,
      minted options={%
        linenos,
        gobble=0,
        breaklines=true,
        breakafter=,,
        fontsize=\small,
        numbersep=8pt,
        #1},
      boxsep=0pt,
      left skip=0pt,
      right skip=0pt,
      left=25pt,
      right=0pt,
      top=3pt,
      bottom=3pt,
      arc=5pt,
      leftrule=0pt,
      rightrule=0pt,
      bottomrule=2pt,
      toprule=2pt,
      colback=bg,
      colframe=orange!70,
      enhanced,
      overlay={%
        \begin{tcbclipinterior}
        \fill[orange!20!white] (frame.south west) rectangle ([xshift=20pt]frame.north west);
        \end{tcbclipinterior}},
      #3,
    }
    
    
    \begin{document}
    
    \begin{frame}[fragile,allowframebreaks]
    \begin{mintedbox}{python}[break at=.8\textheight]
    
    import matplotlib.pyplot as plt
    import numpy as np
    import math
    import random
    
    #Parametres generales
    
    nombrePietons = 20
    largeurPiece = 5 
    longeurPiece = 5 
    k=1.2*1e2  # coeff de repulsion
    h=10**(-2) # pas du temps
    taux = 0.5 #temps de relaxation
    
    #parametres de l'interaction psychologique du pieton avec son entourage
    A=0.5 # permet de considerer le caractere anisotropique du pieton
    B=1   # la portee de l'interaction repulsive
    
    #parametres de la force de formation des sous-groupe:
    
    B1=B2=B3=4 # les amplitudes des forces d'interactions
    q1=q2=1    # q1=q2=0 s'il n'y'a pas un sous-groupe a former
    ai=0       # decrit l'orientation de la tete du pieton par rapport a la direction de son mouvement
    \end{mintedbox}
    \end{frame}
    \end{document}
    

    enter image description here