12

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?

13

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

  • 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
12

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

  • Inside lists, you need to indent with 8 spaces (otherwise just 4). – Caramdir Apr 4 '11 at 16:28
  • 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
  • Certainly @Konrad. I've struggled to add my own lexer to Pygments to use with minted (still don't get it fully I'm afraid), so I'm happy to share the little I've figured out ;-) – ℝaphink Apr 4 '11 at 19:28
3

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

1

I'm new to StackExchange, and don't know well the forum ; but I think this page should be updated.

I first followed the SQL solution, then the \lstdefinelanguage : both of these solutions fail with comments.

However, I found that there is a SPARQL definition in /usr/share/texlive/texmf-dist/tex/latex/listings/lstlang3.sty (install on Ubuntu 18.04) :

.../usr/share/texlive/texmf-dist/tex/latex/listings$ grep -i sparql *
lstlang3.sty:%% SPARQL definition (c) 2006 Christoph Kiefer
lstlang3.sty:\lst@definelanguage{SPARQL}%

As you can see, it was written in 2006 (but maybe not in listings package in 2011 or 2013, when the answers on this page were posted?).

Here's a sample code to compare the solutions :

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{color}

\lstset{
    morecomment=[l]{\#},
    frame = single,
    basicstyle = \ttfamily\footnotesize,
    commentstyle=\color{blue},
}

\lstdefinelanguage{sparqlB}{
    morecomment=[l]{\#},
    language=SQL,
    morekeywords={SELECT},      
    commentstyle=\itshape\color{blue},
    sensitive=true,
}

\begin{document}

    -- SPARQL in lstlang3.sty : 
    \begin{lstlisting}[language=sparql]
        # comment with 'single quotes' = OK
        SELECT * WHERE { ?s ?p ?o }
    \end{lstlisting}

    -- SQL solution :       
    \begin{lstlisting}[language=SQL]
        # comment with 'single quotes' = NOT RECOGNIZED
        SELECT * WHERE { ?s ?p ?o }
    \end{lstlisting}    

    -- \\lstdefinelanguage solution
    \begin{lstlisting}[language=sparqlB]
    # comment with 'single quotes' = NOT RECOGNIZED
    SELECT * WHERE { ?s ?p ?o }
    \end{lstlisting}    


\end{document}

EDIT : Actually the \lstdefinelanguage works for comments if I change the order of options

\lstdefinelanguage{
language=SQL,  % <-- has to be before morecomment
morecomment=[l]{\#},
[...]

or remove language=SQL

But the definition in .sty file is more complete, of course.

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.