1

I'm struggling with line breaks in my document and it seems to me that LaTeX is randomly deciding when to break correctly and when not. See the differences in the three blocks below.

\documentclass[11pt, oneside, a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{fontspec}
\setmainfont[BoldFont=Calibri, ItalicFont=CalibriLightItalic]{Calibri Light}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage[normalem]{ulem}
\newcommand{\eolyemph}[1]{\textcolor{blue}{\emph{#1}}}
\usepackage[hidelinks]{hyperref}

\begin{document}
\textbf{\underline{This is too long:}}

Code was written with the help of \href{https://tex.stackexchange.com}{\uline{tex.stackexchange.com}} (all hail \emph{StackExchange}). There, many friendly people from the community are helping each other out on all sorts of computer-related problems.

\textbf{\underline{But this is fine:}}

Code was written with the help of \href{https://tex.stackexchange.com}{\uline{tex.stackexchange.com}} (all hail \emph{TexStackExchange}). There, many friendly people from the community are helping each other out on all sorts of computer-related problems.

\textbf{\underline{This works, too:}}

This document was written with the help of \href{https://tex.stackexchange.com}{\uline{tex.stackexchange.com}} (all hail \emph{StackExchange}). There, many friendly people from the community are helping each other out on all sorts of computer-related problems.

\end{document}

Why does it sometimes break fine and sometimes not?


Edit: added screenshot

6
  • 1
    Yes a screenshot would be helpful (since not everyone necessarily has Calibri installed). It's weird, when I try your code with Calibri I see your problem, but with the standard font it goes away.
    – TivV
    Feb 21, 2020 at 8:51
  • Added a screenshot, thanks for the hint. Indeed, with standard font it seems to work. Even more confusing to me, to be honest.
    – s6hebern
    Feb 21, 2020 at 8:58
  • 4
    I can see nothing strange. According to its loaded patterns for English, TeX is only able to hyphenate Stack-Ex-change. In the first case, choosing either hyphenation point would produce a paragraph with badness exceeding the tolerance, so TeX produces an imperfect one for you to fix it.
    – egreg
    Feb 21, 2020 at 9:03
  • @s6hebern Given what egreg said, you could try using \usepackage{microtype}, it should do its best to 'fix' such lines. (btw you don't need to load fontenc when you're using fontspec)
    – TivV
    Feb 21, 2020 at 9:08
  • 3
    if you disable hyphenation then you should disable justification as well and set things raggedright or (worse) use \sloppy to allow white space to stretch much more. Constraining white space not to stretch and specifying justified paragraphs and disabling hyphenation is setting unachievable constraints. Feb 21, 2020 at 9:18

1 Answer 1

2

There is nothing strange, actually. If I try \showhyphens{StackExchange} the answer is

Stack-Ex-change

Those are the only points TeX is willing to break a line at. In your first attempt, breaking after x would produce a line where interword spaces are stretched a lot. This doesn't happen in the other two cases. Here's a slightly modified version of your code (I don't own Calibri Light, but adding one millimeter to \textwidth faithfully reproduces your issue) where the effect of breaking the line after x.

\documentclass[11pt, oneside, a4paper]{article}

\usepackage{fontspec}
\usepackage[normalem]{ulem}
\usepackage{hyperref}

\setmainfont{Calibri}

\addtolength{\textwidth}{0.1cm}

\begin{document}
\textbf{\underline{This is overfull:}}

Code was written with the help of 
\href{https://tex.stackexchange.com}{\uline{tex.stackexchange.com}} 
(all hail \emph{StackExchange}). There, many friendly people from the community 
are helping each other out on all sorts of computer-related problems.

\textbf{\underline{This is how it would be:}}

Code was written with the help of 
\href{https://tex.stackexchange.com}{\uline{tex.stackexchange.com}} 
(all hail \emph{StackEx-\linebreak change}). There, many friendly people from the community 
are helping each other out on all sorts of computer-related problems.

\textbf{\underline{But this is fine:}}

Code was written with the help of 
\href{https://tex.stackexchange.com}{\uline{tex.stackexchange.com}} 
(all hail \emph{TexStackExchange}). There, many friendly people from the community 
are helping each other out on all sorts of computer-related problems.

\textbf{\underline{This works, too:}}

This document was written with the help of 
\href{https://tex.stackexchange.com}{\uline{tex.stackexchange.com}} 
(all hail \emph{StackExchange}). There, many friendly people from the community 
are helping each other out on all sorts of computer-related problems.

\end{document}

enter image description here

Instead of producing a very bad line, TeX prefers to output an overfull one, warning you it did, so you can examine the offending paragraph and reword it to make it fit.

However, adding microtype can be very helpful.

\documentclass[11pt, oneside, a4paper]{article}

\usepackage{fontspec}
\usepackage[normalem]{ulem}
\usepackage{microtype}
\usepackage{hyperref}

\setmainfont{Calibri}

\addtolength{\textwidth}{0.1cm}

\begin{document}
\textbf{\underline{This is acceptable:}}

Code was written with the help of 
\href{https://tex.stackexchange.com}{\uline{tex.stackexchange.com}} 
(all hail \emph{StackExchange}). There, many friendly people from the community 
are helping each other out on all sorts of computer-related problems.


\microtypesetup{protrusion=false,expansion=false}

\textbf{\underline{This is how it would be without microtype:}}

Code was written with the help of 
\href{https://tex.stackexchange.com}{\uline{tex.stackexchange.com}} 
(all hail \emph{StackEx-\linebreak change}). There, many friendly people from the community 
are helping each other out on all sorts of computer-related problems.

\end{document}

enter image description here

2
  • I see what microtype does here, thanks for pointing out the issue. However, I think I'll still go with \sloppy, because it produces exactly what I want (move the whole word to the next line and stretch the rest out to \textwidth), although this might be "bad style". Thanks to @David Carlisle for that.
    – s6hebern
    Feb 21, 2020 at 9:41
  • 1
    @s6hebern -- Limit the scope of \sloppy by using the environment sloppypar. See How to avoid using \sloppy document-wide to fix overfull \hbox problems? for more information about avoiding overfull lines.. Feb 22, 2020 at 0:40

You must log in to answer this question.

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