# Listing with mixed english and russian symbols in comments

I'm using the listings package for formatting .cpp code in LaTeX. My code needs to have mixed English and Russian words in comments. The font must be monospaced (preffered Courier family).

/* Prints Hello World */
#include <stdio.h>
int main (void){
printf ("Hello World!"); // This is an english commentary
return 0; // А это русский комментарий
}


Im using the following code as an example:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}
\newfontfamily\cyrillicfont[Script=Cyrillic]{Times New Roman}
\newfontfamily\cyrillicfontsf[Script=Cyrillic]{Arial}
\newfontfamily\cyrillicfonttt[Script=Cyrillic]{Courier New}
\usepackage{polyglossia}
\setdefaultlanguage{russian}
\usepackage{listings}

\lstdefinestyle{CppCodeStyle}{
basicstyle=\footnotesize\ttfamily,
language={[ANSI]C++},
keywordstyle=\bfseries,
showstringspaces=false,
morekeywords={include, printf},
}

\begin{document}

\begin{lstlisting}[style={CppCodeStyle}]
/* Prints Hello World */
#include <stdio.h>
int main (void){
printf ("Hello World!"); // This is an english commentary
return 0; // А это русский комментарий
}
\end{lstlisting}

\end{document}


As I result XeLaTeX (in MiKTeX 2.9) produces following PDF:

/* Prints Hello World */
#include <stdio.h>
int main (void){
printf ("Hello World!"); // This is an english commentary
return 0; // Аэторусскийкомментарий
}


Sorry, I don't have enough reputation to post images :( As you can see, all spaces in Russian are ignored.

How can I fix it?

-
Similar question: tex.stackexchange.com/q/136176/1871, I find the unicode limitation of the great listings package very annoying; and I don't see a fundamental reason for this limitation. Lenguages like C++ are know accepting unicode variable names, however listings is not catching up. –  alfC Nov 14 '13 at 5:32

Maybe minted is an alternative. Please note that you must run xelatex with --shell-escape:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}
\newfontfamily\cyrillicfont[Script=Cyrillic]{Times New Roman}
\newfontfamily\cyrillicfontsf[Script=Cyrillic]{Arial}
\newfontfamily\cyrillicfonttt[Script=Cyrillic]{Courier New}
\usepackage{polyglossia}
\setdefaultlanguage{russian}
\usepackage{minted}

\begin{document}
Test
\begin{minted}{c++}
/* Prints Hello World */
#include <stdio.h>
int main (void){
printf ("Hello World!"); // This is an english commentary
return 0; // А это русский комментарий
}
\end{minted}

\end{document}


-
Hi @Marco, i forgot to mention that Im working in Windows environment, Ive followed the set-up instruction from the minted package documentation, but after all instalation procedures Ive got an error message: "! LaTeX Error: File document.out.pyg' not found.". –  Aganezor Apr 14 '13 at 17:14
@Aganezor: I like my comment there: tex.stackexchange.com/questions/108661/… ;-) -- However this is a good point to ask a follow up question if you want to use minted –  Marco Daniel Apr 14 '13 at 17:18
I figured-out what the problem was - I just need to restart my TeX editor (Eclipse) :) Now everything works fine. Thank you for your help and for very interesting package :) –  Aganezor Apr 14 '13 at 17:34

Unfortunately listings doesn't work well with Unicode.

A working hack is to enclose the cyrillic comments between a rarely used character:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}
\newfontfamily\cyrillicfont[Script=Cyrillic]{Times New Roman}
\newfontfamily\cyrillicfontsf[Script=Cyrillic]{Arial}
\newfontfamily\cyrillicfonttt[Script=Cyrillic]{Courier New}
\usepackage{polyglossia}
\setdefaultlanguage{russian}
\usepackage{listings}

\lstdefinestyle{CppCodeStyle}{
basicstyle=\footnotesize\ttfamily,
language={[ANSI]C++},
keywordstyle=\bfseries,
showstringspaces=false,
morekeywords={include, printf},
escapeinside=§§,
escapebegin=\begin{russian}\commentfont,
escapeend=\end{russian},
}
\newcommand{\commentfont}{\ttfamily}

\begin{document}

\begin{lstlisting}[style={CppCodeStyle}]
/* Prints Hello World */
#include <stdio.h>
int main (void){
printf ("Hello World!"); // This is an english commentary
return 0; // §А это русский комментарий§
}
\end{lstlisting}

\end{document}


-
Hi, @egreg, Your solution works, but for big amount of code its quite uncomfortable. Besides, for \lstinputlisting it will not work :( –  Aganezor Apr 14 '13 at 17:09
@Aganezor I know. It's a big nuisance. :( –  egreg Apr 14 '13 at 18:01