
estoy usando ellistings
Paquete para formatear .cpp
código en LaTeX. Mi código debe tener palabras mezcladas en inglés y ruso en los comentarios. La fuente debe ser monoespaciada ( Courier
familia preferida).
/* Prints Hello World */
#include <stdio.h>
int main (void){
printf ("Hello World!"); // This is an english commentary
return 0; // А это русский комментарий
}
Estoy usando el siguiente código como ejemplo:
\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}
Como resultado, XeLaTeX (en MiKTeX 2.9) produce el siguiente PDF:
/* Prints Hello World */
#include <stdio.h>
int main (void){
printf ("Hello World!"); // This is an english commentary
return 0; // Аэторусскийкомментарий
}
Lo siento, no tengo suficiente reputación para publicar imágenes. :(
Como puedes ver,todos los espacios en ruso se ignoran.
¿Cómo puedo arreglarlo?
Respuesta1
Quizás minted
sea una alternativa. Tenga en cuenta que debe ejecutar xelatex
con --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}
Respuesta2
Lamentablemente listings
no funciona bien con Unicode.
Un truco que funciona es encerrar los comentarios cirílicos entre un carácter poco utilizado:
\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}
Respuesta3
Podrías intentar activar las líneas de comentarios de LaTeX contexcl=true
Esto no necesita adaptación en su código fuente. Puede tener problemas si sus comentarios contienen comandos TeX (por ejemplo, un % en los comentarios debe estar enmascarado como \%
). Pero esto puede requerir menos palabras que encapsular cada comentario en un carácter usado poco común,
Tu ejemplo:
\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}
El resultado: