1

I'm using all ASCII characters in my code blocks, so I need to reach for non-ASCII ones for the escape characters. I expected Xetex to deal with this just fine; alas, it doesn't seem to be the case.

\documentclass{memoir}
\usepackage{listings}

\lstset{escapeinside=〈〉}

\begin{document}

\begin{lstlisting} foo bar baz〈\textit{hello}〉 \end{lstlisting}

\end{document}

When typeset by running latexmk -xelatex foo.tex, this results in a code listing that contains "\textit{hello}" instead of "hello".

How do I use non-ASCII escape characters within lstlisting?

Cactus
  • 1,107

1 Answers1

5

You must enable the parsing:

\documentclass{memoir}
\usepackage{listings}

\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% ^^^^3008^^^^3009% angle chars for xelatex ^^00} \lst@RestoreCatcodes \makeatother

\lstset{escapeinside=〈〉}

\begin{document}

\begin{lstlisting} foo bar baz〈\textit{hello}〉 \end{lstlisting}

\end{document}

See also The 'listings' package and UTF-8

enter image description here

Ulrike Fischer
  • 327,261
  • I'm not sure what you mean by "eating a space" -- if I put a space between baz and 〈\textit{hello}〉, then there is a space between "baz" and "hello". – Cactus Jun 02 '21 at 12:43
  • not in my screenshot, but I'm a bit in a hurry so I can't check now if it is really missing or is I removed by accident. – Ulrike Fischer Jun 02 '21 at 12:47
  • your examples doesn't have a space in the source code either. And neither does my original code. – Cactus Jun 02 '21 at 14:18
  • ah, it is the font, the left angle has a rather wide side bearing which looks like a space ;-) – Ulrike Fischer Jun 02 '21 at 14:21