2

I know that latex sometimes allows overfull hboxes as an alternative to having to stretch beyond the stretch settings or as an alternative to having ragged right sides.

However, in my specific case, LaTeX sometimes permits overflows that should really not be acceptable. I have provided a minimal reproducing example below.

To be clear, I am not looking for a specific solution to this case. I know I can use \sloppy, \raggedright or some combination of the emergencystretch and tolerance settings. I'm looking for a general solution to tell latex that overflows longer than X is unacceptable in any case. I want LaTeX to automatically find a better solution if the overflow would be bigger than X. To me, a ragged right side would be perfectly acceptable as an alternative to such big overflows.

What are the settings to tweak in LaTeX to prevent these problems?

The reproducing example:

\documentclass{article}
\usepackage{listings}

\newcommand{\lstinlinestyle}{\ttfamily} \newcommand\codeinline{\lstinline[basicstyle={\lstinlinestyle}]}

\begin{document} Lorem Ipsum is simply dummy text of the printing and typesetting \codeinline{Textthatcannotbehyphenated}. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. \end{document}

enter image description here

I would prefer the output to look like this without manual intervention:

enter image description here

Edit: Do I have to care about bad boxes? does not answer my question, because it only mentions \sloppy and \emergencystretch as the solutions to the problem. This does not always resolve the problem because sometimes the amount of stretch necessary to create a good line break will result in a strange-looking paragraph. In such cases a ragged right side will look much better.

Edit2: While testing the solution, I noticed that I use \texttt and that it overflows as well. Here is a reproducing example:

\documentclass{article}

\begin{document} Lorem Ipsum is simply dummy text of the printing and typesetting \texttt{Textthatcannotbehyphenated}. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. \end{document}

So, I'm still looking for a more general solution than the one provided in the current answer.

Edit3: This question is much related to what I want to do as well: Wrap line instead of overfull hbox? but it was never answered to satisfaction.

Edit 5: No satisfactory solution has been found yet. If you have any ideas, please let me know! I'm considering to make a LuaLaTeX package that uses raggedright as a fallback in case a paragraph has a certain badness score.

2 Answers2

4

Some notes on the general case at

Do I have to care about bad boxes?

But for this case you can define your code command to have a ragged space that adds up to 0pt so has no effect if a line break is not taken.

enter image description here

\documentclass{article}
\usepackage{listings}

\newcommand{\lstinlinestyle}{\ttfamily} \newcommand\codeinline{% \hspace{0pt plus 3cm}\penalty100\hspace{0pt plus -3cm}% \lstinline[basicstyle={\lstinlinestyle}]}

\begin{document} Lorem Ipsum is simply dummy text of the printing and typesetting \codeinline{Textthatcannotbehyphenated}. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled \codeinline{Textthatcannotbehyphenated} it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. \end{document}

David Carlisle
  • 757,742
  • Thank you for your answer! Can you explain in a bit more detail (or refer to documentation) why your trick with \hspace\penalty\hspace works? Also, for the general case, I understand that I can set \emergencystretch to allow LaTeX to stretch lines more. Is there also something like \emergencyragged to allow LaTeX to make the right side ragged if a certain penalty is then prevented? – Jan-Jaap Korpershoek Feb 09 '23 at 12:28
  • @Jan-JaapKorpershoek Knuth describes this in the texbook, but if a break occurs at the (mildly discouraged) penalty100 there is up to 3cm of whit space to fill the line, the following hspace is discarded as space is always discarded after a line break. If no break happens the spaces cancel out and have no effect. there are probably loads of answers on this site using this but finding them may be hard:-) – David Carlisle Feb 09 '23 at 12:41
  • Thank you for the explanation! While I tested your proposed solution, I noticed that the same problem occurs for \texttt, so the search continues.. I edited my question. Of course I could do the same trick for texttt, but that feels like a bit of a hacky solution, instead of a generic solution to the problem. – Jan-Jaap Korpershoek Feb 09 '23 at 12:53
  • don't use texttt, use \mymoresemanticname and define \mymoresemanticname as \hspace{0pt plus 3cm}\penalty100\hspace{0pt plus -3cm}\texttt – David Carlisle Feb 09 '23 at 12:56
  • I implemented your proposed solution in Edit 4 of my question. It does not resolve all cases yet. In addition, it is still not a general solution. I can imagine that there are many other unbreakable boxes, and redefining them all upfront is sometimes not possible (because other users might use my template for arbitrary purposes and I don't want to break it for them) – Jan-Jaap Korpershoek Feb 10 '23 at 09:08
  • @Jan-JaapKorpershoek really you should not edit solutions in to the question it messes up the site format. I only allowed 3cm if you want to allow more use \textwidth (but then you may want the same after – David Carlisle Feb 10 '23 at 09:52
  • I removed Edit 4. Thank you for the feedback. – Jan-Jaap Korpershoek Feb 10 '23 at 15:34
  • I set the 3cm to \textwidth, this results in the following: https://www.latex4technics.com/?note=zzvv7x, note that the next word now overflows. I also tried repeating the same trick after texttt, which results in the following: https://www.latex4technics.com/?note=zzvv7w. You see that the line break is now before the full stop at the end of the sentence, which is also not desirable. – Jan-Jaap Korpershoek Feb 10 '23 at 16:04
4

If you don't mind use of LuaLaTeX, you can use my linebreaker package. It hooks into the linebreaking callback, detects overflow paragraphs, and re-typesets them with increased values of tolerance and emergencystretch, until it finds a good solution or until it reaches the maximum allowed number of cycles.

Your sample with the linebraker loaded:

\documentclass{article}
\usepackage{listings}
\usepackage{linebreaker}

\newcommand{\lstinlinestyle}{\ttfamily} \newcommand\codeinline{\lstinline[basicstyle={\lstinlinestyle}]}

\begin{document} Lorem Ipsum is simply dummy text of the printing and typesetting \codeinline{Textthatcannotbehyphenated}. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. \end{document}

This is the result:

enter image description here

michal.h21
  • 50,697
  • I'm using LuaLatex already, so that's perfectly fine! One drawback of your method is that it will increase the interline spacing, which can sometimes look quite ugly https://imgur.com/zbuAzlz). Or sometimes it cannot find a solution at all: https://imgur.com/0M96QWq. I think some sort of a fallback to \raggedright would be a nice solution. – Jan-Jaap Korpershoek Feb 10 '23 at 08:56
  • @Jan-JaapKorpershoek you can change the maximal spacing with maxemergencystretch, the default value is 3em, which is quite large. I think in such cases, a manual solution is needed. – michal.h21 Feb 10 '23 at 09:17
  • Good to know! It's exactly these tricky cases that I want to find an automatic/general solution for. – Jan-Jaap Korpershoek Feb 10 '23 at 15:32