为什么环境 ttfamily 是连字符,但宏 ttfamily 没有连字符?

为什么环境 ttfamily 是连字符,但宏 ttfamily 没有连字符?

我尝试复制以下示例使用 ttfamily 字体进行连字的示例并使用名称ttfamily作为\ttfamily{Some text}\begin{ttfamily}& \end{ttfamily}

但是为什么第一行使用宏ttfamily没有连字符,而第二行使用环境\begin{ttfamily}却有连字符呢?

在此处输入图片描述

\documentclass[10pt,a5paper,twoside]{memoir}
\usepackage{lmodern}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[main=english]{babel}
\usepackage[showframe,pass]{geometry}

%THIS IS THE IMPORTANT PART   % <=========================================
\usepackage{letltxmacro}
\LetLtxMacro\origttfamily\ttfamily
\DeclareRobustCommand*{\ttfamily}{%
  \origttfamily
  \hyphenchar\font=`\-\relax
  \fontdimen3\font=.25em\relax
  \fontdimen4\font=.167em\relax
  \fontdimen7\font=.167em\relax
}

\makeatletter
\DeclareRobustCommand\vttfamily{%
  \not@math@alphabet\vttfamily\relax
  \fontfamily{cmvtt}% cmvtt (Computer Modern) or lmvtt (Latin Modern)
  \selectfont
}
\DeclareTextFontCommand{\textvtt}{\vttfamily}
\makeatother % <=========================================================

\begin{document}

    \frenchspacing
    \ttfamily{Typewriter/teletype family - (encoding: T1, family: pcr, series: m,
    shape: n, size: 10.5, baseline: 11.0pt)}

    \bigskip

    \begin{ttfamily}
        Typewriter/teletype family - (encoding: T1, family: pcr, series: m,
        shape: n, size: 10.5, baseline: 11.0pt)
    \end{ttfamily}

\end{document}

关于这个问题如何使用 `\texttt{Some text}` 进行自动连字?我们可以看到没有连字符的相同文本tttext,它导致Overfull \hbox

在此处输入图片描述

test2.tex:182: Overfull \hbox (11.9992pt too wide) in paragraph at lines 182--183[]\T1/lmtt/m/n/10 Typewriter/teletype family - (encoding: T1, family:
test2.tex:182: Overfull \hbox (33.7491pt too wide) in paragraph at lines 182--183\T1/lmtt/m/n/10 pcr, series: m, shape: n, size: 10.5, baseline: 11.0pt)

答案1

根据你的定义,做

{\ttfamily text}

\begin{ttfamily}text\end{ttfamily}

完全相同。事实上,以下示例的输出完全相同。

\documentclass[10pt,a5paper,twoside]{memoir}
\usepackage{lmodern}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[main=english]{babel}
\usepackage[showframe,pass]{geometry}

\usepackage{letltxmacro}
\LetLtxMacro\origttfamily\ttfamily
\DeclareRobustCommand*{\ttfamily}{%
  \origttfamily
  \hyphenchar\font=`\-\relax
  \fontdimen3\font=.25em\relax
  \fontdimen4\font=.167em\relax
  \fontdimen7\font=.167em\relax
}

\begin{document}

\frenchspacing

{\ttfamily 
supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious}

\bigskip

\begin{ttfamily}
supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
supercalifragilisticexpialidocious supercalifragilisticexpialidocious
\end{ttfamily}

\end{document}

在此处输入图片描述

笔记

  1. \ttfamily{...}是错误的,因为之后字体仍将是打字机类型}

  2. 后面的空格\begin{ttfamily}(准确地说是行尾)会被忽略,因为 TeX 位于段落之间时会扫描它。如果\begin{ttfamily}在段落模式下扫描,则不会扫描它。

  3. 避免使用像这样的伪环境。

相关内容