0

This question already has an answer here:

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.

marked as duplicate by Pouya, user31729, Guido, Werner, Adam Liter Nov 5 '14 at 21:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

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

  • OK, but there are a way to set some kind of "generic" language, so LaTeX would not try to reformat code? – Dennis Yurichev Nov 5 '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 5 '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? – Dennis Yurichev Nov 5 '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 5 '14 at 21:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.