4

When I use listings, to display code with Russian comments, in a literate block, I obtain invalid line-breaking, on whitespace characters.

Please refer to the below MWE, for an example, which compiles to the document provided at the end of this question.

How might I be able to resolve this issue?


\documentclass[varwidth=true, border={6pt -5pt 6pt 2pt}]{standalone}
%\documentclass{extarticle}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{cmap}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage[T2A]{fontenc}
\usepackage[babel=true,tracking=true]{microtype}
%------------------------------------------------
\usepackage{PTSans}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{filecontents}
\begin{filecontents*}{dbstart.txt}
-- Cоздаем таблицу Группа
create table sgroup (
id integer not null,
group_name varchar(40),
primary key (id));  -- уникальный ключ id
-- Cоздаем таблицу Студент
create table student (
id integer not null,
id_group integer not null, -- поле для связи с таблицей Группа
student_name varchar(40),
last_name varchar(40),
middle_name varchar(40),
sex char(1),
age smallint,
primary key (id), -- уникальный ключ id
foreign key (id_group) references sgroup (id)); -- определение связи с таблицей Группа
-- Cоздаем таблицу Хобби
create table hobby (
id integer not null, -- уникальный ключ
hobby_name varchar(100),
primary key (id)); -- уникальный ключ id
-- Cоздаем таблицу Студент-Хобби
create table student_hobby (
id_student integer not null, -- поле для связи с таблицей Студент
id_hobby integer not null, -- поле для связи с таблицей Хобби
primary key (id_student, id_hobby), -- уникальный ключ составной id_student, id_hobby
foreign key (id_hobby) references hobby (id), -- определение связи с таблицей Хобби
foreign key (id_student) references student (id)); -- определение связи с таблицей Студент
\end{filecontents*}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}
\usepackage{listings}
\lstset{
    inputencoding=utf8,
    extendedchars=false,
    keepspaces=true,
    breaklines=true,
    breakatwhitespace=true,
    breakindent=0pt,
    literate={а}{{\selectfont\char224}}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
    {й}{{\selectfont\char233}}1
    {к}{{\selectfont\char234}}1
    {л}{{\selectfont\char235}}1
    {м}{{\selectfont\char236}}1
    {н}{{\selectfont\char237}}1
    {о}{{\selectfont\char238}}1
    {п}{{\selectfont\char239}}1
    {р}{{\selectfont\char240}}1
    {с}{{\selectfont\char241}}1
    {т}{{\selectfont\char242}}1
    {у}{{\selectfont\char243}}1
    {ф}{{\selectfont\char244}}1
    {х}{{\selectfont\char245}}1
    {ц}{{\selectfont\char246}}1
    {ч}{{\selectfont\char247}}1
    {ш}{{\selectfont\char248}}1
    {щ}{{\selectfont\char249}}1
    {ъ}{{\selectfont\char250}}1
    {ы}{{\selectfont\char251}}1
    {ь}{{\selectfont\char252}}1
    {э}{{\selectfont\char253}}1
    {ю}{{\selectfont\char254}}1
    {я}{{\selectfont\char255}}1
    {А}{{\selectfont\char192}}1
    {Б}{{\selectfont\char193}}1
    {В}{{\selectfont\char194}}1
    {Г}{{\selectfont\char195}}1
    {Д}{{\selectfont\char196}}1
    {Е}{{\selectfont\char197}}1
    {Ё}{{\"E}}1
    {Ж}{{\selectfont\char198}}1
    {З}{{\selectfont\char199}}1
    {И}{{\selectfont\char200}}1
    {Й}{{\selectfont\char201}}1
    {К}{{\selectfont\char202}}1
    {Л}{{\selectfont\char203}}1
    {М}{{\selectfont\char204}}1
    {Н}{{\selectfont\char205}}1
    {О}{{\selectfont\char206}}1
    {П}{{\selectfont\char207}}1
    {Р}{{\selectfont\char208}}1
    {С}{{\selectfont\char209}}1
    {Т}{{\selectfont\char210}}1
    {У}{{\selectfont\char211}}1
    {Ф}{{\selectfont\char212}}1
    {Х}{{\selectfont\char213}}1
    {Ц}{{\selectfont\char214}}1
    {Ч}{{\selectfont\char215}}1
    {Ш}{{\selectfont\char216}}1
    {Щ}{{\selectfont\char217}}1
    {Ъ}{{\selectfont\char218}}1
    {Ы}{{\selectfont\char219}}1
    {Ь}{{\selectfont\char220}}1
    {Э}{{\selectfont\char221}}1
    {Ю}{{\selectfont\char222}}1
    {Я}{{\selectfont\char223}}1
}
\lstloadlanguages{XML, SQL, C}%Загружаемые языки
\lstdefinelanguage{SQL}
{
    keywords={create, table, primary, foreign, references, key, not, null, select,
        insert, update, where, from, as, into, values, set, delete, order, by, and,
        distinct, max, min, group, having, count, left, right, full, join, on, union, plan, collate, all, returns, procedure, begin, end, suspend, generator, to, active, trigger, for, before, after, is, if, then, new, old, gen_id, position, inactive, grant, revoke, with, option, public, domain, collation, character, translation, usage, cascade, restrict, sequence, increment, minvalue, maxvalue, start},
    keywordstyle=\color{darkblue},
    ndkeywords={varchar, integer, smallint, char},
    ndkeywordstyle=\color{cyan},
    sensitive=false,
    morecomment=[l]--,
    morecomment=[s][\color{orange}]{/*}{*/},
    morestring=[d]',
    morestring=[d]",
    stringstyle=\color{purple}
}
\lstdefinestyle{listSQL}{
    language=SQL,
    belowcaptionskip=5pt,
    xleftmargin=0.5cm,
    numbers=left,
    numberstyle=\scriptsize\usefont{T2A}{PTMono-TLF}{m}{n},%шрифт нумерации
    frame=single,
    emph={},
    emphstyle=\color{red},
    basicstyle=\small\usefont{T2A}{PTMono-TLF}{m}{n},
    columns=fullflexible,
    showstringspaces=false,
    commentstyle=\color{gray}\upshape}
\begin{document}
    \lstinputlisting[label=dbstart, style=listSQL]{dbstart.txt}
\end{document}

A screenshot, of the compiled output, of the above code.

1
  • 1
    I think the fundamental question here is that lstlistings has poor support for non-ASCII characters so cannot distinguish between word break or not when these characters are used. Maybe you have to switch to minted.
    – user202729
    Aug 14 at 13:50

2 Answers 2

3

listings seems to not reset the whitespace status correctly, when literate is used.

As a side-note, I believe that all \selectfont commands are unnecessary, so I removed them in the below code.

You could try this:

\makeatletter
\lstset{
    keepspaces=true,
    breaklines=true,
    breakatwhitespace=true,
    breakindent=0pt,
    literate={а}{\lst@whitespacefalse\char224 }1
    {б}{\lst@whitespacefalse\char225 }1
    {в}{\lst@whitespacefalse\char226 }1
    {г}{\lst@whitespacefalse\char227 }1
    {д}{\lst@whitespacefalse\char228 }1
    {е}{\lst@whitespacefalse\char229 }1
    {ё}{\"e }1
    {ж}{\lst@whitespacefalse\char230 }1
    {з}{\lst@whitespacefalse\char231 }1
    {и}{\lst@whitespacefalse\char232 }1
    {й}{\lst@whitespacefalse\char233 }1
    {к}{\lst@whitespacefalse\char234 }1
    {л}{\lst@whitespacefalse\char235 }1
    {м}{\lst@whitespacefalse\char236 }1
    {н}{\lst@whitespacefalse\char237 }1
    {о}{\lst@whitespacefalse\char238 }1
    {п}{\lst@whitespacefalse\char239 }1
    {р}{\lst@whitespacefalse\char240 }1
    {с}{\lst@whitespacefalse\char241 }1
    {т}{\lst@whitespacefalse\char242 }1
    {у}{\lst@whitespacefalse\char243 }1
    {ф}{\lst@whitespacefalse\char244 }1
    {х}{\lst@whitespacefalse\char245 }1
    {ц}{\lst@whitespacefalse\char246 }1
    {ч}{\lst@whitespacefalse\char247 }1
    {ш}{\lst@whitespacefalse\char248 }1
    {щ}{\lst@whitespacefalse\char249 }1
    {ъ}{\lst@whitespacefalse\char250 }1
    {ы}{\lst@whitespacefalse\char251 }1
    {ь}{\lst@whitespacefalse\char252 }1
    {э}{\lst@whitespacefalse\char253 }1
    {ю}{\lst@whitespacefalse\char254 }1
    {я}{\lst@whitespacefalse\char255 }1
    {А}{\lst@whitespacefalse\char192 }1
    {Б}{\lst@whitespacefalse\char193 }1
    {В}{\lst@whitespacefalse\char194 }1
    {Г}{\lst@whitespacefalse\char195 }1
    {Д}{\lst@whitespacefalse\char196 }1
    {Е}{\lst@whitespacefalse\char197 }1
    {Ё}{\"E }1
    {Ж}{\lst@whitespacefalse\char198 }1
    {З}{\lst@whitespacefalse\char199 }1
    {И}{\lst@whitespacefalse\char200 }1
    {Й}{\lst@whitespacefalse\char201 }1
    {К}{\lst@whitespacefalse\char202 }1
    {Л}{\lst@whitespacefalse\char203 }1
    {М}{\lst@whitespacefalse\char204 }1
    {Н}{\lst@whitespacefalse\char205 }1
    {О}{\lst@whitespacefalse\char206 }1
    {П}{\lst@whitespacefalse\char207 }1
    {Р}{\lst@whitespacefalse\char208 }1
    {С}{\lst@whitespacefalse\char209 }1
    {Т}{\lst@whitespacefalse\char210 }1
    {У}{\lst@whitespacefalse\char211 }1
    {Ф}{\lst@whitespacefalse\char212 }1
    {Х}{\lst@whitespacefalse\char213 }1
    {Ц}{\lst@whitespacefalse\char214 }1
    {Ч}{\lst@whitespacefalse\char215 }1
    {Ш}{\lst@whitespacefalse\char216 }1
    {Щ}{\lst@whitespacefalse\char217 }1
    {Ъ}{\lst@whitespacefalse\char218 }1
    {Ы}{\lst@whitespacefalse\char219 }1
    {Ь}{\lst@whitespacefalse\char220 }1
    {Э}{\lst@whitespacefalse\char221 }1
    {Ю}{\lst@whitespacefalse\char222 }1
    {Я}{\lst@whitespacefalse\char223 }1
}
1
  • How do I deal with the "..." character? There are two options: {…}{\lst@whitespacefalse\char46\char46\char46 }1 {…}{\lst@whitespacefalse\ldots }1 Both work. How to do it right? Can use package \usepackage{listingsutf8}? But it doesn't support: \lstinline, Environment lstlisting, Environments de ned by \lstnewenvironment. @Ulrike Fischer
    – Youra_P
    Aug 14 at 19:06
3

There seem to be all kinds of font and encoding issues. Создаем uses a regular LATIN CAPITAL LETTER (U+0043), e.g., instead of С (U+0421 CYRILLIC CAPITAL LETTER ES). \inputenc hasn't been needed for a couple of years; \usepackage{filecontents} is no longer needed, either.

This example for LuaLaTeX works for me pretty much as intended, perhaps it can get you started, too:

\documentclass{article}
 \usepackage{fontspec}
    \setsansfont{PT Sans}
\usepackage[english,russian]{babel}

\usepackage{xcolor}
    \definecolor{gray}{rgb}{0.4,0.4,0.4}
    \definecolor{darkblue}{rgb}{0.0,0.0,0.6}
    \definecolor{cyan}{rgb}{0.0,0.6,0.6}

\renewcommand{\familydefault}{\sfdefault}

\usepackage{listings}
\lstset{%
    keepspaces=true,
    breaklines=true,
    breakatwhitespace=true,
}

\lstloadlanguages{SQL}
\lstdefinelanguage{SQL}{%
    keywords={create, table, primary, foreign, references, key, not, null, 
    select, insert, update, where, from, as, into, values, set, delete, 
    order, by, and, distinct, max, min, group, having, count, left, right, 
    full, join, on, union, plan, collate, all, returns, procedure, begin, end, 
    suspend, generator, to, active, trigger, for, before, after, is, if, then, 
    new, old, gen_id, position, inactive, grant, revoke, with, option, public, 
    domain, collation, character, translation, usage, cascade, restrict, 
    sequence, increment, minvalue, maxvalue, start},
    keywordstyle=\color{darkblue},
    ndkeywords={varchar, integer, smallint, char},
    ndkeywordstyle=\color{cyan},
    sensitive=false,
    stringstyle=\color{purple}
}

\lstdefinestyle{listSQL}{%
    language=SQL,
    belowcaptionskip=5pt,
    xleftmargin=0.5cm,
    numbers=left,
    numberstyle=\scriptsize,
    frame=single,
    basicstyle=\small,
    columns=fullflexible,
    commentstyle=\color{gray}\upshape
}

\begin{filecontents*}{dbstart.txt}
-- Создаем таблицу Группа
create table sgroup (
id integer not null,
group_name varchar(40),
primary key (id));  -- уникальный ключ id
-- Создаем таблицу Студент
create table student (
id integer not null,
id_group integer not null, -- поле для связи с таблицей Группа
student_name varchar(40),
last_name varchar(40),
middle_name varchar(40),
sex char(1),
age smallint,
primary key (id), -- уникальный ключ id
foreign key (id_group) references sgroup (id)); -- определение связи с таблицей Группа
-- Создаем таблицу Хобби
create table hobby (
id integer not null, -- уникальный ключ
hobby_name varchar(100),
primary key (id)); -- уникальный ключ id
-- Создаем таблицу Студент-Хобби
create table student_hobby (
id_student integer not null, -- поле для связи с таблицей Студент
id_hobby integer not null, -- поле для связи с таблицей Хобби
primary key (id_student, id_hobby), -- уникальный ключ составной id_student, id_hobby
foreign key (id_hobby) references hobby (id), -- определение связи с таблицей Хобби
foreign key (id_student) references student (id)); -- определение связи с таблицей Студент
\end{filecontents*}

\begin{document}
    \lstinputlisting[label=dbstart, style=listSQL]{dbstart.txt}
\end{document}

enter image description here

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.