
在下面的程式碼中,第二個參數\examplecode
同時是檔案名稱和框架標題。當檔案名稱包含下劃線時,它會失敗。
\documentclass[a4paper,10pt,openany]{scrbook}
\usepackage{minted}
\usepackage{mdframed}
\newcommand{\examplecode}[2]
{
\mdfsetup{frametitle={\colorbox{white}{\space #2 \space}}}
\begin{mdframed}
\inputminted[linenos,fontsize=\footnotesize,baselinestretch=.7]{#1}{src/#2}
\end{mdframed}
}
\begin{document}
\examplecode{c}{testfile.c} % ok
\examplecode{c}{test_file.c} % error
\end{document}
我該如何處理#2,以便它可以用於這兩個目的?
答案1
您的程式碼失敗,因為您無法_
在文字模式下使用。最簡單的解決方案是\_
在命令中本地重新定義:
\newcommand{\examplecode}[2]{
\mdfsetup{frametitle={\colorbox{white}{\space #2 \space}}}
\begin{mdframed}
\def\_{_}
\inputminted[linenos,fontsize=\footnotesize,baselinestretch=.7]{#1}{src/#2}
\end{mdframed}
}
並在通話中使用它:
\examplecode{c}{text\_file.c}
答案2
我這樣做的另一種方法是將檔案名稱參數包裝在另一對大括號中,例如:
\inputminted[linenos,fontsize=\footnotesize,baselinestretch=.7]{#1}{{src/#2}}