[Tex/LaTex] Can’t apply monospaced font to listings (when using pdflatex)

fontslistingspdftextypewriter

I'm a newbie to LaTeX and I can't find the cause of a problem described below.


I need to insert several C++ code snippets into my document, so I use the listings package for this purpose. The compiler is pdflatex. Everything works, but the monospaced font just doesn't apply to code within lstlisting environment.

  • I have already tried to fix this with including the beramono package. But the \ttfamily command just seems "ignorant" of it.
  • Also, I can't use the courier package, because the corresponding \texttt command can't be used in basicstyle parameter of \lstset. So I use the \ttfamily. But it doesn't work:-)

Here is my "simplified" LaTeX code:

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

What I have in result: this code definitely doesn't look cute

Note: I'm not OK with just leaving it serif. The ampersand is unsatisfiable.

I hope someone of you guys will help to figure out what's wrong.

P.S. I am not desperate enough to try this: http://www.radamir.com/tex/ttf-tex.htm Looks quite painful.

Best Answer

You get a warning:

LaTeX Font Warning: Font shape `T2A/fvm/m/n' undefined
(Font)              using `T2A/cmr/m/n' instead on input line 47.

that you shouldn't disregard.

Solution: tell LaTeX you're not using Cyrillic in listings.

basicstyle=\normalsize\fontencoding{T1}\ttfamily,

Full code

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

enter image description here