# Problem with \hfill and hyphenation

When I use \hfill and in the previous line the last word is hyphenated, the \hfill doesn't work the same way as usual:

\documentclass[a4paper, 12pt]{book}
\begin{document}

Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but phonetic spellings are exceptions   \hfill go to library

AA Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but phonetic spellings  \hfill go to library

\end{document}


What should I write to get \hfill work like the first \hfill in the example?

-
Welcome to TeX.SX! Your problem has nothing to do with hyphenation. Is tex.stackexchange.com/questions/91548/… helping? –  egreg Dec 29 '13 at 17:34
alternatively to @egreg's link, you can try to reduce the occurrences of hyphenation by using the microtype package. In your mwe it works. –  ArTourter Dec 29 '13 at 17:42
Thank you both for your answers. I write mostly in greek, so I use the babel package and the microtype package does't reduce the occurrences of hyphenation, I don't know why. The link you gave me is not helping with hypernation. I found today this tex.stackexchange.com/questions/9325/… and @Jørgen Tesman describes the same problem with hypernation in his answer. –  Bozelou M. Dec 30 '13 at 18:47

The first solution is a modification of my solution to Bump right-aligned text to next line iff no room

I show also David's solution, in order to see where it can fail.

\documentclass[a4paper, 12pt,draft]{book} % draft for showing the overfull boxes

\newcommand*{\gotoend}[1]{%
\unskip
{\nobreak\hfill\penalty50\ \null\nobreak
\hfill\mbox{#1}%
\parfillskip=0pt \finalhyphendemerits=0 \par}
}

%% David's version
\newcommand*{\Dgotoend}[1]{\unskip\hspace*{\fill}\mbox{ #1}}

\begin{document}

Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but phonetic spellings are exceptions \gotoend{go to library}

AA Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but phonetic spellings \gotoend{go to library}

AA Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but AA phonetic spellings are just bigger
exceptions \gotoend{go to library}

David's version:

Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but phonetic spellings are exceptions \Dgotoend{go to library}

AA Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but phonetic spellings \Dgotoend{go to library}

AA Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but AA phonetic spellings are just bigger exceptions \Dgotoend{go to library}

\end{document}


-
I'll give you a vote but I updated my solution a bit:-) –  David Carlisle May 29 at 22:31
I prefer the “Bourbaki” solution by Knuth. ;-) –  egreg May 29 at 22:33
This works!!! Thank you!!! –  Bozelou M. Jun 4 at 20:51

The behaviour isn't really related to hypphenation, but I think you just want to avoid the final phrase breaking over a line so:

\documentclass[a4paper, 12pt]{book}
\begin{document}

Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but phonetic spellings are exceptions\hspace*{\fill}\mbox{ go to library}

AA Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but phonetic spellings\hspace*{\fill}\mbox{ go to library}

\end{document}


You could make a macro out of this (and address egreg's point) by

\documentclass[a4paper, 12pt,draft]{book} % draft for showing the overfull boxes

%% David's version
\newcommand*{\Dgotoend}[1]{\unskip\hfill\penalty9999\hspace*{\fill}\mbox{ #1}}

\begin{document}

David's version:

Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but phonetic spellings are exceptions \Dgotoend{go to library}

AA Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but phonetic spellings \Dgotoend{go to library}

AA Spellings attempt to transcribe the sounds of the language into
alphabetic letters, but AA phonetic spellings are just bigger exceptions \Dgotoend{go to library}

\end{document}


-