# Bump right-aligned text to next line iff no room

I a chat discussion it was suggested that \hspace*{\fill} (with a space before it to allow for a line break) was sufficient to always get the text following aligned to the right. And indeed, it is aligned to the right.

However, in one case it is inserting an additional line break where one should not be required as illustrated by the last example here. I want the text bumped to the next line if and only if (iff ) there is no room:

If I remove the space before the \hspace*{\fill}, there are cases (second and third) where a new line should have been inserted but is not:

## Related Question:

I have tried the suggestions at the related question and:

• Martin Scharrer's suggestion of using \hbox{}\hfill results in the text being on the left for the last case.
• Herbert's \When macro exhibits a similar problem for the last case.
• Thorsten Donig's \signed macro does yield the correct number of lines, but portions of the file name for the middle two cases show up on the second line when they easily fit in the first line.

## Notes:

I believe that this is unrelated to the problem, as I was able to reproduce the problem without it. But, am mentioning it here in case it causes an issue with the proposed solution.

I am using \texttt for the file name. This resulted in an additional complication which I initially thought was the cause of the problem. So, my actual use case is commented out below and it incorporates a solution from

## Code:

\documentclass{article}
\usepackage[paperwidth=7.0cm,showframe]{geometry}
\usepackage{xcolor}
\usepackage{xparse}

\newcommand*{\FillLine}[2]{%
\noindent\parbox{\linewidth}{%
\raggedright
File:~#2 % <-- Need space here
%Space before \hspace* allows for a break before it
\hspace*{\fill}\texttt{\small CONFIDENTIAL}%
}%
}

%\newcommand*{\EnableHyphenationInTexttt}{\hyphenchar\font=45\relax}
%
%\newcommand*{\FillLine}[2]{%
%    \noindent\parbox{\linewidth}{%
%       \raggedright
%       File:~\texttt{\EnableHyphenationInTexttt#2} % <-- Need space here
%       %Space before \hspace* allows for a break before it
%       \hspace*{\fill}\texttt{\small CONFIDENTIAL}%
%    }%
%}

\begin{document}
\FillLine{Name}{Value}

\bigskip
\FillLine{Name}{A-Long-Value}

\bigskip
\FillLine{Name}{iiiiiii-iiiii-iiiiiii}

\bigskip
\FillLine{Name}{Some-Really-Really-Long-Value}
\end{document}

-

You can encourage TeX not to break the lines with \linepenalty

\newcommand*{\FillLine}[2]{%
\noindent\parbox{\linewidth}{%
\rightskip\fill\parfillskip-\rightskip
\linepenalty100
\exhyphenpenalty0
File:~#2\linebreak[0] % <-- Need space here
%Space before \hspace* allows for a break before it
\hspace*{\fill}\texttt{\small CONFIDENTIAL}%
}%
}

-
Been using this for a while now and seems to be working great. – Peter Grill Jan 22 '13 at 1:00

This is known in some inner circles as the Bourbaki problem. Here's an adaptation to your needs:

\documentclass{article}
\usepackage[paperwidth=7.0cm,showframe]{geometry}

\newcommand*{\FillLine}[2]{%
\noindent\parbox{\linewidth}{%
\raggedright
File:~#2%
{\nobreak\hfill\penalty50\hskip1em\null\nobreak
\hfill\texttt{\small CONFIDENTIAL}%
\parfillskip=0pt \finalhyphendemerits=0 \par}
}% End of \parbox
}

\begin{document}
\FillLine{Name}{Value}

\bigskip
\FillLine{Name}{A-Long-Value}

\bigskip
\FillLine{Name}{A-Long-Valu}

\bigskip
\FillLine{Name}{A-Long-Val}

\bigskip
\FillLine{Name}{A-Long-Va}

\bigskip
\FillLine{Name}{A-Long-V}

\bigskip
\FillLine{Name}{iiiiiii-iiiii-iiiiiii}

\bigskip
\FillLine{Name}{Some-Really-Really-Long-Value}
\end{document}


How does this work? Look for "Bourbaki" in the source file of the TeXbook.

-

I guess your friend is \looseness=-1: http://www.tug.org/utilities/plain/cseq.html#looseness-rp

It doesn't provide ideal result (the break in the last example gets modified), but it seems to work quite well.

\documentclass{article}
\usepackage[paperwidth=7.0cm,showframe]{geometry}
\usepackage{xcolor}
\usepackage{xparse}

\newcommand*{\FillLine}[2]{%
\noindent\parbox{\linewidth}{%
\raggedright
\parfillskip-\rightskip
\looseness=-1
File:~#2\ % <-- Need space here
%Space before \hspace* allows for a break before it
\hspace*{0pt plus 1fil}%
\texttt{\small CONFIDENTIAL}%
}%
}

%\newcommand*{\EnableHyphenationInTexttt}{\hyphenchar\font=45\relax}
%
%\newcommand*{\FillLine}[2]{%
%    \noindent\parbox{\linewidth}{%
%       \raggedright
%       File:~\texttt{\EnableHyphenationInTexttt#2} % <-- Need space here
%       %Space before \hspace* allows for a break before it
%       \hspace*{\fill}\texttt{\small CONFIDENTIAL}%
%    }%
%}

\begin{document}
\FillLine{Name}{Value}

\bigskip
\FillLine{Name}{A-Long-Value}

\bigskip
\FillLine{Name}{iiiiiii-iiiii-iiiiiii}

\bigskip
\FillLine{Name}{Some-Really-Really-Long-Value}
\end{document}

-

Here's a trick from this question that does what you want. Put the text you want right aligned in \signed{CONFIDENTIAL}.

% Make author name appear right.
\def\signed #1{{\leavevmode\unskip\nobreak\hfil\penalty50\hskip2em
\hbox{}\nobreak\hfil#1%
\parfillskip=0pt \finalhyphendemerits=0 \endgraf}}


