5

How do you change the font size globally for the \texttt environment?

I can do it for the verbatim environment with the following pair of commands

\newcommand{\verbatimfont}[1]{\renewcommand{\verbatim@font}{\ttfamily#1}}
\verbatimfont{\small}

How do I accomplish the same thing for the \texttt environment?

1
  • \texttt is not an environment and it doesn't set a size. Jan 10, 2018 at 22:52

2 Answers 2

4

You can use the fancyvrb package that offers many other customizations (you can also check the fvextra package that adds many features).

\documentclass{article}
\usepackage{fancyvrb}

\RecustomVerbatimEnvironment{verbatim}{Verbatim}{fontsize=\footnotesize}

\begin{document}

This is inline \verb|verbatim|, the size doesn't change.
But it can be changed when we are in a \verb|verbatim|
enviroment
\begin{verbatim}
This is inline \verb|verbatim|, the size doesn't change.
But it can be changed when we are in a \verb|Verbatim|
enviroment
\end{verbatim}
Isn't it nice?

\end{document}

enter image description here

In order to change the size of the monospaced font, there is a “nasty” trick. Modify the value in the definition of \ttscale to suit your need.

\documentclass{article}

\newcommand{\ttscale}{s*[0.9]}
\DeclareFontFamily{OT1}{cmtt}{\hyphenchar\font -1 }
\DeclareFontShape{OT1}{cmtt}{m}{n}{
        <-9>   \ttscale cmtt8
        <9-10> \ttscale cmtt9
        <10-12>\ttscale cmtt10
        <12->  \ttscale cmtt12
      }{}
\DeclareFontShape{OT1}{cmtt}{m}{it}{
        <->    \ttscale cmitt10
      }{}
\DeclareFontShape{OT1}{cmtt}{m}{sl}{
        <->    \ttscale cmsltt10
      }{}
\DeclareFontShape{OT1}{cmtt}{m}{sc}{
        <->    \ttscale cmtcsc10
      }{}
\DeclareFontShape{OT1}{cmtt}{m}{ui}
       {<->ssub*cmtt/m/it}{}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
       {<->ssub*cmtt/m/n}{}
\DeclareFontShape{OT1}{cmtt}{bx}{it}
       {<->ssub*cmtt/m/it}{}
\DeclareFontShape{OT1}{cmtt}{bx}{ui}
       {<->ssub*cmtt/m/it}{}

\begin{document}

ABC \texttt{ABC} 

\Large ABC \texttt{ABC}

\end{document}

enter image description here

Compare with the original:

enter image description here

3

Font switches are typically robust. You could update the \ttfamily font switch to append a sizing element to it in the same way you would for your \verbatimfont:

enter image description here

\documentclass{article}

\usepackage{letltxmacro}
% https://tex.stackexchange.com/q/88001/5764
\LetLtxMacro\oldttfamily\ttfamily
\DeclareRobustCommand{\ttfamily}{\oldttfamily\csname ttsize\endcsname}
\newcommand{\setttsize}[1]{\def\ttsize{#1}}%

\begin{document}

\setttsize{\Large}% \ttfamily will be \Large

Something \texttt{texttt}.

\begingroup
\ttfamily Something texttt.
\endgroup

\setttsize{}% \ttfamily will be \normalsize

Something \texttt{texttt}.

\begingroup
\ttfamily Something texttt.
\endgroup

\end{document}
2
  • Can i do it without the letltxmacro package. I mean using only \let in that third line instead of \LetLtxMacro
    – Aakash
    Oct 8, 2019 at 3:24
  • @Aakash: It could depend on your use-case. For clarity, see When to use \LetLtxMacro?
    – Werner
    Oct 8, 2019 at 4:15

You must log in to answer this question.

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