所有 \ttfamily 字体改变字体大小

所有 \ttfamily 字体改变字体大小

\documentclass[11pt]{book}

此代码显示默认字体大小。我想更改文档中所有 \ttfamily 字体的字体大小(仅适用于 \ttfamily 字体)。其他字体不变。

我该怎么办?谢谢,

答案1

您可以更新 的定义\ttfamily以包含所需的字体更改。例如,下面\ttfamily已更新为始终使用(例如)。以下\LARGE是 的默认定义\ttfamilylatex.ltx

\DeclareRobustCommand\ttfamily
        {\not@math@alphabet\ttfamily\mathtt
         \fontfamily\ttdefault\selectfont}

以下是以最小示例形式提供的更新版本:

在此处输入图片描述

\documentclass[11pt]{article}
\makeatletter
% Definition of \ttfamily from latex.ltx
\DeclareRobustCommand\ttfamily
        {\not@math@alphabet\ttfamily\mathtt
         \fontfamily\ttdefault\LARGE\selectfont}
\makeatother
\begin{document}
Some normal text.

{\ttfamily Some typewriter text.\par}

Some normal and \texttt{typewriter} text.
\end{document}

上面的例子也说明了为什么不应该这样做。

答案2

最可靠的方法是更改​​字体定义,应用放大系数。我将对 T1 编码执行此操作,放大系数为 1.1。下面是 OT1 编码的类似代码。

\documentclass[11pt]{book}
\usepackage[T1]{fontenc}

\makeatletter
\expandafter\let\csname T1/cmtt/m/n/10.95\endcsname\relax
\begingroup
\nfss@catcodes
\DeclareFontFamily{T1}{cmtt}{\hyphenchar \font\m@ne}
\DeclareFontShape{T1}{cmtt}{m}{n}{
        <-9>    s*[1.1] ectt0800
        <9-10>  s*[1.1] ectt0900
        <10-12> s*[1.1] ectt1000
        <12-17> s*[1.1] ectt1200
        <17->   s*[1.1] ectt1728
      }{}
\DeclareFontShape{T1}{cmtt}{m}{it}{
        <-9>    s*[1.1] ecit0800
        <9-10>  s*[1.1] ecit0900
        <10-12> s*[1.1] ecit1000
        <12-17> s*[1.1] ecit1200
        <17->   s*[1.1] ecit1728
      }{}
\DeclareFontShape{T1}{cmtt}{m}{sl}{
        <-9>    s*[1.1] ecst0800
        <9-10>  s*[1.1] ecst0900
        <10-12> s*[1.1] ecst1000
        <12-17> s*[1.1] ecst1200
        <17->   s*[1.1] ecst1728
      }{}
\DeclareFontShape{T1}{cmtt}{m}{sc}{
        <-9>    s*[1.1] ectc0800
        <9-10>  s*[1.1] ectc0900
        <10-12> s*[1.1] ectc1000
        <12-17> s*[1.1] ectc1200
        <17->   s*[1.1] ectc1728
      }{}
\DeclareFontShape{T1}{cmtt}{bx}{n}
       {<->sub  cmtt/m/n}{}
\DeclareFontShape{T1}{cmtt}{bx}{it}
       {<->sub  cmtt/m/it}{}
\DeclareFontShape{T1}{cmtt}{bx}{sl}
       {<->sub  cmtt/m/sl}{}
\DeclareFontShape{T1}{cmtt}{b}{n}
       {<->sub  cmtt/m/n}{}
\DeclareFontShape{T1}{cmtt}{b}{it}
       {<->sub  cmtt/m/it}{}
\DeclareFontShape{T1}{cmtt}{b}{sl}
       {<->sub  cmtt/m/sl}{}
\endgroup
\makeatother

\begin{document}
abc\texttt{def}ghi

\fontencoding{OT1}\selectfont
abc\texttt{def}ghi
\end{document}

第二行只是为了显示与原始外观的差异。应用您认为最佳的放大倍数。

在此处输入图片描述

如果您更喜欢坚持使用 OT1,那么您应该使用以下代码:

\makeatletter
\begingroup
\nfss@catcodes
\DeclareFontFamily{OT1}{cmtt}{\hyphenchar \font\m@ne}
\DeclareFontShape{OT1}{cmtt}{m}{n}{
        <-9>    s*[1.1] cmtt8
        <9-10>  s*[1.1] cmtt9
        <10-12> s*[1.1] cmtt10
        <12->   s*[1.1] cmtt12
      }{}
\DeclareFontShape{OT1}{cmtt}{m}{it}{
        <->     s*[1.1] cmitt10
      }{}
\DeclareFontShape{OT1}{cmtt}{m}{sl}{
        <->     s*[1.1] cmsltt10
      }{}
\DeclareFontShape{OT1}{cmtt}{m}{sc}{
        <->     s*[1.1] 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}{}
\endgroup
\makeatother

答案3

如果您愿意更改字体,您可以对 ttfamily 使用可缩放字体。

\documentclass[11pt]{book}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.8]{beramono}

\begin{document}
abc\texttt{def}ghi

\end{document}

在此处输入图片描述

答案4

对内联打字机字体进行快速而肮脏的破解:

\newcommand{\texttx}[1]{{\small \texttt{#1}}}

相关内容