2

(Latex, Mac OS X)

Imagine we want use courier font. Start from this:

\texttt{Wanna nice monospace font here}
{\ttfamily TeleType And here}
\begin{Verbatim}[fontfamily=courier]
    And here please too!
\end{Verbatim}

This is not working. First two sections (texttt and ttfamily) have monospace font, but from stone age. In 2015 that's not good. Last section (begin/end Verbatim) doesn't work completely - it prints totally non-monospace font which looks like times-new-roman which is rather far from monospace (especially courier).

I've tried

\usepackage{courier}

And even

\usepackage[T1]{fontenc} % Why user should care about it in 2015 btw?
                         % T2A not working too.

So this package makes "times-new-roman-like" font instead of any monospace font. Well, maybe useless package?

As I know, many latex packages are incompatible with each other. I've tried to investigate it, but no success.

Here is the header of latex document:

\documentclass[10pt]{book}
\usepackage[a5paper,left=3cm,right=2cm,top=1.5cm,bottom=1.5cm]{geometry}
\usepackage[russian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{import}
\usepackage{listings}
\usepackage{fancyvrb}
\usepackage{color}
\usepackage{hyperref}
\usepackage{setspace}
\usepackage{courier}
\usepackage[T1]{fontenc}

** So the main question is: How to get nice monospace font Courier in latex? **

** Edit: Of course I need Courier in verbatim environmend AND with russian letters. As I see, latex is rather ancient to deal with it. Could be there alternative solutions? Like: 1) Another nice monospace font like Courier, but better than standard stone-age font. 2) Another latex engine? xetex, xelatex? can they deal with it? **

Thanks.

  • 2
    Welcome to TeX.SX! There's no Cyrillic support with Courier. If your verbatim parts don't use Cyrillic, something can be done, of course. – egreg Sep 3 '15 at 11:11
1

If your verbatim text doesn't contain Cyrillic letters, you can modify the setup macro of fancyvrb to use the T1 encoding. The problem is that TeX distributions don't feature a Cyrillic Courier compatible font.

Similarly, you can use a similar declaration for listings.

\documentclass[10pt]{book}
\usepackage[a5paper,left=3cm,right=2cm,top=1.5cm,bottom=1.5cm]{geometry}

\usepackage[T1,T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}

\usepackage{courier}

\usepackage{listings}
\usepackage{fancyvrb}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\FV@SetupFont}
  {\FV@BaseLineStretch}
  {\fontencoding{T1}\FV@BaseLineStretch}
  {}{}
\makeatother

\lstset{
  basicstyle=\fontencoding{T1}\ttfamily,
  columns=fullflexible
}

\begin{document}

Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.

\begin{Verbatim}
some verbatim text in Courier
\end{Verbatim}

Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.

\begin{lstlisting}
some verbatim text in Courier
\end{lstlisting}

\end{document}

enter image description here

A monospaced font that has coverage for Cyrillic is PTMono; unfortunately, listings does not behave well with UTF-8 characters (check with listings-utf8 that however requires using \lstinputlisting).

\documentclass[10pt]{book}
\usepackage[a5paper,left=3cm,right=2cm,top=1.5cm,bottom=1.5cm]{geometry}

\usepackage[T1,T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}

\usepackage[scaled=0.9]{PTMono}

\usepackage{fancyvrb}

\begin{document}

Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.

\begin{Verbatim}
some дословно text in PTMono
\end{Verbatim}

Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.

\end{document}

enter image description here

If you're keen to switch to XeLaTeX/LuaLaTeX, then there's no big problem, if you have an OpenType/TrueType version of Courier on your system that supports Cyrillic; in my case it's called Courier New.

\documentclass[10pt]{book}
\usepackage[a5paper,left=3cm,right=2cm,top=1.5cm,bottom=1.5cm]{geometry}

\usepackage{fontspec}
\usepackage[russian]{babel}

\usepackage{fancyvrb}

\setmainfont{Linux Libertine O} % or whatever
\setmonofont[Scale=MatchLowercase]{Courier New}

\begin{document}

Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.

\begin{Verbatim}
some дословно text in Courier
\end{Verbatim}

Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.
Этот текст на русском языке.

\end{document}

enter image description here

The same as above can be said about lstlisting. You may also want to look at minted.

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.