コメントに英語とロシア語の記号が混在するリスト

コメントに英語とロシア語の記号が混在するリスト

私はlistingsLaTeX でコードをフォーマットするためのパッケージ.cpp。私のコードでは、コメントに英語とロシア語の単語を混在させる必要があります。フォントは等幅フォント (推奨Courierファミリ) である必要があります。

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

例として次のコードを使用します。

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

結果として、XeLaTeX (MiKTeX 2.9) は次の PDF を生成します。

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

申し訳ありませんが、画像を投稿するには評判が足りません。:( ご覧のとおり、ロシア語ではスペースはすべて無視されます

どうすれば修正できますか?

答え1

代替案としては、minted次の方法があります。以下のコマンドを実行する必要があることに注意してxelatexください--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}

ここに画像の説明を入力してください

答え2

残念ながら、listingsUnicode ではうまく動作しません。

実用的なハックとしては、キリル文字のコメントをめったに使用されない文字で囲むことです。

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

ここに画像の説明を入力してください

答え3

LaTeXのコメント行を有効にするには、texcl=true

これはソースコードで調整する必要はありません。コメントにTeXコマンドが含まれている場合は問題が発生する可能性があります(たとえば、コメント内の%は としてマスクする必要があります\%)。ただし、各コメントをあまり使用されない文字でカプセル化するよりも単語数が少なくなる場合があります。

あなたの例:

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

結果:

ここに画像の説明を入力してください

関連情報