13

I have sometimes very long words, which are for example method names and so on.

Is there any possibility to force LaTeX to push them to a new line, if they overflow the margins?

It is possible to push them into a new line, because I know in my document aren't words which are too long for a line.

Here a mini example which produce the problem:

\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}

Thanks for any advices.

8
  • if this text is representative, i think that \raggedright would be the best solution. otherwise, if hyphenation is suppressed (which would make the technical terms more comprehensible) many lines will be very badly stretched and thus hard to read. Commented Jul 5, 2012 at 18:31
  • Yes this text is representative. I have learnt from my last question. :) But \raggedright doesn't work with all words. Is there a possibility to disable hyphenation for a region? With flushleft I got better results.
    – CSchulz
    Commented Jul 5, 2012 at 18:37
  • \begingroup \hyphenpenalty=10000 \exhyphenpenalty=10000 <de-hyphenated text here> \par \endgroup should satisfactorily isolate the change. if the group doesn't end with a \par (or a blank line), any salutary effects of changing the penalties could be lost for the last paragraph. and yes, \flushleft is better here than \raggedright. Commented Jul 5, 2012 at 19:07
  • @barbarabeeton: I guess there’s no \flushleft it’s an environment {flushleft}. You likely know that but a other user reading this later my don’t …
    – Tobi
    Commented Jul 5, 2012 at 19:12
  • See the \sloppypar suggestion at tex.stackexchange.com/questions/53364/… Commented Jul 5, 2012 at 19:36

3 Answers 3

11

If your text has a lot of these words it's probably best to go with fully unjustified text as in the first answer, but sometimes you have text which would look best justified apart from occasional outcrops of unbreakable words. In these cases you can just use a definition as below which allows lines to break short before such words but otherwise tries to justify both paragraph margins.

enter image description here

\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}

Note, unrelated to the linebreaking issue the standard fonts don't include a bold small caps, so you get a font warning and bold normal text for the first case.

8
  • Thanks for the idea, but I got the same warnings Font shape `OT1/cmr/bx/sc' undefined and Some font shapes were not available, defaults substituted.
    – CSchulz
    Commented Jul 6, 2012 at 6:57
  • Yes That's what I mean. Those warnings come from using \textsc{\textbf{ in your original example as there are no bold small caps fonts in the standard set. They are not related to the line breaking issue. I did not change it as I don't know waht you want to do (change fonts, or have small caps not bold or bold not small caps) Commented Jul 6, 2012 at 8:19
  • Okay thanks I did miss understand your answer.
    – CSchulz
    Commented Jul 6, 2012 at 13:16
  • I face now the problem that words which are too long for one line breaks up the page borders. Can I fix that problem?
    – CSchulz
    Commented Oct 22, 2012 at 21:53
  • 1
    You mean a single word is wider than the page:( you could add hyphenation points by adding \- at suitable places, of if not that what do you want to happen. probably best to edit your question (or ask another) with an example Commented Oct 22, 2012 at 23:47
13

it seems that the text involved is highly technical. under the circumstances, it is probably best to set it flush left and suppress hyphenation entirely for the duration.

this code should do the job for a limited section of the text.

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

if such text is justified, and the population of long technical terms is greater than that of ordinary text, the spacing in the ordinary text becomes very uneven and is consequently harder to comprehend than ragged right text.

sloppypar, as suggested in a comment, justifies the text within the environment. this would work if there are more ordinary words than long ones, and the long ones don't occur in inconvenient locations within the lines. a test with the affected material, trying both techniques side by side, will quickly make it clear which is preferable in a given situation.

2
  • Is there a possibility to use it with full justification?
    – CSchulz
    Commented Jul 26, 2012 at 9:29
  • 1
    @CSchulz -- i presume that by "with full justification" you mean turning off hyphenation but not using flushleft. yes. in that case, change \begin{flushleft} ... \end{flushleft} to \begingroup ... \endgroup, or add the two penalties within sloppypar. the spacing will probably be quite uneven and make the passage hard to read, but it can be done. (i strongly recommend against it though.) test a representative sample with several "wrappers" to see which is most effective in your situation. Commented Jul 26, 2012 at 13:55
5

If anyone stumbles across this and looks for an easy way out, just try adding extra non-breaking whitespaces by adding ~ between the words. Do this when the section your doing is finished and then you can fine tune the spaces.

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

If you added enough whitespaces the long word will be pushed into the next line without enforcing a linebreak which would cut the sentence in half and omit the sentence to be spread over the whole width of the paragraph.

1
  • Thats a good solution I feel like. Thanks!
    – HWilmer
    Commented May 26, 2022 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .