2

I am trying to typeset the source code of a Perl program, containing Russian characters. As far as I learned --- pdflatex is not able to process non-ascii characters in lslisting. So I took xelatex and used the following code:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{listings}
\lstset{
  breaklines=true,
  extendedchars=true,
  inputencoding=utf8
}

\begin{document}
\begin{lstlisting}
my $excel = Spreadsheet::XLSX->new('Шаблон сделки.xlsx');
\end{lstlisting}
\end{document}

It generated a pdf, but with blank spaces instead of Russian characters:

enter image description here

and output an error:

Package babel Warning: No Cyrillic font encoding has been loaded so far.
(babel)                A font encoding should be declared before babel.
(babel)                Default `EU1' encoding will be loaded  on input line
...
LaTeX Font Warning: Font shape `EU1/cmr/m/n' undefined
(Font)              using `EU1/lmr/m/n' instead on input line 12.
3
  • Unfortunately, listings neither works with XeLaTeX.
    – egreg
    Nov 14, 2016 at 22:25
  • @egreg Is it possible to typeset the code with Russian characters somehow?
    – user4035
    Nov 14, 2016 at 22:26
  • You may use minted
    – egreg
    Nov 14, 2016 at 22:31

1 Answer 1

4

For xelatex you can setup the missing chars. ^^^^0428 is for U+0428 etc. But you must use a font which has cyrillic glyphs.

\documentclass{article}
\usepackage{fontspec}
\setmonofont{Arial Unicode MS} % a font with cyrillic
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\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%
  ^^^^0428^^^^0429^^^^042a^^^^042b^^^^042c^^^^042d^^^^042e^^^^042f% new xetex
  ^^^^0430^^^^0431^^^^0432^^^^0433^^^^0434^^^^0435^^^^0436^^^^0437^^^^0438^^^^0439%
  ^^^^043a^^^^043b^^^^043c^^^^043d^^^^043e^^^^043e% new xetex
  ^^^^0440^^^^0441^^^^0442^^^^0444^^^^0444^^^^0445^^^^0446^^^^0447^^^^0448^^^^0449%
  ^^^^044a^^^^044b^^^^044c^^^^044d^^^^044e^^^^044e% new xetex
  %perhaps more
  ^^00}
\lst@RestoreCatcodes
\makeatother
\begin{document}
\begin{lstlisting}
my $excel = Spreadsheet::XLSX->new('Шаблон сделки.xlsx');
\end{lstlisting}

\end{document}

enter image description here

With pdflatex you can use literate to setup all the chars. You can find the needed correlation between unicode position and command name in t2aenc.dfu.

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{listings}
\lstset{
  breaklines=true,
  extendedchars=true,
  literate={Ш}{\CYRSH}1
           {а}{\cyra}1
 }

\begin{document}
\begin{lstlisting}
my $excel = Spreadsheet::XLSX->new('Ша.xlsx');
\end{lstlisting}
\end{document}

enter image description here

2
  • "Шаблон" - о is missing in your output, it gives "Шаблн"
    – user4035
    Nov 15, 2016 at 12:00
  • I had a typo in the code: there were two ^^^^043e but the second should have been ^^^^043f (but I didn't know that this typo has the interesting side effect to break the char ...) Nov 15, 2016 at 12:12

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.