在Beamer LaTeX中展示整洁的Python代码。

huangapple go评论82阅读模式
英文:

Displaying neat python code in beamer LaTeX

问题

我最近在如何在LaTeX中显示漂亮的代码上遇到了困难,我发现使用'minted'库以以下语法很有用:

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

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

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

% 以下省略...

这个结果几乎令人满意,但仍然存在一个关键问题:在这种代码显示方式下,页脚中的编号停止计数,如上面的代码片段所示。

我希望有人能帮助解决编号的问题,我将不胜感激。

英文:

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:

在Beamer LaTeX中展示整洁的Python代码。

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

答案1

得分: 1

以下是您要翻译的内容:

"在 beamer 中,永远不应将任何内容放在帧环境之外。如果您使用帧环境,底部的帧编号将按预期工作:

% !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}

在Beamer LaTeX中展示整洁的Python代码。

英文:

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}

在Beamer LaTeX中展示整洁的Python代码。

huangapple
  • 本文由 发表于 2023年6月6日 05:59:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定