Disclaimer: I don't understand how this works.

-

This was dealt with by Knuth in The TeXbook p. 106. A LaTeX version is (after removing any typos):

\newcommand*{\atright}[1]{{%
\unskip\nobreak\hfil\penalty50
\hskip2em\hbox{}\nobreak\hfil#1
\parfillskip=0pt\finalhyphendemerits=0\par}}


and use \atright{short text} at the end of the paragraph.

The 2007 Glisterings column http://tug.org/TUGboat/tb28-2/tb89glister.pdf in TUGboat gives code for all sorts of special paragraph shapes including this one.

Here is a slightly modified version of your MWE which seems to meet your requirements.

\documentclass{article}
\usepackage[paperwidth=7.0cm,showframe]{geometry}
\newcommand*{\atright}[1]{{%
\unskip\nobreak\hfil\penalty50
\hskip2em\hbox{}\nobreak\hfil#1
\parfillskip=0pt\finalhyphendemerits=0\par}}

\newcommand*{\FillLine}[2]{%  a revised version
\noindent\parbox{\linewidth}{%
% \raggedright
File:-#2 % need space here
\atright{\texttt{\small CONFIDENTIAL}}}}

\begin{document}
\FillLine{Name}{Value}

\bigskip
\FillLine{Name}{A-Long-Value}

\bigskip
\FillLine{Name}{iiiiiii-iiiii-iiiiiii}

\bigskip
\FillLine{Name}{Some-Really-Long-Value}

\end{document}

-
Looks interesting, but it doesn't work for me when I attempt to use it the MWE provided in the Question. Can you adapt it to the MWE and post the full code so I can cut and paste to try it out. – Peter Grill Mar 13 at 2:08
@Peter Grill. I've added an example to my original answer. – Peter Wilson Mar 17 at 20:49
Please actually compile the code that you post. This has several errors and does not compile: hskp2em is not a known macro, You are missing a backslash in \newcommand*{FillLine}, the first call to FillLine is missing a backslash. Also, I don't like the 2em space, would prefer TeX normal spacing -- But that is just a preference thing. – Peter Grill Mar 18 at 9:14
@Peter Grill. I have made the corrections. I do not know how to insert a *.tex file into StackExchange. I LaTeXed my MWE and it worked but I then rewrote in in StackExchange and made some transcription errors. Did you try it making the corrections you noted? – Peter Wilson Mar 18 at 19:06
Works now. You can use copy and paste .tex code. – Peter Grill Mar 18 at 23:45