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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use the following listing to show a SPARQL query:

\begin{lstlisting}[captionpos=b, caption=SPARQL query, label=lst: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
}

But now I don't know which language tag I should set, since SPARQL is not built-in. I also tried to define my own highlighting, but I couldn't achieve a good result. Is there any macro available for highlighting SPARQL queries?

share|improve this question
up vote 12 down vote accepted

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

share|improve this answer
    
It should be noted that these are sample-specific formatting instructions, as the prefixes have to be listed explicitly. Using a given string both as a prefix and as an identifier would be problematic, and in the depicted example, java and rdf are also bolded nonsensically in the base IRIs. – O. R. Mapper Oct 16 '15 at 11:33

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

share|improve this answer

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

share|improve this answer
    
Inside lists, you need to indent with 8 spaces (otherwise just 4). – Caramdir Apr 4 '11 at 16:28
    
Thanks @Caramdir, I was getting crazy with this :-) – ℝaphink Apr 4 '11 at 16:31
2  
see meta.tex.stackexchange.com/questions/1192/… for all the ways to format code. – Caramdir Apr 4 '11 at 16:33
    
Thanks. I'm used to indenting with 4 in SE sites, and then indenting button didn't seem to fix anything... – ℝaphink Apr 4 '11 at 16:35
2  
Amazing answer. Can I perhaps “steal” this as a how-to for the documentation, once I get the time to bundle a new release of minted? – Konrad Rudolph Apr 4 '11 at 18:22

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.