
我想知道如何在minted
python 程式碼區塊中新增標題列?這是我想使用的標題列的範例:
我嘗試使用來自的程式碼這個解決方案但我無法在該解決方案中發布程式碼以使用 WinEdt 和pdftex
-shell-escape
.當我運行發布的程式碼時解決方案,我收到此錯誤:
錯誤:無法讀取 infile:[Errno 2] 沒有這樣的檔案或目錄:'codeboxes_1.pyg'
!包鑄造錯誤:缺少 Pygments 輸出; \inputminted 可能獲得了一個不存在的檔案—否則,您可能需要 outputdir 套件選項,或者可能正在使用不相容的建置工具,或者可能正在使用具有遺失檔案的 freezecache。
到目前為止,這是我的程式碼:
程式碼
\documentclass[letter, 12pt]{report}
%########################### FOR PYTHON ####################################
% https://www.overleaf.com/learn/latex/Code_Highlighting_with_minted
\usepackage{minted}
\usepackage{xcolor} % to access the named colour LightGray
\definecolor{LightGray}{gray}{0.9}
\begin{document}
\begin{minted}
[
frame=lines,
framesep=2mm,
baselinestretch=1.2,
bgcolor=LightGray,
fontsize=\footnotesize,
firstnumber = 41,
linenos
]
{python}
fig = plt.subplots(figsize=(12,2))
ax = plt.subplot(1,1,1)
ax.plot(times, x)
ax.grid(True)
plt.ylabel('amplitude [in A.U.]', fontsize=14)
plt.xlabel('time [in sec]', fontsize=14)
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()
\end{minted}
\end{document}
答案1
很難看出您發布的程式碼如何產生您發布的錯誤訊息 - 或者您在嘗試連結解決方案中的程式碼時是否得到了該訊息?看起來您正在嘗試從文件中輸入代碼,但該文件不存在。
您發布的實際程式碼可以為我編譯,但沒有標題列;為此,您需要 tcolorbox 套件。
以下是嘗試修改您連結到的解決方案中的程式碼以用於您的 python 程式碼。我沒有關心該解決方案中定義的其他環境:只是產生看起來像您所需的輸出的程式碼區塊的環境。
我在給定的程式碼中加入了第四個參數作為起始行號。
但是,它與您發布的程式碼風格仍然有很大不同:您想要進行調整嗎?
\documentclass[letter, 12pt]{report}
\newcounter{commentCount}
\newcounter{filePrg}
\newcounter{inputPrg}
\usepackage[dvipsnames]{xcolor}
\usepackage{minted}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{minted}
\usepackage{ifthen}
\usepackage{fontawesome}
\usepackage{tabularx}
\newcolumntype{\CeX}{>{\centering\let\newline\\\arraybackslash}X}%
\newcommand{\TwoSymbolsAndText}[3]{%
\begin{tabularx}{\textwidth}{c\CeX c}%
#1 & #2 & #3
\end{tabularx}%
}
\newtcblisting[use counter=inputPrg, number format=\arabic]{codeInput}[4]{
listing engine=minted,
minted language=#1,
minted options={autogobble,linenos,breaklines, firstnumber={#4}},
listing only,
size=title,
arc=1.5mm,
breakable,
enhanced jigsaw,
colframe=brown,
coltitle=White,
boxrule=0.5mm,
colback=white,
coltext=Black,
title=\TwoSymbolsAndText{\faCode}{%
\textbf{Input program \thetcbcounter}\ifthenelse{\equal{#2}{}}{}{\textbf{:} \textit{#2}}%
}{\faCode},
label=inputPrg:#3
}
\begin{document}
\begin{codeInput}{python}{A sample program.}{code01}{41}
fig = plt.subplots(figsize=(12,2))
ax = plt.subplot(1,1,1)
ax.plot(times, x)
ax.grid(True)
plt.ylabel('amplitude [in A.U.]', fontsize=14)
plt.xlabel('time [in sec]', fontsize=14)
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()
\end{codeInput}
\end{document}