'listings' 包和 UTF-8

'listings' 包和 UTF-8

请问,您是否知道是否有可能将该包与 XeLaTeX 一起使用listings?我必须使用 XeLaTeX,因此我有 UTF-8 编码的文件。这与允许单字节编码的列表不兼容。(我知道有listingsutf8包,但它会转换文件UTF-8,我不需要。)

listings 包可以正常工作,但我只能使用英文字母。我找到了一些解决方案,比如特殊字符列表(列表和 UTF-8 存在问题。可以修复吗?) 但似乎只适用于某些字母。有什么东西可以“自然”地支持 UTF-8 列出吗?

答案1

您提到的链接适用于像 pdflatex 这样的 8 位引擎,它不适用于 xetex 或 luatex。

使用 xelatex + lualatex 时,unicode 位置低于 256 的字符即可开箱即用:

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

如果您想使用超过 256 个字符,事情会变得更加复杂。例如 € 或法语“oe”或希腊语等。在这种情况下,您必须先将它们添加到列表处理列表中,然后使用extendedchars=true。这里有一个添加欧元和法语 oe 和 Oe 的示例(中间列表显示了没有扩展定义的问题):

\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}

输出:

在此处输入图片描述

答案2

感谢您发布此代码块。可以使用相同的技术用 APL385 Unicode 字体排版 UTF8 APL 字符。包括以下内容:

% 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

更多详情请见: http://bakerjd99.wordpress.com/2011/08/15/typesetting-utf8-apl-code-with-the-latex-lstlisting-package/

答案3

只是对之前答案的补充。这是 UTF8 APL、希腊文和西里尔文符号的表格

\usepackage{listingsutf8}

\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%
    %
    ^^^^0374^^^^0375^^^^037a^^^^037e^^^^0384^^^^0385^^^^0386^^^^0387^^^^0388^^^^0389^^^^038a%
    ^^^^038c^^^^038e^^^^038f^^^^0390^^^^0391^^^^0392^^^^0393^^^^0394^^^^0395^^^^0396^^^^0397%
    ^^^^0398^^^^0399^^^^039a^^^^039b^^^^039c^^^^039d^^^^039e^^^^039f^^^^03a0^^^^03a1^^^^03a3%
    ^^^^03a4^^^^03a5^^^^03a6^^^^03a7^^^^03a8^^^^03a9^^^^03aa^^^^03ab^^^^03ac^^^^03ad^^^^03ae%
    ^^^^03af^^^^03b0^^^^03b1^^^^03b2^^^^03b3^^^^03b4^^^^03b5^^^^03b6^^^^03b7^^^^03b8^^^^03b9%
    ^^^^03ba^^^^03bb^^^^03bc^^^^03bd^^^^03be^^^^03bf^^^^03c0^^^^03c1^^^^03c2^^^^03c3^^^^03c4%
    ^^^^03c5^^^^03c6^^^^03c7^^^^03c8^^^^03c9^^^^03ca^^^^03cb^^^^03cc^^^^03cd^^^^03ce^^^^03d0%
    ^^^^03d1^^^^03d2^^^^03d3^^^^03d4^^^^03d5^^^^03d6^^^^03d7^^^^03d8^^^^03d9^^^^03da^^^^03db%
    ^^^^03dc^^^^03dd^^^^03de^^^^03df^^^^03e0^^^^03e1^^^^03e2^^^^03e3^^^^03e4^^^^03e5^^^^03e6%
    ^^^^03e7^^^^03e8^^^^03e9^^^^03ea^^^^03eb^^^^03ec^^^^03ed^^^^03ee^^^^03ef^^^^03f0^^^^03f1%
    ^^^^03f2^^^^03f3^^^^03f4^^^^03f5^^^^03f6^^^^03f7^^^^03f8^^^^03f9^^^^03fa^^^^03fb^^^^1f00%
    ^^^^1f01^^^^1f02^^^^1f03^^^^1f04^^^^1f05^^^^1f06^^^^1f07^^^^1f08^^^^1f09^^^^1f0a^^^^1f0b%
    ^^^^1f0c^^^^1f0d^^^^1f0e^^^^1f0f^^^^1f10^^^^1f11^^^^1f12^^^^1f13^^^^1f14^^^^1f15^^^^1f18%
    ^^^^1f19^^^^1f1a^^^^1f1b^^^^1f1c^^^^1f1d^^^^1f20^^^^1f21^^^^1f22^^^^1f23^^^^1f24^^^^1f25%
    ^^^^1f26^^^^1f27^^^^1f28^^^^1f29^^^^1f2a^^^^1f2b^^^^1f2c^^^^1f2d^^^^1f2e^^^^1f2f^^^^1f30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^^^1fbf%
    ^^^^1fc0^^^^1fc1^^^^1fc2^^^^1fc3^^^^1fc4^^^^1fc6^^^^1fc7^^^^1fc8^^^^1fc9^^^^1fca^^^^1fcb%
    ^^^^1fcc^^^^1fcd^^^^1fce^^^^1fcf^^^^1fd0^^^^1fd1^^^^1fd2^^^^1fd3^^^^1fd6^^^^1fd7^^^^1fd8%
    ^^^^1fd9^^^^1fda^^^^1fdb^^^^1fdd^^^^1fde^^^^1fdf^^^^1fe0^^^^1fe1^^^^1fe2^^^^1fe3^^^^1fe4%
    ^^^^1fe5^^^^1fe6^^^^1fe7^^^^1fe8^^^^1fe9^^^^1fea^^^^1feb^^^^1fec^^^^1fed^^^^1fee^^^^1fef%
    ^^^^1ff2^^^^1ff3^^^^1ff4^^^^1ff6^^^^1ff7^^^^1ff8^^^^1ff9^^^^1ffa^^^^1ffb^^^^1ffc^^^^1ffd%
    ^^^^1ffe^^^^0410^^^^0411^^^^0412^^^^0413^^^^0414^^^^0415^^^^0416^^^^0417^^^^0418^^^^0419%
    ^^^^041a^^^^041b^^^^041c^^^^041d^^^^041e^^^^041f^^^^0420^^^^0421^^^^0422^^^^0423^^^^0424%
    ^^^^0425^^^^0426^^^^0427^^^^0428^^^^0429^^^^042a^^^^042b^^^^042c^^^^042d^^^^042e^^^^042f%
    ^^^^0430^^^^0431^^^^0432^^^^0433^^^^0434^^^^0435^^^^0436^^^^0437^^^^0438^^^^0439^^^^043a%
    ^^^^043b^^^^043c^^^^043d^^^^043e^^^^043f^^^^0440^^^^0441^^^^0442^^^^0443^^^^0444^^^^0445%
    ^^^^0446^^^^0447^^^^0448^^^^0449^^^^044a^^^^044b^^^^044c^^^^044d^^^^044e^^^^044f^^^^0401%
    ^^^^0451% 
    ^^00}
\lst@RestoreCatcodes
\makeatother

答案4

看来listingsXeLaTeX 的最新版本已经支持 UTF-8 了。

然而我发现非 ASCII 字符无法正确显示(它们被丢弃)的情况。

事实证明,我认为的 UTF-8 编码文件是 ISO-8859 编码文件(使用file命令检查),所以我必须转换它们:

iconv -f ISO-8859 -t UTF-8 inputfile outputfile

转换后,重音符号和其他非 ASCII 字符可以正确显示。

相关内容