otherkeywords
我尝试自定义代码列表以支持最适合我的伪代码格式。使用或很容易添加关键字morekeywords
,但相反, 并deletekeywords
没有按预期工作。如果将单词的词干定义为关键字,则该单词将被突出显示。例如,如果创造位于morekeywords
,则创建也突出显示,除了d最后。将创建添加到deletekeywords
列表中并没有帮助。
请建议我如何解决这个问题。
\documentclass[12pt]{article}
\usepackage{fancyhdr}
\usepackage[ansinew]{inputenc} %Windows-1252 encoding
\usepackage{listings}
\usepackage[english]{babel}
\usepackage[usenames,dvipsnames]{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{lightgray}{rgb}{0.9,0.9,0.9}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{numbers=left,
numbersep=5pt,
basicstyle=\sffamily\tiny,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
backgroundcolor=\color{lightgray},
frame=single,
columns=fullflexible
}
\begin{document}
\title{}
\author{}
\maketitle
\section{somesection}
\lstset{language=Python, caption=somecaption, otherkeywords={smooth}, deletekeywords={smoothed, is, not}}
\begin{figure}[h]
\begin{lstlisting}
smooth is welcome
smoothed is not
\end{lstlisting}
\end{figure}
\end{document}
答案1
而是使用keywords={smooth}
而不是otherkeywords
。根据listings
文档, 后者
定义包含其他字符或以数字开头的关键字。
以下是更新的 MWE,显示了所需的输出:
\documentclass[12pt]{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage[usenames,dvipsnames]{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{lightgray}{rgb}{0.9,0.9,0.9}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{numbers=left,
numbersep=5pt,
basicstyle=\sffamily\tiny,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
backgroundcolor=\color{lightgray},
frame=single,
columns=fullflexible
}
\begin{document}
\title{}
\author{}
\maketitle
\section{somesection}
\lstset{language=Python, caption=somecaption, keywords={smooth}}
\begin{figure}[h]
\begin{lstlisting}
smooth is welcome
smoothed is not
\end{lstlisting}
\end{figure}
\end{document}
上述 MWE 指定了keyword
s,但随后也覆盖了设置支持的现有关键字language
。要维护现有集合并添加更多/删除一些,请使用morekeywords={smooth}, deletekeywords={is,not}
。