0

I am using unicode characters in some code examples.

If I set the listing completely, everything works as expected when the font supoorts the characters. But once I want to omit lines with lsitings's linerange option, things get messy. When I run this minimal example with lualatex:

\documentclass{standalone}

% allow unicode characters in listings
\RequirePackage{fontspec} 
\setmonofont[Scale=MatchLowercase]{DejaVu Sans Mono} 

\usepackage{listings}

\lstset{language=[Objective]Caml,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  escapeinside={(*@}{@*)},
  basicstyle=\ttfamily,
  columns=fullflexible,keepspaces=true
  inputencoding=utf8
}

\begin{document}
  \begin{lstlisting}[linerange=2-2]
(* This line should be ignored λx.x *)
let f x = x
  \end{lstlisting}
\end{document}

The lambda unicode character is not skipped but prepended, while the rest of the line is skipped:

problem

Is this a bug in listings or do I miss some arcane option?

choeger
  • 915

1 Answers1

3

If you want to use xelatex + luatex and unicode chars above position 256 you must add them to the process letter list. See The 'listings' package and UTF-8. In the case of the lambda you should add ^^^^03bb:

\documentclass{standalone}
%\usepackage{luatex85} %needed with newer luatex
% allow unicode characters in listings
\RequirePackage{fontspec}
\setmonofont[Scale=MatchLowercase]{DejaVu Sans Mono}

\usepackage{listings}

\lstset{language=[Objective]Caml,
  showspaces=false,
  showtabs=false,
  breaklines=true,
  showstringspaces=false,
  breakatwhitespace=true,
  escapeinside={(*@}{@*)},
  basicstyle=\ttfamily,
  columns=fullflexible,keepspaces=true
  %inputencoding=utf8
}

\makeatletter
\lst@InputCatcodes
\def\lst@DefEC{%
 \lst@CCECUse \lst@ProcessLetter
  ^^80^^81^^82^^83^^84^^85^^86^^87^^88^^89^^8a^^8b^^8c^^8d^^8e^^8f%
  ^^90^^91^^92^^93^^94^^95^^96^^97^^98^^99^^9a^^9b^^9c^^9d^^9e^^9f%
  ^^a0^^a1^^a2^^a3^^a4^^a5^^a6^^a7^^a8^^a9^^aa^^ab^^ac^^ad^^ae^^af%
  ^^b0^^b1^^b2^^b3^^b4^^b5^^b6^^b7^^b8^^b9^^ba^^bb^^bc^^bd^^be^^bf%
  ^^c0^^c1^^c2^^c3^^c4^^c5^^c6^^c7^^c8^^c9^^ca^^cb^^cc^^cd^^ce^^cf%
  ^^d0^^d1^^d2^^d3^^d4^^d5^^d6^^d7^^d8^^d9^^da^^db^^dc^^dd^^de^^df%
  ^^e0^^e1^^e2^^e3^^e4^^e5^^e6^^e7^^e8^^e9^^ea^^eb^^ec^^ed^^ee^^ef%
  ^^f0^^f1^^f2^^f3^^f4^^f5^^f6^^f7^^f8^^f9^^fa^^fb^^fc^^fd^^fe^^ff%
  ^^^^20ac^^^^0153^^^^0152^^^^03bb%<----
  ^^00}
\lst@RestoreCatcodes
\makeatother
\begin{document}
  \begin{lstlisting}[linerange=2-2]
(* This line should be ignored λx.x *)
let f x = x
  \end{lstlisting}
\end{document}

enter image description here

Ulrike Fischer
  • 327,261