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?
Best Answer
Maybe
minted
is an alternative. Please note that you must runxelatex
with--shell-escape
: