我定义了一个名为“\showLineNo”的新命令,并在“\lstdefinestyle”中使用,出现错误:
包裹清单错误:数字无未知。\lstset{style=customstyleone}
示例代码:
\documentclass{article}
\RequirePackage{listings}
\def\showLineNo{none}
\lstdefinestyle{customstyleone}{
numbers=\showLineNo,
numbersep=5pt
}
\lstset{style=customstyleone}
\begin{document}
\begin{lstlisting}[language=Python, caption=python code example]
def incmatrix(genl1,genl2):
m = len(genl1)
n = len(genl2)
M = None #to become the incidence matrix
VT = np.zeros((n*m,1), int) #dummy variable
\end{lstlisting}
\end{document}
但工作如下:
\documentclass{article}
\RequirePackage{listings}
\lstdefinestyle{customstyleone}{
numbers=none,
numbersep=5pt
}
\lstset{style=customstyleone}
\begin{document}
\begin{lstlisting}[language=Python, caption=python code example]
def incmatrix(genl1,genl2):
m = len(genl1)
n = len(genl2)
M = None #to become the incidence matrix
VT = np.zeros((n*m,1), int) #dummy variable
\end{lstlisting}
\end{document}
如何使用自定义命令传递给 lstdefinestyle?
答案1
该listings
包使用一种不扩展键值的机制numbers
来搜索有效值(而错误恢复会扩展该值)。因此,使用宏保存值的方式numbers
不起作用。
但是,我们可以定义一个替代键(如果它尚不存在),以扩展其值并将其传递给该numbers
键。您可以将以下内容视为一个小窍门,因为我们使用listings
keyval
-namespace(或扩展?)。
\documentclass{article}
\RequirePackage{listings}
\def\showLineNo{none}
\makeatletter
\@ifundefined{KV@lst@numbers-expanded}
{%
\define@key{lst}{numbers-expanded}
{\expandafter\KV@lst@numbers\expanded{{#1}}}%
}
{\GenericError{}{key numbers-expanded already defined}{}}
\makeatother
\lstdefinestyle{customstyleone}{
numbers-expanded=\showLineNo,
numbersep=5pt
}
\lstset{style=customstyleone}
\begin{document}
\begin{lstlisting}[language=Python, caption=python code example]
def incmatrix(genl1,genl2):
m = len(genl1)
n = len(genl2)
M = None #to become the incidence matrix
VT = np.zeros((n*m,1), int) #dummy variable
\end{lstlisting}
\end{document}