我正在使用该包尝试格式化我的 R 代码,但我不喜欢某些被视为关键字的东西(用于配色方案)。但是,当我deletekeywords={*,<-}
在中指定时\lstset
,它拒绝删除它们。以下是代码:
\documentclass{scrartcl}
\usepackage{courier}
\usepackage{listings}
\usepackage{xcolor}
\xdefinecolor{gray}{rgb}{0.4,0.4,0.4}
\xdefinecolor{blue}{RGB}{58,95,205}% R's royalblue3; #3A5FCD
\lstset{ %
language=R, % the language of the code
basicstyle=\ttfamily\footnotesize, % the size of the fonts that are used for the code
deletekeywords={*, <-},
numbers=left, % where to put the line-numbers
numberstyle=\tiny\color{gray}, % the style that is used for the line-numbers
stepnumber=2, % the step between two line-numbers. If it's 1, each line
% will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))
tabsize=2, % sets default tabsize to 2 spaces
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
title=\lstname, % show the filename of files included with \lstinputlisting;
% also try caption instead of title
keywordstyle=\color{blue}, % keyword style
commentstyle=\color{gray}, % comment style
stringstyle=\color{gray}, % string literal style
escapeinside={}, % if you want to add a comment within your code
morekeywords={byrow} % if you want to add more keywords to the set
}
\begin{document}
\begin{lstlisting}[caption={A first example}, label=list:ex]
x <- c(1, 2, 3, 4, 5, 6)
xmat <- matrix(x, byrow = TRUE, nrow = 3)
xmat %*% t(xmat)
\end{lstlisting}
\end{document}
答案1
一些背景...
该listings
软件包有不同的机制来定义关键字。您最常使用的两个键是keywords
和morekeywords
。前者从头开始定义关键字列表(覆盖任何现有列表),而后者添加到现有列表(如果有)。您还可以使用 键删除现有关键字deletekeywords
。
此外,还有另一个关键点,叫做otherkeywords
,可用于定义特别的关键字。手册(第 4.18 节)对此进行了如下描述:
otherkeywords={
⟨关键词⟩}
定义包含其他字符或以数字开头的关键字。每个给定的“关键字”都以关键字样式打印,但不改变字符的“字母”、“数字”和“其他”状态。此键用于定义关键字,如
=>
、->
、-->
、--
、::
等。如果一个关键字是另一个关键字的子序列(如--
和-->
),则必须先指定较短的那个。
那么,这里发生了什么事?
被指控的“关键字”(*
和)由包在语言定义中<-
使用键定义(在otherkeywords
R
listings
lstdvrs.dtx 文件):
otherkeywords={!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,_,/}
无法使用该键删除使用该键定义的“其他关键字” deletekeywords
。
一个办法
遗憾的是,没有deleteotherkeywords
键。删除此类“其他关键字”的唯一方法是重新初始化与otherkeywords
键关联的值
otherkeywords={},
最好是在调用之后立即执行language=R
。
编辑:如果要保留这些“其他关键字”的子集,则需要从原始语言定义中复制该行,
otherkeywords={!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,_,/}
并根据您的喜好进行编辑。
完整代码
\documentclass{scrartcl}
\usepackage{courier}
\usepackage{listings}
\usepackage{xcolor}
\xdefinecolor{gray}{rgb}{0.4,0.4,0.4}
\xdefinecolor{blue}{RGB}{58,95,205}% R's royalblue3; #3A5FCD
\lstset{ %
language=R, % the language of the code
basicstyle=\ttfamily\footnotesize, % the size of the fonts that are used for the code
%otherkeywords={!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,_,/}, % default in the R language defined by the listings package
otherkeywords={}, % resets the list of "other keywords"
numbers=left, % where to put the line-numbers
numberstyle=\tiny\color{gray}, % the style that is used for the line-numbers
stepnumber=2, % the step between two line-numbers. If it's 1, each line
% will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))
tabsize=2, % sets default tabsize to 2 spaces
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
title=\lstname, % show the filename of files included with \lstinputlisting;
% also try caption instead of title
keywordstyle=\color{blue}, % keyword style
commentstyle=\color{gray}, % comment style
stringstyle=\color{gray}, % string literal style
escapeinside={}, % if you want to add a comment within your code
morekeywords={byrow} % if you want to add more keywords to the set
}
\begin{document}
\begin{lstlisting}[caption={A first example}, label=list:ex]
x <- c(1, 2, 3, 4, 5, 6)
xmat <- matrix(x, byrow = TRUE, nrow = 3)
xmat %*% t(xmat)
\end{lstlisting}
\end{document}