新聞中心
在LaTeX中插入Python代碼,可以使用listings宏包,需要在文檔的導(dǎo)言區(qū)(preamble)中加載listings宏包,并設(shè)置一些參數(shù),以便正確地顯示Python代碼,使用lstlisting環(huán)境插入Python代碼。

寒亭網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,寒亭網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為寒亭近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的寒亭做網(wǎng)站的公司定做!
以下是一個(gè)簡(jiǎn)單的示例:
1、在導(dǎo)言區(qū)加載listings宏包:
usepackage{listings}
2、設(shè)置Python代碼的參數(shù):
lstset{
language=Python,
basicstyle=ttfamily,
keywordstyle=color{blue}bfseries,
commentstyle=color{green!50!black},
stringstyle=color{red}ttfamily,
numbers=left,
numberstyle=smallcolor{gray},
stepnumber=1,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
tabsize=2,
captionpos=b,
breaklines=true,
breakatwhitespace=false,
escapeinside={(*@}{@*)},
}
3、使用lstlisting環(huán)境插入Python代碼:
begin{lstlisting}
def hello_world():
print("Hello, World!")
hello_world()
end{lstlisting}
4、將以上內(nèi)容整合到一個(gè)LaTeX文件中:
documentclass{article}
usepackage{listings}
lstset{
language=Python,
basicstyle=ttfamily,
keywordstyle=color{blue}bfseries,
commentstyle=color{green!50!black},
stringstyle=color{red}ttfamily,
numbers=left,
numberstyle=smallcolor{gray},
stepnumber=1,
numbersep=5pt,
backgroundcolor=color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
tabsize=2,
captionpos=b,
breaklines=true,
breakatwhitespace=false,
escapeinside={(*@}{@*)},
}
begin{document}
begin{lstlisting}
def hello_world():
print("Hello, World!")
hello_world()
end{lstlisting}
end{document}
將以上內(nèi)容保存為一個(gè)LaTeX文件(python_code.tex),然后使用pdflatex或xelatex編譯該文件,即可生成一個(gè)包含Python代碼的高亮顯示的PDF文件。
文章題目:latex插入python代碼
本文來(lái)源:http://www.dlmjj.cn/article/dpjjpes.html


咨詢
建站咨詢
