8

I'm trying to add a language (Scheme) in the lstlisting package.

\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]",
}

But, it does when I use a lstlisting it does not recognize the keyword with "!", "?", "-"...

Do you know how can I fix it?

Thank you,

2 Answers 2

4

Andreas Stuhlmüller (stuhlmueller) in github developed a Scheme syntax highlighting for the LaTeX listings package. Please, see:

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

Another possibility is to use SLaTeX, available in http://evalwhen.com/slatex/slatxdoc.html

At http://evalwhen.com/slatex/slatxdoc-Z-H-3.html#node_sec_3 there are examples of use of the "?" for word termination.

2
  • Thanks, now it is better. But it still do not recognize the "?" ending words: like null?, list?, eq?, etc etc, also if i add them in the morekeywords list. Any idea?
    – user12818
    Apr 25, 2012 at 15:50
  • Did you try escaping the question marks? e.g. positive\??
    – gmfawcett
    Apr 16, 2013 at 17:14
8

If you want ?, !, -, etc. to be allowed in keywords, you need to make them letters, using the listings key called alsoletter, like so

alsoletter=!?-,

I've shamelessly taken the listings language definition for Scheme that is available at the end of the link posted in Papiro's answer, added your keywords to it, and made ?, !, - letters. See below.

enter image description here

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .