0

I try to write some cyrillic text in a source code block:

\begin{lstlisting}[caption={asdfa},label={lst:tokens},language=SQL]
SELECT 
(SELECT COUNT(*) 
FROM cards
WHERE description ILIKE '%#НОМЕР#%') AS PhoneNumber,
(SELECT COUNT(*) 
FROM cards
WHERE description ILIKE '%#ГОС.НОМЕР#%') AS LicensePlateNumber
\end{lstlisting}

style is set like below:

\lstset{
    language=SQL,frame=ltrb,framesep=5pt,basicstyle=\normalsize,
    keywordstyle=\ttfamily\color{OliveGreen},
    identifierstyle=\ttfamily\color{CadetBlue}\bfseries, 
    commentstyle=\color{Brown},
    stringstyle=\ttfamily,
    showstringspaces=true,
    escapeinside={<--}{-->},
    extendedchars=\true,
    literate={а}{\cyra}1
    {б}{{\selectfont\char225}}1
    {в}{{\selectfont\char226}}1
    {г}{{\selectfont\char227}}1
    {д}{{\selectfont\char228}}1
    {е}{{\selectfont\char229}}1
    {ё}{{\"e}}1
    {ж}{{\selectfont\char230}}1
    {з}{{\selectfont\char231}}1
    {и}{{\selectfont\char232}}1
    ...
    }

None of the cyrillic characters is displayed. I tried to enclose cyrillic in rare used characters (Listing with mixed english and russian symbols in comments), but it didn't work either. Usage of extendedchars=true instead of \true (Why does utf8 not allow Cyrillic letters in listings when utf8x does?) doesn't help. I've found suggestion to use mintex (the first link), but I will have to create a list of listings.

I also have following imports at the beginning:

\usepackage[utf8]{inputenc}
\usepackage[russian, english]{babel}

Use of polyglossia with Russian as an additional language didn't help as well.

How can I make this thing work with cyrillic symbols?

Edit: Here are the imports:

\documentclass[a4paper,12pt]{report}
\usepackage{graphicx}
\graphicspath{pics/}
\usepackage[a4paper,top=25mm,bottom=25mm]{geometry}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyfoot{}
\fancyfoot[R]{\thepage}
\fancypagestyle{plain}{}
\usepackage[nottoc]{tocbibind}
\usepackage[utf8]{inputenc}
\usepackage[russian, english]{babel}
\usepackage{url}
\usepackage{xurl}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{indentfirst}
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{enumitem}
\usepackage[acronym,automake]{glossaries}
\usepackage{xcolor,listings}
\usepackage{fontspec}
\usepackage[T2A]{fontenc}
\setmonofont{Courier New}
\input{common/lststyles}

None of the packages are applied after this.

Edit 2: This sample code works, although in a weird manner:

\lstdefinestyle{code-style-sql}{
    belowcaptionskip=1\baselineskip,
    xleftmargin=\parindent,
    identifierstyle=\color{blue},
    language=SQL,                % the language of the code
    backgroundcolor=\color{backgray},       % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}; should come as last argument  
    commentstyle=\color{codegray},        % comment style
    keywordstyle=\color{codeblue}\bfseries,    % keyword style
    numberstyle=\tiny\color{codegray},      % the style that is used for the line-numbers
    stringstyle=\color{codeblue},        % string literal style
    basicstyle=\small\ttfamily\footnotesize,  % Размер и тип шрифта, the size of the fonts that are used for the code
    breakatwhitespace=false,               % sets if automatic breaks should only happen at whitespace
    breaklines=true,                  % Перенос строк, the size of the fonts that are used for the code       
    captionpos=lt,                          % sets the caption-position to bottom               
    keepspaces=true,                % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)             
    numbers=left,                          % where to put the line-numbers; possible values are (none, left, right)
    numbersep=5pt,                     % how far the line-numbers are from the code       
    stepnumber=1,                          % the step between two line-numbers. If it's 1, each line will be numbered
    showspaces=false,           % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'   
    showstringspaces=false,    % underline spaces within strings only    
    showtabs=false,             % show tabs within strings adding particular underscores     
    tabsize=2,          % Размер табуляции, sets default tabsize to 2 spaces
    %  frame=single,               % Рамка, adds a frame around the code
    %  frame=TB,
    literate={--}{{-{}-}}2,     % Корректно отображать двойной дефис
    literate={---}{{-{}-{}-}}3,  % Корректно отображать тройной дефис
    escapeinside={<--}{-->}
}
\lstset{style=code-style-sql}

It moves cyrillic text to the left apostrophe and leaves the signs at the right side: 'ГОСНОМЕР%#.#%' instead of '%#ГОС.НОМЕР#%'.

  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – DG' Jul 15 '20 at 07:29
  • Would you please merge those code snippets to a complete example (also called MWE as mentioned by @DG' )? – muzimuzhi Z Jul 15 '20 at 19:03
  • The commands such as {б}{{\selectfont\char225}} are incorrect. There might originally have been a \fontencoding{T2A} before \selectfont in a document for PDFTeX. However, a document in XeTeX should just use Unicode. – Davislor Dec 12 '20 at 18:08

2 Answers2

1

You can use XeLaTeX, but you have to manually add support for the basic (Russian) Cyrillic alphabet.

\documentclass{article}
\usepackage[a4paper,top=25mm,bottom=25mm]{geometry}
\usepackage[russian,english]{babel}
\usepackage{fontspec}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}

% a main font that supports Cyrillic; use your preferred one \setmainfont{NewCM10}[ Extension=.otf, UprightFont=-Regular, ItalicFont=-Italic, BoldFont=-Bold, BoldItalicFont=-BoldItalic, ] \setmonofont{Courier New}

\lstset{ language=SQL, frame=ltrb, framesep=5pt, basicstyle=\normalsize, keywordstyle=\ttfamily\color{OliveGreen}, identifierstyle=\ttfamily\color{CadetBlue}\bfseries, commentstyle=\color{Brown}, stringstyle=\ttfamily, showstringspaces=true, escapeinside={<--}{-->}, }

\makeatletter % see https://tex.stackexchange.com/a/320345 \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% % Basic Cyrillic alphabet coverage ^^^^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

\begin{document}

\begin{lstlisting}[caption={asdfa},label={lst:tokens},language=SQL] SELECT (SELECT COUNT() FROM cards WHERE description ILIKE '%#НОМЕР#%') AS PhoneNumber, (SELECT COUNT() FROM cards WHERE description ILIKE '%#ГОС.НОМЕР#%') AS LicensePlateNumber \end{lstlisting}

\end{document}

Note that you shouldn't load inputenc and fontenc.

enter image description here

egreg
  • 1,121,712
0

Switching to pdfLaTeX from XeTeX solves the problem.

  • 3
    Switching compiler is usually not a solution as there are other situations where one is asked to switch from pdftex to xetex. For non-Latin languages in particular, pdftex is just unpleasant. – Symbol 1 Jul 15 '20 at 18:50