我是 LaTeX 的新手,我找不到下面描述的问题的原因。
我需要在文档中插入几个 C++ 代码片段,因此我使用列表软件包。编译器pdflatex一切正常,但等宽字体不适用于代码列表环境。
- 我已经尝试修复此问题,包括贝拉莫诺包。但是\ttfamily命令似乎对此“一无所知”。
- 另外,我不能使用导游包,因为相应的\文本命令不能用于基本风格的参数\lst设置。所以我使用\ttfamily。但它不起作用:-)
这是我的“简化” LaTeX 代码:
\documentclass{book}
\usepackage[beramono]{classicthesis} % The layout is based on the Classic Thesis style
\usepackage{arsclassica} % Modifies the Classic Thesis package
\usepackage{amsfonts}
% Just in case: The main document language is Russian, so I use cp1251.
% Of course, the C++ code doesn't have any cyrillic letters
\usepackage[T1, T2A]{fontenc}
\usepackage[cp1251]{inputenc}
\usepackage[english, russian]{babel}
\usepackage{xcolor}
\usepackage{listings}
% Here I change the default font, because the \textbf command doesn't work on it
\renewcommand*\rmdefault{iwona}
\definecolor{BackgroundColor}{rgb}{0.9,0.9,0.9}
\definecolor{OliveGreen}{rgb}{0,0.6,0}
\lstset{
basicstyle=\normalsize\ttfamily, % Doesn't work !
language=C++,
backgroundcolor=\color{BackgroundColor},
tabsize=4,
captionpos=b,
%tabsize=3,
frame=lines,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
breaklines=true,
showstringspaces=false,
keywordstyle=\color{blue},
commentstyle=\color{OliveGreen},
stringstyle=\color{red}
}
\begin{document}
Example of code snippet:
\begin{lstlisting}[language=C++]
#include <Bo/services/Ucntl.h>
ssize_t
__attribute__((nonnull(1))
AorpRelease(
aorp_object_t aThis,
aorp_opflags_t Flags /* = 0 */,
struct aorp_error *anErrPtr /* = NULL */
);
\end{lstlisting}
\end{document}
注意:我不同意只保留衬线。& 符号是无法满足的。
我希望你们中有人能帮助找出问题所在。
PS:我还没有绝望到去尝试这个:http://www.radamir.com/tex/ttf-tex.htm看上去相当痛苦。
答案1
您会收到一条警告:
LaTeX Font Warning: Font shape `T2A/fvm/m/n' undefined
(Font) using `T2A/cmr/m/n' instead on input line 47.
你不应该忽视它。
解决方案:告诉 LaTeX 您没有在列表中使用西里尔字母。
basicstyle=\normalsize\fontencoding{T1}\ttfamily,
完整代码
\documentclass{book}
\usepackage[beramono]{classicthesis} % The layout is based on the Classic Thesis style
\usepackage{arsclassica} % Modifies the Classic Thesis package
\usepackage{amsfonts}
% Just in case: The main document language is Russian, so I use cp1251.
% Of course, the C++ code doesn't have any cyrillic letters
\usepackage[T1, T2A]{fontenc}
\usepackage[cp1251]{inputenc}
\usepackage[english, russian]{babel}
\usepackage{xcolor}
\usepackage{listings}
% Here I change the default font, because the \textbf command doesn't work on it
\renewcommand*\rmdefault{iwona}
\definecolor{BackgroundColor}{rgb}{0.9,0.9,0.9}
\definecolor{OliveGreen}{rgb}{0,0.6,0}
\lstset{
basicstyle=\normalsize\fontencoding{T1}\ttfamily,
language=C++,
backgroundcolor=\color{BackgroundColor},
tabsize=4,
captionpos=b,
%tabsize=3,
frame=lines,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
breaklines=true,
showstringspaces=false,
keywordstyle=\color{blue},
commentstyle=\color{OliveGreen},
stringstyle=\color{red}
}
\begin{document}
Example of code snippet:
\begin{lstlisting}[language=C++]
#include <Bo/services/Ucntl.h>
ssize_t
__attribute__((nonnull(1))
AorpRelease(
aorp_object_t aThis,
aorp_opflags_t Flags /* = 0 */,
struct aorp_error *anErrPtr /* = NULL */
);
\end{lstlisting}
\end{document}