Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)?

share|improve this question
    
Welcome to TeX.SX! You may have a look on our starter guide –  Mario S. E. May 25 '13 at 13:37

1 Answer 1

up vote 6 down vote accepted

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

share|improve this answer
    
Ah, okay. I was unaware of this letter/other distinction. –  oarfish May 25 '13 at 13:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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