texttt 没有在现有连字符处换行

texttt 没有在现有连字符处换行

我有较长的短语,它们之间用连字符分隔,我想用打字机字体排版。我希望 Latex 能断行,避免它们溢出:

\documentclass{article}
\begin{document}

    Normal font breaks the line:

    a-very-long-text-in-typewriter-font-separated-by-hyphens-that-should-break-the-line.

    But typewriter font does not:

    \texttt{a-very-long-text-in-typewriter-font-separated-by-hyphens-that-should-break-the-line.}
\end{document}

需要明确的是,我不希望乳胶添加更多的连字符,我只希望它在现有的连字符处断线。

答案1

在此处输入图片描述

\documentclass{article}
\begin{document}

\raggedright
    Normal font breaks the line:

    a-very-long-text-in-typewriter-font-separated-by-hyphens-that-should-break-the-line.

    But typewriter font does not:

    \texttt{%
     \hyphenchar\font=`\- % enable hyphenation
     \hyphenpenalty=10000 % disable it again
     \exhyphenpenalty=-50 % enable (encourage) it at explicit hyphens
     a-very-long-text-in-typewriter-font-separated-by-hyphens-that-should-break-the-line.}
\end{document}

相关内容