0

I am writing a document in LaTeX using the standard Computer Modern fonts. The text is mainly in Russian but it includes some listings. I prefer to typeset listings using the standard typewriter font (\ttfamily). It would be really nice if I could use a bold typewriter font for keywords in listings.

If my document was in English only, I would use the bold typewriter font from lmodern:

\usepackage{lmodern}
\usepackage{listings}
\lstset{%
basicstyle=\ttfamily\fontseries{l}\selectfont,
keywordstyle=\ttfamily\fontseries{b}\selectfont
}

However, this cannot be used in my case because lmodern does not support Cyrillic. But what is (theoretically) good is that all the keywords in listings contain Latin letters only. Consequently, my idea is to use the bold typewriter lmodern font (i.e., \ttfamily\fontseries{b}\selectfont) for keywords in listings and to keep Computer Modern fonts in the rest of my document (including the non-keyword entries in listings which contain strings and comments in Russian). Unfortunately, I do not know how to do it. It seems that when I load lmodern package, it changes the fonts throughout the entire document and I lose much of font formatting because of Russian letters. Could someone please help me with this?

Importantly, I need to use LaTeX only (not XeLaTeX, not LuaLaTeX). And I should probably note that the bold typewriter font specifically from lmodern is not really important, I just like how it looks.

8
  • 1
    remove lmodern and change \ttfamily to \fontfamily{lmtt} in keywordstyle Commented Aug 18, 2021 at 22:49
  • @DavidCarlisle, thank you for your suggestion. Unfortunately, in this case, my keywords appear not in a bold typewriter font but in a regular non-bold serif (roman) font.
    – crazytexer
    Commented Aug 18, 2021 at 23:01
  • @DavidCarlisle, oh, I finally got it! The problem was that I did not switch the font encoding. I will update my question shortly.
    – crazytexer
    Commented Aug 18, 2021 at 23:16
  • 1
    it is always best to include complete small test documents in questions Commented Aug 18, 2021 at 23:20
  • 1
    please don't add solutions to the question, it messes up the question and answer site format, you can post it as an answer, thanks Commented Aug 18, 2021 at 23:55

1 Answer 1

3

Thanks to David Carlisle and egreg, I came up with this solution:

\documentclass[12pt]{article}

\usepackage[T1,T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\usepackage{listings}
\lstset{%
basicstyle=\ttfamily,
keywordstyle=\fontencoding{T1}\fontfamily{lmtt}\bfseries
}

\begin{document}

Русский текст. \textbf{Жирный шрифт.}

\begin{lstlisting}[language=Python]
def func(s):
    return s
\end{lstlisting}

\end{document}

Output

2
  • 1
    \fontencoding{T1}\fontfamily{lmtt}\bfseries is simpler and conceptually better.
    – egreg
    Commented Aug 19, 2021 at 7:58
  • @egreg, thank you very much for your comment. Indeed, it is better. I've just updated my answer.
    – crazytexer
    Commented Aug 19, 2021 at 9:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .