2

I need to set a certain monospace font only for the verbatim parts of the document. How can one do this?

For example, I understand that the commands

\usepackage{lmodern}
\renewcommand*\familydefault{\ttdefault} %% Only if the base font of the document is to be typewriter style
\usepackage[T1]{fontenc}

will set the font for the entire document, not solely for the verbatim part.

sequence
  • 197

2 Answers2

5

Do not redefine \familydefault but \ttfamily, i.e.:

\renewcommand*\ttdefault{txtt} % or ...
\renewcommand\ttfamily{\sffamily}

But you can do that simply loading a monospaced font:

\usepackage{nimbusmono} % for instance

Or using xelatex (or lualatex):

\usepackage{fontspec}
\setmonofont{Bitstream Vera Sans} 

The LaTeX Font catalogue explain the particular setting of every monospaced typewriter font.

Fran
  • 80,769
2

As far as I can tell, this is what \verbatim@font is for. Example setting this font to newtxtt family in T1 encoding, medium series and upright shape:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\let\normalttdefault\ttdefault
\usepackage{newtxtt}
\let\ttdefault\normalttdefault  % restore normal \ttdefault

\makeatletter
\renewcommand{\verbatim@font}{\usefont{T1}{newtxtt}{m}{n}}
\makeatother

\begin{document}

\texttt{This is typeset with cmtt. Look for instance at the *, \textdollar\ and
  @ characters. What follows is verbatim and uses a different font:}
\begin{verbatim}
The quick brown fox jumps over the lazy dog.
a += 2*(1 + l[44])
l = {1,...5}
#~&\^@_|3/%$÷`'"
\end{verbatim}

\end{document}

newtxtt verb

For comparison, the default verbatim font (cmtt when using pdfTeX) gives:

cmtt verb

This was obtained with:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{textcomp}

\begin{document}

\texttt{This is typeset with cmtt. Look for instance at the *, \textdollar\ and
  @ characters. What follows is verbatim and uses the same font:}
\begin{verbatim}
The quick brown fox jumps over the lazy dog.
a += 2*(1 + l[44])
l = {1,...5}
#~&\^@_|3/%$÷`'"
\end{verbatim}

\end{document}
frougon
  • 24,283
  • 1
  • 32
  • 55