22

Please, do you know if there's any possibility how to use the listings package together with XeLaTeX? I have to use XelaTeX and therefore I have UTF-8 encoded files. This is not compatible with listings which allows one-byte encoding. (I know there is listingsutf8 package, but it converts files from UTF-8 which I don't need.)

The listings package works somehow, bu I only can use English letters. I've fond some solution like a list of special characters ( Having problems with listings and UTF-8. Can it be fixed? ) but it seems it works only for some letters. It there anything that would support UTF-8 in listing 'naturally'?

21

The link you are mentioning is for 8-bit engines like pdflatex, it will not work with xetex or luatex.

With xelatex + lualatex chars with unicode positions below 256 will work out of the box:

\documentclass{article}
\usepackage{listings,fontspec}
\begin{document}
\begin{lstlisting}
öäü é è ß 
\end{lstlisting}
\end{document}

Things get more complicated if you want to use chars above 256. E.g. an € or the french "oe" or greek etc. In this case you will have to add them to the processing list of listings first and use extendedchars=true. Here an example which adds the euro and the french oe and Oe (the middle listings shows the problems without the extended definitions):

\documentclass{article}
\usepackage{fontspec}
\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%
  ^^^^20ac^^^^0153^^^^0152% nouveau pour xetex
  ^^00}
\lst@RestoreCatcodes
\makeatother
\begin{document}
\begin{lstlisting}[extendedchars=true]%default je pense
bœuf BŒeuf a€e
Dès Noël où un zéphyr haï me
vêt de glaçons würmiens je
dîne d'exquis rôtis de bœuf
au kir à l'aÿ d'âge mûr \& cætera!
\end{lstlisting}

\begin{lstlisting}[extendedchars=false]
bœuf BŒeuf a€e
Dès Noël où un zéphyr haï me
vêt de glaçons würmiens je
dîne d'exquis rôtis de bœuf
au kir à l'aÿ d'âge mûr \& cætera!
\end{lstlisting}


\begin{lstlisting}[language=XML]
bœuf BŒeuf a€e
Dès Noël où un zéphyr haï me
vêt de glaçons würmiens je
dîne d'exquis rôtis de bœuf
au kir à l'aÿ d'âge mûr \& cætera!
\end{lstlisting}
\end{document}

Output:

enter image description here

  • How do you know what to add to \lst@InputCatcodes? I was trying this William Shakespeare (/ˈʃeɪkspɪər/) – PatrickT May 2 '16 at 15:10
  • 1
    @PatrickT: You will have to look up the unicode positions. – Ulrike Fischer May 2 '16 at 15:13
  • Thanks Ulrike, where do you recommend I look for the example given? – PatrickT May 2 '16 at 15:14
  • Got it thanks! The code for ʃ is 0283, do I just append to the existing list? like ``^^^^0152^^^^0283%`, that's all there is to it right? Tedious, but great! Thanks. – PatrickT May 2 '16 at 15:24
6

Thanks for posting this block of code. The same technique can be used to typeset UTF8 APL characters with APL385 Unicode font. Include the following:

% set lstlisting to accept UTF8 APL text
\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%
  ^^^^20a7^^^^2190^^^^2191^^^^2192^^^^2193^^^^2206^^^^2207^^^^220a%
  ^^^^2218^^^^2228^^^^2229^^^^222a^^^^2235^^^^223c^^^^2260^^^^2261%
  ^^^^2262^^^^2264^^^^2265^^^^2282^^^^2283^^^^2296^^^^22a2^^^^22a3%
  ^^^^22a4^^^^22a5^^^^22c4^^^^2308^^^^230a^^^^2336^^^^2337^^^^2339%
  ^^^^233b^^^^233d^^^^233f^^^^2340^^^^2342^^^^2347^^^^2348^^^^2349%
  ^^^^234b^^^^234e^^^^2350^^^^2352^^^^2355^^^^2357^^^^2359^^^^235d%
  ^^^^235e^^^^235f^^^^2361^^^^2362^^^^2363^^^^2364^^^^2365^^^^2368%
  ^^^^236a^^^^236b^^^^236c^^^^2371^^^^2372^^^^2373^^^^2374^^^^2375%
  ^^^^2377^^^^2378^^^^237a^^^^2395^^^^25af^^^^25ca^^^^25cb%
  ^^00}
\lst@RestoreCatcodes
\makeatother

More details at: http://bakerjd99.wordpress.com/2011/08/15/typesetting-utf8-apl-code-with-the-latex-lstlisting-package/

0

Possible work-around from this answer on SO, is to escape some parts of your code to latex. For example if you use UTF-8 character only in code comments use texcl=true option.

\lstset{texcl=true}

\begin{lstlisting}
    int iLink = 0x01; // use here any UTF-8 symbols
\end{lstlisting}

For more escape options see Listings package docs on page 40.

You can even escape specific sequence surrounded with delimiter (like here).

\lstset{    
    escapeinside={\%*}{*)}
}

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.