[Tex/LaTex] Listing: Syntax highlighting for SPARQL query

highlightinglistings

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?

Best Answer

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