
我正在使用listings
.cpp
用於在 LaTeX 中格式化程式碼的套件。我的程式碼需要在註釋中混合英語和俄語單字。字體必須是等寬字體(首選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
不幸的是,listings
它不能很好地與 Unicode 配合使用。
一個有效的技巧是將西里爾文註釋括在很少使用的字元之間:
\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}
結果: