0

I've got something really weird.

Here is MWE:

\documentclass[a4paper,oneside]{book}
\usepackage[english,russian]{babel}
\usepackage{inputenc}
\usepackage{listings}

\usepackage{fontspec}
% fonts
\setmonofont{Droid Sans Mono}

\lstset{
    basicstyle=\ttfamily\small,
    columns=fullflexible,keepspaces,
    inputencoding=utf8
}

\begin{document}

\begin{lstlisting}
; sign-extend input 32-bit value to 64-bit
; расширить входное 32-битное значение до 64-битного
; (shift is arithmetical)
; (арифметический сдвиг)
\end{lstlisting}

\end{document}

The file is encoded in UTF-8 encoding. Here is russian text interleaved with english. I'm running xelatex test.tex and here is PDF screenshot I've got:

Source yurichev.com/tmp3/test.png

As you may see, english text is fine, but as for russian one, numbers 32 and 64 are somewhat swapped at second line and also left bracket is at wrong position at line 4.

But why?

I've tried to move the text into external file and include it with \lstinputlisting, but result is the same.

jub0bs
  • 58,916
  • 1
    Listings and unicode do not go along peacefully. Have a look at this. I didn't flag your question as duplicate, but there is a good chance it will be. – Pouya Nov 05 '14 at 20:50
  • Well, this solution is worked for me: http://tex.stackexchange.com/a/108698/19843 –  Nov 05 '14 at 20:55
  • So you probably can close... –  Nov 05 '14 at 20:56

1 Answers1

0

You have not defined the programming language for your code.

Your code looks like comments.

If you add the rule everything after a ; is a comment, then the Russian code is printed correct.

Code to define the comment:

    morecomment=[l]{;}, %<--- added
    texcl=true          %<--- added    

If you define the language, then the comment style should be defined (you still need the texcl=true)

Full code:

\documentclass[a4paper,oneside]{book}
\usepackage[english,russian]{babel}
\usepackage{inputenc}
\usepackage{listings}

\usepackage{fontspec}
% fonts
\setmonofont{Droid Sans Mono}

\lstset{
    basicstyle=\ttfamily\small,
    columns=fullflexible,keepspaces,
    inputencoding=utf8
    morecomment=[l]{;}, %<--- added
    texcl=true          %<--- added    
}

\begin{document}

\begin{lstlisting}
; sign-extend input 32-bit value to 64-bit
; расширить входное 32-битное значение до 64-битного
; (shift is arithmetical)
; (арифметический сдвиг)
\end{lstlisting}
\end{document}

The result:enter image description here

knut
  • 8,838
  • OK, but there are a way to set some kind of "generic" language, so LaTeX would not try to reformat code? –  Nov 05 '14 at 21:10
  • What do you mean with reformat? DO you mean keywords in bold? Maybe it is enough if you define the language (which one?) and redefine keywordstyle. – knut Nov 05 '14 at 21:25
  • I mean, what if I have some unknown to LaTeX programming language? How to set all variables so listing would be included intact? –  Nov 05 '14 at 21:32
  • 1
    Still not sure where you see the problem. You could define your own style with \lstdefinestyle and let everybody use it. – knut Nov 05 '14 at 21:47