2

I have the following set up for my listings:

\documentclass[russian,pdf]{beamer}
\mode<presentation>{}

\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}
\usepackage{graphicx, subfig, amsmath, mathtools, lmodern, listings, lipsum, courier}

\definecolor{darkred}{rgb}{0.7, 0, 0}
\definecolor{darkgreen}{rgb}{0, 0.4, 0}
\definecolor{navy}{rgb}{0, 0, 0.5}
\definecolor{purple}{rgb}{0.45, 0, 0.45}
\definecolor{codebg}{rgb}{0.95, 0.95, 0.95}

\lstset{
language=C++,                
basicstyle=\ttfamily,
keywordstyle=\color{navy},
stringstyle=\color{darkred},
commentstyle=\color{darkgreen},
morecomment=[l][\color{purple}]{\#},
backgroundcolor=\color{codebg},
showspaces=false, 
showstringspaces=false,
showtabs=false,
} 

\begin{document}

\section{C++}
\begin{frame}[fragile]
\begin{lstlisting}
    struct employee
    {
        char name [64] ;
        long employee_id;
        float salary;
        char phone[10];
        int office_number;
    };
\end{lstlisting}
\end{frame}

\end{document}

And it produces enter image description here

But I want it to be courier font. What is wrong with my latex source code?

1 Answer 1

2

Change the beginning of the frame to:

\begin{frame}[fragile]
\fontencoding{T1}\selectfont
\begin{lstlisting}

If you have to do this for many frames, you can alternatively add these lines to your preamble:

\usepackage{etoolbox}
\AtBeginEnvironment{lstlisting}{\fontencoding{T1}\selectfont}

Alternatively, you can compile with xelatex, loading the fontspec package.

enter image description here

4
  • 1
    Thank you for your answer! Now code looks great, but the \fontencoding{T1}\selectfont conflicts with russian language and I can't add comments in russian to the slides with code. Second option you offered unfortunately doesn't work at all:C
    – Dankevich
    Apr 17, 2016 at 12:44
  • Do you mean \AtBeginEnvironment?
    – Bernard
    Apr 17, 2016 at 12:46
  • Yes, \AtBeginEnvironment doesn't change a thing. I found out everything works okay if I switch from T1 to T2A when I need russian and then back again to use listings. Kinda 'dirty' solution, but still fine)
    – Dankevich
    Apr 17, 2016 at 12:46
  • 1
    I had a typo in my initial code: I wrote \AtBeginEnvironment{listings} with an s. Without an s, it should work. For the problem of comments, perhaps a begin{russian} … \end{russian} should do the trick. That said, with xelatex, you don't have to use font encodings, and it should compile smoothly.
    – Bernard
    Apr 17, 2016 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.