Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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; // А это русский комментарий
    }

I`m 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},
commentstyle={},
}

\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?

share|improve this question
    
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

2 Answers 2

up vote 4 down vote accepted

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}

enter image description here

share|improve this answer
    
Hi @Marco, i forgot to mention that I`m working in Windows environment, I`ve followed the set-up instruction from the minted package documentation, but after all instalation procedures I`ve 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},
commentstyle={},
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}

enter image description here

share|improve this answer
    
Hi, @egreg, Your solution works, but for big amount of code it`s 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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