将长单词推入新行

将长单词推入新行

我有时会有一些很长的单词,例如方法名称等等。

如果它们溢出了边距,是否有可能强制 LaTeX 将它们推到新行?

可以将它们推入新行,因为我知道我的文档中没有一行太长的单词。

下面是一个产生问题的小例子:

\documentclass{scrartcl}

\begin{document}
\section{Test}
Finally there is a simple solution using \textsc{\textbf{XMLResource.OPTION\_RECORD\_UNKNOWN\_FEATURE}} option. And the
text must go on \ldots.
\par
And another example the show must go on, but we have too less text (\textbf{createUnspecifiedNodeWarningMarker} and
\textbf{createUnspecifiedNodeErrorMarker}, sdjklashjksa \textbf{createUnspecifiedLinkWarningMarker} and
\textbf{createUnspecifiedLinkErrorMarker}).
\end{document}

谢谢您的任何建议。

答案1

如果您的文本中有很多这样的单词,那么最好使用完全不对齐的文本,就像第一个答案中那样,但有时除了偶尔出现的不可断开的单词外,您的文本看起来最好是对齐的。在这些情况下,您可以像下面这样使用定义,允许行在这些单词之前断开,但否则会尝试对齐两个段落边距。

在此处输入图片描述

\documentclass{scrartcl}

\newenvironment{foo}
{\par
\hyphenpenalty=10000
\exhyphenpenalty=10000
}
{\par}

\newcommand\lword[1]{\leavevmode\nobreak\hskip0pt plus\linewidth\penalty50\hskip0pt plus-\linewidth\nobreak\textbf{#1}}
\begin{document}
\section{Test}
\begin{foo}
Finally there is a simple solution using \textsc{\lword{XMLResource.OPTION\_RECORD\_UNKNOWN\_FEATURE}} option. And the
text must go on \ldots.

And another example the show must go on, but we have too less text (\lword{createUnspecifiedNodeWarningMarker} and
\lword{createUnspecifiedNodeErrorMarker}, sdjklashjksa \lword{createUnspecifiedLinkWarningMarker} and
\lword{createUnspecifiedLinkErrorMarker}).
More text, more text. More text, more text. More text, more text.
More text, more text. More text, more text. More text, more text.
More text, more text. More text, more text. More text, more text.
\end{foo}
\end{document}

请注意,与换行问题无关,标准字体不包含粗体小型大写字母,因此在第一种情况下,您会收到字体警告和粗体普通文本。

答案2

所涉及的文本似乎具有很高的技术性。在这种情况下,最好将其设置为左对齐,并在持续时间内完全抑制连字符。

此代码应能完成文本的有限部分的工作。

\begin{flushleft}
\hyphenpenalty=10000
\exhyphenpenalty=10000
 <de-hyphenated text here>
\end{flushleft}

如果此类文本是两端对齐的,并且长技术术语的数量多于普通文本,则普通文本中的间距会变得非常不均匀,因此比右边不对齐的文本更难理解。

sloppypar正如评论中所建议的那样,在环境中调整文本。如果普通单词比长单词多,并且长单词不会出现在行内的不方便位置,则此方法会起作用。使用受影响的材料进行测试,同时尝试两种技术,将很快清楚地知道在特定情况下哪种技术更可取。

答案3

如果有人偶然发现这个问题并想找到一种简单的解决方法,只需尝试在单词之间添加 ~ 来添加额外的不间断空格。当您完成的部分完成后执行此操作,然后您可以微调空格。

Finally~ there~ is~ a~ simple~ solution~ using~ \textsc{\textbf{XMLResource.OPTION\_RECORD\_UNKNOWN\_FEATURE}} option. And the text must go on \ldots.

如果添加了足够多的空格,长单词将被推到下一行而不会强制换行,这会将句子切成两半,并省略分布在段落整个宽度上的句子。

相关内容