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?

  • Seems to be caused by babel. In particular, by the ngerman option. Change ngerman to english to see the effect. – Steven B. Segletes May 5 '17 at 14:53
  • The posted code does not make the image shown (eg the accented letters fail as inputenc isn't used), and HB comes on line 2. – David Carlisle May 5 '17 at 14:55
  • 1
    @StevenB.Segletes the difference is that german adds a hyphen on the first line and there is a relatively high cost to a paragraph with a hyphen on the penultimate line. – David Carlisle May 5 '17 at 15:06
  • @david-carlisle you're right, I use xelatex and there the output is as described. but i'll check the proposed solutions now. – Martin Mueller May 5 '17 at 15:13
  • If you use xelatex you really should not be using \usepackage[T1]{fontenc} – David Carlisle May 5 '17 at 15:14
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.

  • 1
    Can you comment on \linebreak[0] versus \nolinebreak[3]? What is going on inside TeX at that point? – Steven B. Segletes May 5 '17 at 15:04
  • @StevenB.Segletes I was adding more text as you were commenting – David Carlisle May 5 '17 at 15:05
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.

  • 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 5 '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 5 '17 at 15:19
  • You are changing \finalhyphendemerits globally, so it will affect following paragraphs. – Ulrike Fischer May 5 '17 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.