2

I want to highlight some parts of my Turtle document (with a specific color) in the lstlisting element as you can see in my MWE:

\documentclass{article}

\usepackage{listings}
\usepackage[dvipsnames]{xcolor} %with special prdefined colors 

\lstset{ basicstyle=\ttfamily,breaklines=true}
\lstset{escapeinside={<@}{@>}}


\begin{document}

\begin{lstlisting}[language=SPARQL,keywordstyle=\color{blue},basicstyle=\normalsize\ttfamily]
b:Shakespeare foaf:givenName <@\textcolor{NavyBlue}{"William"^^xsd:string}@>.
\end{lstlisting}

\end{document}

However, as the result instead of "Shakespeare"^^xsd:string, I have "William"8sd:string. The coloring is actually all right, but I couldn't prevent showing 8 instead of ^^.

3

Between <@ and @>, TeX uses its standard rules and these rules say that a combination like ^^x refers to the character whose ASCII code differs from the ASCII code of x by 64 (plus or minus, depending on which operation gives a value in the interval 0–127). Thus ^^x gives 8.

Actually the rule is a bit more complicated: if the two characters following ^^ are among 0123456789abcdef, they are interpreted as a hexadecimal 16 bit number. However, x doesn't make this particular rule to apply.

Solution: use \string:

\documentclass{article}

\usepackage{listings}
\usepackage[dvipsnames]{xcolor} %with special prdefined colors 

\lstset{
  basicstyle=\ttfamily,
  breaklines=true,
  escapeinside={<@}{@>},
  columns=fullflexible,
}


\begin{document}

\begin{lstlisting}[language=SPARQL,keywordstyle=\color{blue},basicstyle=\normalsize\ttfamily]
b:Shakespeare foaf:givenName <@\textcolor{NavyBlue}{"William"\string^\string^xsd:string}@>.
\end{lstlisting}

\end{document}

I believe it's better to use columns=fullflexible to get uniform spacing.

enter image description here

  • Thank you egreg for your detailed answer. This solved my problem. – pero Aug 3 '16 at 8:29

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.