Listing: Syntax highlighting for SPARQL query?

Load the defined language SQL and define more keywords, if needed. Alternetivily write a package SPARQL.sty with a a complete language definition. Choose the one from SQL as template which is in the file lstlang1.sty

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}
\usepackage{listings}
\lstset{language=SQL,morekeywords={PREFIX,java,rdf,rdfs,url}}

\begin{document}

\begin{lstlisting}[captionpos=b, caption=SPARQL query, label=lst:sparql,
   basicstyle=\ttfamily,frame=single]
PREFIX java: <http://evolizer.org/ontologies/seon/2009/06/java.owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?url ?name
WHERE {
   ?url rdf:type java:Package .
   ?url rdfs:label ?name
}
\end{lstlisting}
\end{document}

enter image description here


If you use the minted package, you can write your own lexer quite easily by writing a python module for Pygments. I wrote a little example on my blog.

Edit: There's actually a SPARQL lexer for Pygments in this repository, so you can

  • Download the sw.py file

    $ mkdir -p swlexers/swlexers
    $ cd swlexers
    $ wget http://www.openvest.com/trac/browser/tools/trunk/pygments/lexers/sw.py?format=txt -O swlexers/__init__.py
    
  • Write a setup.py file for it

    $ cat setup.py
    """
    Sparql syntax highlightin for Pygments.
    """
    from setuptools import setup
    entry_points = """
    [pygments.lexers]
    sparql = swlexers:SparqlLexer
    """
    setup(
        name         = 'swlexers',
        version      = '0.1',
        description  = __doc__,
        packages     = ['swlexers'],
        entry_points = entry_points
    )
    
  • Install the module:

    $ sudo python setup.py install
    
  • Check that the module was installed:

    $ pygmentize -L | grep -i sparql
    
  • Use it in your code with minted:

    \documentclass{article}
    \usepackage{minted}
    \begin{document}
    \begin{minted}{sparql}
    PREFIX java: <http://evolizer.org/ontologies/seon/2009/06/java.owl#>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT ?url ?name
    WHERE {
       ?url rdf:type java:Package .
       ?url rdfs:label ?name
    }
    \end{minted}
    \end{document}
    

Sparql in minted


The other answers provide much fully features ways to achieve this. For completeness sake I would note that you can also just use the \lstdefinelanguage command from the listings package to define a completely new language.

The following is the crude definition of SPARQL I've used in the past.

% Language Definitions for SPARQL
\lstdefinelanguage{sparql}{
morecomment=[l][\color{olivegreen}]{\#},
morestring=[b][\color{blue}]\",
morekeywords={SELECT,CONSTRUCT,DESCRIBE,ASK,WHERE,FROM,NAMED,PREFIX,BASE,OPTIONAL,FILTER,GRAPH,LIMIT,OFFSET,SERVICE,UNION,EXISTS,NOT,BINDINGS,MINUS,a},
sensitive=true
}

You'll notice I didn't bother to fill out all the keywords for the built in functions