添加带有特殊字符的关键字

添加带有特殊字符的关键字

我正在尝试在 lstlisting 包中添加一种语言(Scheme)。

\section{Scheme}
\lstdefinelanguage{Scheme}
{morekeywords={,lambda, cond, case, display, let, import, quote, quasiquote, unquote,
define, begin, newline, if, list, apply, null?, car, cdr, or, not, and, for-each, 
make-vector, vector-length, vector-ref, vector-set!, eqv?, eq?, equal?, else, set!, 
define-record-type, fields, mutable, immutable, assert, parent, with-exception-handler, }
sensitive=false,
morecomment=[l]{;},
morecomment=[s]{/*}{*/},
morestring=[b]",
}

但是,当我使用 lstlisting 时,它无法识别带有“!”,“?”,“-”的关键字...

你知道我该如何修复它吗?

谢谢你,

答案1

github 上的 Andreas Stuhlmüller (stuhlmueller) 为 LaTeX 列表包开发了 Scheme 语法高亮功能。请参阅:

https://github.com/stuhlmueller/scheme-listings

另一种可能性是使用 SLaTeX,可在 http://evalwhen.com/slatex/slatxdoc.html

http://evalwhen.com/slatex/slatxdoc-ZH-3.html#node_sec_3有使用“?”作为单词终止的例子。

答案2

如果您希望关键字中允许使用?!、等,则需要使它们-字母,使用listings名为的键alsoletter,如下所示

alsoletter=!?-,

我厚着脸皮地抄袭了listingsScheme 的语言定义,它位于链接发布在 Papiro 的回答中,在其中添加了关键字,并创建了?!-个字母。见下文。

在此处输入图片描述

\documentclass{article}

\usepackage[T1]{fontenc}
\renewcommand{\ttdefault}{pcr}
\usepackage{xcolor}
\usepackage{textcomp}
\usepackage{listings}

\usepackage{filecontents}
\begin{filecontents*}{ycombinator.ss}
(eqv? 'a 'a)                           ==>  #t
(eqv? 'a 'b)                           ==>  #f
(eqv? 2 2)                             ==>  #t
(eqv? '() '())                         ==>  #t
(eqv? 100000000 100000000)             ==>  #t
(eqv? (cons 1 2) (cons 1 2))           ==>  #f
(eqv? (lambda () 1)
      (lambda () 2))                   ==>  #f
(eqv? #f 'nil)                         ==>  #f
(let ((p (lambda (x) x)))
  (eqv? p p))                          ==>  #t
\end{filecontents*}


\lstdefinelanguage{Scheme}{
  morekeywords=[1]{define, define-syntax, define-macro, lambda,
    define-stream, stream-lambda},
  morekeywords=[2]{begin, call-with-current-continuation, call/cc,
    call-with-input-file, call-with-output-file, case, cond,
    do, else, for-each, if,
    let*, let, let-syntax, letrec, letrec-syntax,
    let-values, let*-values,
    and, or, not, delay, force,
    quasiquote, quote, unquote, unquote-splicing,
    map, fold, syntax, syntax-rules, eval, environment, query,
    display, newline,list, apply, null?, car, cdr, for-each, 
make-vector, vector-length, vector-ref, vector-set!, eqv?, eq?, equal?, set!, 
define-record-type, fields, mutable, immutable, assert, parent, with-exception-handler,
  },
  morekeywords=[3]{import, export},
  alsoletter=?!-,
  alsodigit=\$\%&*+./:<=>@^_~,
  sensitive=false,
  morecomment=[l]{;},
  morecomment=[s]{/*}{*/},
  morestring=[b]",
  basicstyle=\small\ttfamily,
  keywordstyle=\bf\ttfamily\color[rgb]{0,.3,.7},
  commentstyle=\color[rgb]{0.133,0.545,0.133},
  stringstyle={\color[rgb]{0.75,0.49,0.07}},
  upquote=true,
  breaklines=true,
  breakatwhitespace=true,
  literate=*{`}{{`}}{1}
}

\begin{document}
\section{Scheme}
\lstinputlisting[language=Scheme]{ycombinator.ss}
\end{document}

相关内容