使用 T1 編碼和 lmodern 時如何在 \texttt 內自動連字符

使用 T1 編碼和 lmodern 時如何在 \texttt 內自動連字符

我正在尋找一種在\texttt.我發現這個答案它看起來很完美:對很多人來說簡單且有用。

我選擇在全域啟用連字符,\texttt並按照那裡的建議,添加了以下行:

\DeclareFontFamily{\encodingdefault}{\ttdefault}{\hyphenchar\font=`\-}

不幸的是,它不起作用並且產生了不良的副作用:內部不再使用打字機字體\texttt,並且在控制台輸出中出現以下錯誤:

LaTeX Font Warning: Some font shapes were not available, defaults substituted.

這個錯誤指出了我的正確方向:我總是在我的 LaTeX 文件中使用 T1 編碼和 lmodern 字體:

\usepackage[T1]{fontenc}
\usepackage{lmodern}

當我註解掉這些行後,一切都工作正常。

有沒有辦法讓原來的答案也適應這種情況?我本來想對該帖子添加評論,但不幸的是我仍然沒有足夠的聲譽來做到這一點。

非常感謝。

答案1

該問題可以透過不同的方式解決,這需要手動載入字體定義檔並在使用該系列的任何字體之前更改連字符:

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

\makeatletter
\input{t1lmtt.fd}
\@namedef{T1+lmtt}{}
\makeatother

\begin{document}
\parbox{0pt}{\ttfamily
  \hspace{0pt}hyphenation in typewriter type 
}
\end{document}

在此輸入影像描述

的職責之一\DeclareFontFamily{enc}{fam}{code}是定義\enc+fam擴展至code。由於 ,控制序列\enc+fam不能直接使用+,因此可以使用\@namedef(即)。\expandafter\def\csname#1\endcsname由於正常的聲明是

\DeclareFontFamily{T1}{lmtt}{\hyphenchar=-1 }

這確實

\@namedef{T1+lmtt}{\hyphenchar=-1 }

我們透過這樣做來抵消它

\@namedef{T1+lmtt}{}

code一旦選擇了與字體系列相關的字體形狀並永久附加到所選字體,就會使用它。因此,在實際選擇任何字體之前進行重新定義非常重要。

答案2

Franks 的答案遺漏了重要的一點:當您在序言中聲明字體系列時,乳膠不再嘗試輸入 .fd 文件,因此所有其他字體聲明都會丟失。您可以在進行更改之前輸入它們:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{lipsum}
\makeatletter
\input{t1lmtt.fd}
\makeatother
\DeclareFontFamily{\encodingdefault}{\ttdefault}{\hyphenchar\font=`\-}


\begin{document}
\ttfamily abc \lipsum
\end{document}

或者:

\ttfamily
\hyphenchar\font=`\-
\DeclareFontFamily{\encodingdefault}{\ttdefault}{\hyphenchar\font=`\-}

相關內容