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
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.\usepackage{microtype}
, it should do its best to 'fix' such lines. (btw you don't need to loadfontenc
when you're usingfontspec
)\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.