7

Ok, so here's my problem which I'm repeatedly gnawing on, whenever I try to typset source code. To this day, I have not found someone on the internet asking for the same problem.

I want to add a keywordsprefix so that everything starting with a & is highlighted as a keyword. Here's a MWE illustrating my problem

\documentclass{article}
\usepackage{listings, xcolor}

\begin{document}

\definecolor{lispgreen}{RGB}{154, 228, 151}
\definecolor{lightgray}{gray}{0.97}
\definecolor{violet}{rgb}{0.8, 0, 0.7}

\lstset{language=Lisp,
        basicstyle=\ttfamily\small,
        numbers=left,
        numberstyle=\tiny,
        keywordsprefix=&,
        keywordstyle=\color{lispgreen!70!black},
        stringstyle=\color{violet},
        showstringspaces=false,
        backgroundcolor=\color{lightgray},
        frame=single}

\begin{lstlisting}
(defmacro simple-incf (var &optional (amount 1)) 
    (list setq var (list + var amount)))
\end{lstlisting}

\end{document}

Now, a in a statement like &optional, everything except the ampersand is highlighted, which looks profoundly weird to me. Maybe other people use other editors or settings, but by default, my vim will highlight also the keyword prefix itself so this is what I see daily.

My question now is, can I somehow tell listings to either extend the highlighting to include also the prefixes or, alternatively, just brutely make that one character & highlighted everywhere (which in this case would also solve the problem)?

1

1 Answer 1

6

If you want to highlight the keyword prefix too you have to change the category from other to letter. See table 2: Standard character table of the listings documentation.

To achieve this you can use the option alsoletter:

\documentclass{article}
\usepackage{listings, xcolor}

\begin{document}

\definecolor{lispgreen}{RGB}{154, 228, 151}
\definecolor{lightgray}{gray}{0.97}
\definecolor{violet}{rgb}{0.8, 0, 0.7}

\lstset{language=Lisp,
        basicstyle=\ttfamily\small,
        numbers=left,
        numberstyle=\tiny,
        keywordsprefix=\&,alsoletter=\&,% 
        keywordstyle=\color{lispgreen!70!black},
        stringstyle=\color{violet},
        showstringspaces=false,
        backgroundcolor=\color{lightgray},
        frame=single}

\begin{lstlisting}
(defmacro simple-incf (var &optional (amount 1)) 
    (list setq var (list + var amount)))
\end{lstlisting}

\end{document}

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.