如何在 \texttt 环境中全局更改字体大小?

如何在 \texttt 环境中全局更改字体大小?

如何在 \texttt 环境中全局更改字体大小?

我可以使用以下命令对 verbatim 环境执行此操作

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

我如何在 \texttt 环境中完成同样的事情?

答案1

您可以使用fancyvrb提供许多其他定制的包(您也可以检查fvextra添加了许多功能的包)。

\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}

在此处输入图片描述

为了改变等宽字体的大小,有一个“卑鄙”的技巧。修改定义中的值以\ttscale满足您的需要。

\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}

在此处输入图片描述

与原文对比:

在此处输入图片描述

答案2

字体切换通常强壮的。您可以更新\ttfamily字体开关以将大小调整元素附加到其中,方法与对 进行的操作相同\verbatimfont

在此处输入图片描述

\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}

相关内容