如何在 LaTeX 中高效率地呈現 Python 程式碼片段?

如何在 LaTeX 中高效率地呈現 Python 程式碼片段?

我對 LaTeX 很陌生,不知道如何在 LaTeX 中以專業的方式呈現這樣的簡單 Python 程式碼。擁有您的解決方案和範例將對我的學習和進步有很大幫助。

if transactions: Transaction.create_transactions() # if transactions = "true"
node.generate_emptyState() # empty state for all nodes
S.initial_events() # initiate initial events to start with

while not queue.isEmpty() and clock <= targetTime:
    next_e = queue.get_next_event()
    clock = next_e.time # move clock to the time of the event
    Event.execute_event(next_e)
    Queue.remove_event(next_e)

print results

答案1

這是我喜歡做的方式。當然,您可以更改顏色、字體大小、邊框等。

你需要皮格門斯使用minted.

在此輸入影像描述

微量元素

\documentclass[11pt,a4paper]{report}
\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}
if transactions: Transaction.create_transactions() # if transactions = "true"
node.generate_emptyState() # empty state for all nodes
S.initial_events() # initiate initial events to start with

while not queue.isEmpty() and clock <= targetTime:
      next_e = queue.get_next_event()
      clock = next_e.time # move clock to the time of the event
      Event.execute_event(next_e)
      Queue.remove_event(next_e)

print results
\end{mintedbox}

\end{document}

答案2

我更喜歡minted,但您也可以使用pythonhighlight,這是 -package 的一個很好的前端listings

\documentclass{article}
\usepackage{pythonhighlight}

\begin{document}
\begin{python}
if transactions: Transaction.create_transactions() # if transactions = "true"
node.generate_emptyState() # empty state for all nodes
S.initial_events() # initiate initial events to start with

while not queue.isEmpty() and clock <= targetTime:
      next_e = queue.get_next_event()
      clock = next_e.time # move clock to the time of the event
      Event.execute_event(next_e)
      Queue.remove_event(next_e)

print results
\end{python}
\end{document}

在此輸入影像描述

listings另一種無需過多擺弄就能獲得良好結果的方法是solarzied-包裹

\documentclass{article}
\usepackage{solarized-light}

\begin{document}
\begin{lstlisting}[language=python]
if transactions: Transaction.create_transactions() # if transactions = "true"
node.generate_emptyState() # empty state for all nodes
S.initial_events() # initiate initial events to start with

while not queue.isEmpty() and clock <= targetTime:
      next_e = queue.get_next_event()
      clock = next_e.time # move clock to the time of the event
      Event.execute_event(next_e)
      Queue.remove_event(next_e)

print results
\end{lstlisting}
\end{document}

在此輸入影像描述

答案3

您可以使用{Piton}擴展名piton.它使用 LuaLaTeX 的 Lua 庫 LPEG(並且需要 LuaLaTeX)。不需要外部程式。

\documentclass{article}
\usepackage{xcolor}
\usepackage{piton}

\begin{document}

\begin{Piton}
from math import pi

def arctan(x,n=10):
   """Compute the mathematical value of arctan(x)

   n is the number of terms in the sum
   """
    if x < 0:
        return -arctan(-x) # recursive call
    elif x > 1: 
        return pi/2 - arctan(1/x) 
        #> (we have used that $\arctan(x)+\arctan(1/x)=\frac{\pi}{2}$ for $x>0$)
    else: 
        s = 0
        for k in range(n):
            s += (-1)**k/(2*k+1)*x**(2*k+1)
        return s 
\end{Piton}

\end{document}

上述程式碼的輸出

可以與 結合使用tcolorbox

\documentclass{article}
\usepackage{xcolor}
\usepackage{piton}
\usepackage{tcolorbox}

\NewPitonEnvironment{Python}{}
  {\begin{tcolorbox}}
  {\end{tcolorbox}}

\begin{document}

\begin{Python}
from math import pi

def arctan(x,n=10):
   """Compute the mathematical value of arctan(x)

   n is the number of terms in the sum
   """
    if x < 0:
        return -arctan(-x) # recursive call
    elif x > 1: 
        return pi/2 - arctan(1/x) 
        #> (we have used that $\arctan(x)+\arctan(1/x)=\frac{\pi}{2}$ for $x>0$)
    else: 
        s = 0
        for k in range(n):
            s += (-1)**k/(2*k+1)*x**(2*k+1)
        return s 
\end{Python}

\end{document}

第二段程式碼的輸出

相關內容