[Tex/LaTex] LaTeX doesn’t break lines with capitalized words to the end properly

line-breaking

I have a text like:

Lorem ipsum dolor sit amet, consetetur BRANDNAME sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.

And LaTeX breaks the lines like

Lorem ipsum dolor sit amet, consetetur BRANDNAME  
sadipscing elitr, sed diam nonumy eirmod tem-  
por  invidunt ut  labore et dolore magna ali-  
quyam erat, sed diam voluptua. At vero eos et  
accusam et justo duo dolores et ea rebum.

Instead I would it expect to do something like:

Lorem   ipsum   dolor   sit   amet,  consetetur  
BRANDNAME sadipscing elitr, sed diam nonumy ei-  
rmod tempor  invidunt ut labore et dolore magna  
aliquyam erat,  sed diam voluptua.  At vero eos  
et accusam et justo duo dolores et ea rebum.

I have no idea how to break latin syllables, but I hope the problem got obvious. The name has to be completely capitalized and not wrapped, but the layout should also not be destroyed. I'm using PdfLaTeX as compiler and the Harvard style. This problem only affects completely capitaized words at the end of a line. I'm a bit surprised that I didn't find anyone else encountering the same problem earlier…

Best Answer

The optimal way to handle this would be use a macro (for convenience and consistency) and adjust \emergencystretch to suit your needs:

enter image description here

\documentclass{article}
\usepackage{pgffor}
\pagestyle{empty}
\usepackage[paper=a6paper,paperheight=.25\paperheight]{geometry}

\newlength{\BRANDNAMElen}
\settowidth{\BRANDNAMElen}{BRANDNAME}
\setlength{\emergencystretch}{1.1\BRANDNAMElen}% \emergencystretch is slightly longer than \BRANDNAMElen
\newcommand{\BRANDNAME}{\mbox{BRANDNAME}}

\begin{document}

\setlength{\emergencystretch}{5em}
\foreach \x in {\unskip,s,si,sit,sit{ },
  sit a,sit am,sit ame,sit amet,sit amet{ },
  sit amet c,sit amet co,sit amet con,sit amet cons,
  sit amet conse,sit amet conset,sit amet consete,
  sit amet consetet,sit amet consetetu,sit amet consetetur,
  sit amet consetetur{ },sit amet consetetur l,sit amet consetetur lo,
  sit amet consetetur lor,sit amet consetetur lore,sit amet consetetur lorem,
  sit amet consetetur lorem{ },sit amet consetetur lorem i,sit amet consetetur lorem ip,
  sit amet consetetur lorem ips,sit amet consetetur lorem ipsu,sit amet consetetur lorem ipsum,
  sit amet consetetur lorem ipsum,sit amet consetetur lorem ipsum,sit amet consetetur lorem ipsum,sit amet consetetur lorem ipsum,sit amet consetetur lorem ipsum,sit amet consetetur lorem ipsum,sit amet consetetur lorem ipsum,sit amet consetetur lorem ipsum,sit amet consetetur lorem ipsum,sit amet consetetur lorem ipsum} {
\clearpage
Lorem ipsum dolor \x{} \BRANDNAME{}
sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut  labore et dolore magna aliquyam erat, 
sed diam voluptua. At vero eos et accusam et justo 
duo dolores et ea rebum.
}
\end{document}

The above macro inserts letters around \BRANDNAME (set as an unbreakable \mbox) to identify whether the break would flush it to the following line. \emergencystretch is set to 10% more of the width of \BRANDNAME to allow for the line-breaking algorithm to check a third time during paragraph setting whether adjustments should be made.

You may have to reset \emergencystretch to 0pt when you don't need it.

During paragraph assembly, TeX attempts (perhaps multiple times) to lay out the lines in an optimal way.

Read TeX by Topic, specifically section 19.2 The process of breaking (p 179). Here is an excerpt:

19.2.1 Three passes

First an attempt is made to split the paragraph into lines without hyphenating, that is, without inserting discretionary hyphens. This attempt succeeds if none of the lines has a badness exceeding \pretolerance.

Otherwise, a second pass is made, inserting discretionaries and using \tolerance. If \pretolerance is negative, the first pass is omitted.

TeX can be made to make a third pass if the first and second pass fail. If \emergencystretch is a positive dimension, TeX will assume this much extra stretchability in each line when badness and demerits are calculated. Thus solutions that only slightly exceeded the given tolerances will now become feasible. However, no glue of size \emergencystretch is actually present, so underfull box messages may still occur.