![结果](https://linux22.com/image/318572/%E7%BB%93%E6%9E%9C.png)
答案1
monotype 字体的换行是一个难题。下面有五个选项(实际上很少,因为其中一些会产生不可接受的结果);我建议您使用listings
包而不是仅仅\ttfamily
(顺便说一句,\tt
它已被弃用,不应再使用)来编写您的列表。我加载了 showframe
文本区域的视觉指南。
\documentclass{article}
\usepackage{listings}
\usepackage{underscore}
\usepackage{showframe}
\lstset{
basicstyle=\ttfamily\small,
columns=fullflexible,
breaklines=true
}
\begin{document}
The method {\ttfamily set_current_state(current_state)} is synonimous to {\ttfamily set_task_state(current_state)}
The method \lstinline|set_current_state(current_state)| is synonimous to \lstinline|set_task_state(current_state)|
{\sloppy
The method \lstinline|set_current_state(current_state)| is synonimous to \lstinline|set_task_state(current_state)|\par}
{\sloppy
The method {\ttfamily set_current_state(current_state)} is synonimous to {\ttfamily set_task_state(current_state)}\par}
{\raggedright
The method \lstinline|set_current_state(current_state)| is synonimous to \lstinline|set_task_state(current_state)|\par}
{\raggedright
The method {\ttfamily set_current_state(current_state)} is synonimous to {\ttfamily set_task_state(current_state)}\par}
{{\ttfamily\hyphenchar\the\font=`\-}%
The method {\ttfamily set_current_state(current_state)} is synonimous to {\ttfamily set_task_state(current_state)}\par}
\end{document}
结果
一些评论
使用linebreaks=true
启用换行点;然而,在这种情况下,逐字文本仍然突出。
使用sloppy
会在单词之间留下可怕的巨大间隙。
使用\raggedright
在这里会给出更好且可以接受的结果。
答案2
我建议结合两种方法:允许在_
字符处设置断点,并在需要时允许更大的空间。第一种方法是通过设置_
为活动(使用\tth
代替\tt
)来实现,第二种方法需要将\emergencystretch
寄存器设置为适当的值:
{\catcode`\_=13 \gdef_{\string_\-}}
\def\tth{\tt\catcode`\_=13 }
\emergencystretch=3em
\hsize=14cm % for example
The method {\tth set_current_state(current_state)} is synonimous to
{\tth set_task_state(current_state)}.
\bye
您可能正在使用纯 TeX(恭喜),因为 LaTeX 用户讨厌\tt
。因此,我的示例是纯 TeX。