Syntaxhighlighting mit Pygments und LaTeX (Beamer-Version)

Das minted-Syntaxhighlighting für eingebetteten Code funktioniert natürlich auch für Beamer-Präsentationen. (Hinweis: Python und pygments müssen installiert sein, --shell-escape muss aktiviert sein)

\documentclass[12pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
 
\usepackage{xcolor}
\definecolor{colBack}{rgb}{1,1,0.8}
 
\usepackage{minted}
\setminted[python]{frame=lines, framesep=2mm, baselinestretch=1.2, bgcolor=colBack,fontsize=\footnotesize,linenos}
\setminted[text]{frame=lines, framesep=2mm, baselinestretch=1.2, bgcolor=colBack,fontsize=\footnotesize,linenos}
 
\usepackage{fancyvrb}
\makeatletter
\newenvironment{pycode}[1]%
  {\xdef\d@tn@me{#1}\xdef\r@ncmd{python #1.py > #1.plog}%
  \typeout{Writing file #1}\VerbatimOut{#1.py}% 
  }
  {\endVerbatimOut %
 \toks0{\immediate\write18}%
 \expandafter\toks\expandafter1\expandafter{\r@ncmd}%
 \edef\d@r@ncmd{\the\toks0{\the\toks1}}\d@r@ncmd %
 \noindent Input
 \inputminted{python}{\d@tn@me.py}%
 \noindent Output
 \inputminted{text}{\d@tn@me.plog}%
}
\makeatother
 
\begin{document}
 
\begin{frame}[containsverbatim]
\frametitle{Python Code Evaluation}
 
\begin{pycode}{abc}
import pandas as pd
print(pd.__version__);
print(123+456)
\end{pycode}
 
\end{frame}
 
\end{document}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website