7

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?

1
  • 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
    Commented Nov 14, 2013 at 5:32

3 Answers 3

5

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

3
  • 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
    Commented Apr 14, 2013 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 Commented Apr 14, 2013 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
    Commented Apr 14, 2013 at 17:34
4

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

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

You could try to activate LaTeX comment lines with texcl=true

This needs no adaption in your source code. You may get problems if your comments contain TeX-commands (e.g. a % in the comments should be masked as \%). But this may be less word then encapsulate each comment in a rare used character,

Your 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={},
texcl=true,     %<---- added
}

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

The result:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .