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

s6hebern
  • 297
  • 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 '20 at 08: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 '20 at 08: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 '20 at 09: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 '20 at 09:08
  • @egreg @TivV \usepackage{microtype} does a better job, but does not fix it entirely (the full stop still overflows). I also want to use \usepackge[none]{hyphenate} to prevent hyphenation and in that case microtype makes it even worse. @egreg Not sure if I understand you point, what is your suggestion to fix this? – s6hebern Feb 21 '20 at 09:16
  • 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. – David Carlisle Feb 21 '20 at 09:18

1 Answers1

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

egreg
  • 1,121,712