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.

I'm wondering, does listings allow you to define your own syntax highlighting, and if so, how sophisticated is its system?

Here's a minimal example:

\documentclass{article}
\usepackage{listings}

\lstset{emph={%  
   {:-}%
     },emphstyle={\color{red}\bfseries\underbar}%
}%

\begin{document}
\begin{lstlisting}[language=Prolog]
    foo(Arg1, Arg2) :-
      bar(Arg3),
      baz(Arg4),
      length([1,2,3], 3).
\end{lstlisting}
\end{document}

Ideally, in the following code sample, "foo" would be highlighted (since it's declaring a new clause), as would ":-".

I tried to use emph and morekeywords to highlight :-, but neither worked. They seems to work for regular words like "for" and "while", but not for the sequence ":-". Is there a special way to escape this to add a keyword?

To highlight the name of the predicate before :-, I have no idea how I would tackle that. It's not essential, but it would be nice if I could get it working. Can I define custom syntax/grammar for highlighting Prolog?

share|improve this question
    
    
emph and keywords won't highlight characters such as : and - because listings treats them as "other" characters; you'll have to declare them as "letters" by using alsoletter=:-,. As for highlighting stuff before :-, see my comment to the question at the end of the second link above. –  Jubobs Mar 14 at 14:18
add comment

1 Answer

up vote 1 down vote accepted

Not quite a direct answer to your question, but I recommend the rather wonderful package minted. It uses pygments to highlight a wide variety of languages. Keep in mind that you'll need to pass -shell-escape to latex so that it can call out to pygments. Documentation here (pdf).

Frankly, I switched to minted because setting up anything complex in listings quickly got out of hand for my tastes.

share|improve this answer
    
This is perfect. Looks beautiful. Thanks! –  jmite Jun 20 '13 at 21:50
add comment

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.