저는 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
.
MWE
\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
나는 을 선호 하지만, -package 에 대한 좋은 프런트엔드인 을 minted
사용할 수도 있습니다 .pythonhighlight
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 libraray 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}