我正在尝试使用该包lstlisting
,并且大部分代码都显示正确。
但字符_
、{
和}
被替换为其他字符。字符_
被替换为"
,{
被替换为-
依此类推。
你知道出了什么问题吗?
\documentclass{article}
\usepackage{beramono}
\usepackage{xcolor}
\usepackage{listings}
\lstset{language=C, commentstyle=\color{green},
backgroundcolor=\color{white}, keywordstyle=\color{blue},
basicstyle = \ttfamily \color{black} \footnotesize }
\begin{document}
\begin{lstlisting}[]
int main(int argc, char* argv){
int niter=0;
int i,count=0;
double z,pi,x,y;
#pragma omp parallel for
for (i=0; i<niter; i++) {
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = x*x+y*y;
if (z<=1)
count++;
}
#pragma omp task
pi=(double)count/niter*4;
}
\end{lstlisting}
\end{document}
答案1
如果你有
\documentclass{article}
\usepackage{beramono}
\usepackage{xcolor}
\usepackage{listings}
\lstset{language=C, commentstyle=\color{green},
backgroundcolor=\color{white}, keywordstyle=\color{blue},
basicstyle = \ttfamily \color{black} \footnotesize }
\begin{document}
\begin{lstlisting}[]
int main(int argc, char* argv){
int niter=0;
int i,count=0;
double z,pi,x,y;
#pragma omp parallel for
for (i=0; i<niter; i++) {
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = x*x+y*y;
if (z<=1)
count++;
}
#pragma omp task
pi=(double)count/niter*4;
}
\end{lstlisting}
\end{document}
你收到警告
LaTeX Font Warning: Font shape `OT1/fvm/m/n' undefined
(Font) using `OT1/cmr/m/n' instead on input line 9.
因为 Bera Mono 字体只适用于 T1 编码。事实上,列表使用的字体是默认的文本字体,其位置上的字符与_|{}
。
如果您不想将 T1 设为默认文档编码,您可以仅为以下情况启用它listings
:
\documentclass{article}
\usepackage{beramono}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
language=C,
commentstyle=\color{green},
backgroundcolor=\color{white},
keywordstyle=\color{blue},
basicstyle = \fontencoding{T1}\ttfamily \color{black} \footnotesize,
}
\begin{document}
\begin{lstlisting}[]
int main(int argc, char* argv){
int niter=0;
int i,count=0;
double z,pi,x,y;
#pragma omp parallel for
for (i=0; i<niter; i++) {
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = x*x+y*y;
if (z<=1)
count++;
}
#pragma omp task
pi=(double)count/niter*4;
}
\end{lstlisting}
\end{document}
但是,如果您在文档的其他部分使用\texttt
或,则会遇到同样的问题,因此最简单的解决方法是全局启用:\ttfamily
T1
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{beramono}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
language=C,
commentstyle=\color{green},
backgroundcolor=\color{white},
keywordstyle=\color{blue},
basicstyle = \ttfamily \color{black} \footnotesize,
}
\begin{document}
\begin{lstlisting}[]
int main(int argc, char* argv){
int niter=0;
int i,count=0;
double z,pi,x,y;
#pragma omp parallel for
for (i=0; i<niter; i++) {
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = x*x+y*y;
if (z<=1)
count++;
}
#pragma omp task
pi=(double)count/niter*4;
}
\end{lstlisting}
\end{document}