4

I try to use the command outlined by David Carlisle here: Flush what's following \hfill to the next line if it doesn't fit

But in this MWE it doesn't work:

\documentclass[fontsize=10pt]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[babel]{microtype}
\usepackage[textwidth=10cm]{geometry}

\def\foo{\hspace{\fill}\mbox{}\linebreak[0]\hspace*{\fill}}

\begin{document}
Schöne Verhältnis herzustellen, in
               welchem sich erst das eigentliche Talent zeigt, bleibt abzuwarten.\foo\mbox{H. B.}      
\end{document}

The name of the author is moved to the next line even though there is enough space: enter image description here

Why?

David Carlisle
  • 757,742

2 Answers2

5

You can try a bit harder to discourage a line break at that point:

\documentclass[fontsize=10pt]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[babel]{microtype}
\usepackage[textwidth=10cm]{geometry}

\def\foo{\hspace{\fill}\mbox{}\nolinebreak[3]\hspace*{\fill}}

\begin{document}
Schöne Verhältnis herzustellen, in
               welchem sich erst das eigentliche Talent zeigt, bleibt abzuwarten.\foo\mbox{H. B.}      
\end{document}

The reason TeX is taking three lines is because it penalises a hyphen on the penultimate line. If you leave the linebreak penalty as 0 but lower the final hyphen demerit to 50 by adding

\finalhyphendemerits=50

it also just takes two lines.

David Carlisle
  • 757,742
4

The problem is the hyphenation in the previous line. LaTeX tries normally to avoid that there is hyphenation is the line before the last. The penalty for this is \finalhyphendemerits. That's why the code from the TeX-Book (see link below) sets it locally to 0 (and also \parfillskip to 0). If you set it too zero it compiles as expected:

\documentclass[fontsize=10pt]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[babel]{microtype}
\usepackage[textwidth=10cm]{geometry}

\def\foo{\hspace{\fill}\mbox{}\linebreak[0]\hspace*{\fill}}

\begin{document}\finalhyphendemerits=0

Schöne Verhältnis herzustellen, in
               welchem sich erst das eigentliche Talent zeigt, bleibt abzuwarten.\foo\mbox{H. B.}
\end{document}

For a safer implementation see here https://tex.stackexchange.com/a/16333/2388.

Ulrike Fischer
  • 327,261
  • it works! I'll try the safer implementation right away. but would a general approach work as well: \def\foo{\finalhyphendemerits=0\hspace{\fill}\mbox{}\linebreak[0]\hspace*{\fill}} also work or would that produce another problem? – Martin Mueller May 05 '17 at 15:17
  • @MartinMueller there is no grouping so that would set the demerits for the rest of the document unless the paragraph was in some other group – David Carlisle May 05 '17 at 15:19
  • You are changing \finalhyphendemerits globally, so it will affect following paragraphs. – Ulrike Fischer May 05 '17 at 15:19