[Tex/LaTex] Syntax highlighting for SPARQL query in a listing? (works fine in minted environment but not in listing with mint)

colorhighlightinglistingsmintedsyntax

I can do syntax highlighting in the minted environment, for any SPARQL query using this Pygments lexer thanks to this answer.

My problem is how to get SPARQL syntax highlighting to work with mint in a listing (so that I can get a label and a caption). I am missing the BOM/EOM escape code.

Here's a minimal example:

\documentclass{article}
\usepackage{minted}
\begin{document}

%Minted environment works fine 
\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}

%Mint in listing gives runaway argument error
\begin{listing}[H]
\mint{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
\caption{Example of a listing.}
\label{lst:example}
\end{listing}

Listing \ref{lst:example} contains an example of a listing.

\end{document}

When I try to compile the above example with pdflatex -shell-escape I get:

Runaway argument?
REFIX java: <http://evolizer.org/ontologies/seon/2009/06/java.owl#>
! Forbidden control sequence found while scanning use of \FancyVerbGetVerb.
<inserted text> 
                \par 
l.17 ...zer.org/ontologies/seon/2009/06/java.owl#>

This example from the mint manual works fine:

\begin{listing}[H]
\mint{cl}/(car (cons 1 ’(2)))/
\caption{Example of a listing.}
\label{lst:example}
\end{listing}

Listing \ref{lst:example} contains an example of a listing. 

I seem to be missing the escape code (/ in cl) that signals the beginning and end of a message for SPARQL. Thoughts?

I'd also consider other approaches to syntax highlighting (e.g. other libraries that use Pygments); since I want colors, this b+w highlighting solution isn't what I need.

Best Answer

You can use the minted environment by putting it inside the listing environment, with the label and caption after the end of the minted environment (on a separate line). The nested environments will work fine.

The \mint command is for typesetting a single line of code, so the code must delimited by a pair of matched characters. For example, \mint{<language>}/<code>/ uses the / character as a delimiter.