TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

enter image description here

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:

enter image description here

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}
share|improve this question
    
up vote 11 down vote accepted

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

enter image description here

\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}% 
}%
}
share|improve this answer
    
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}

enter image description here

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

share|improve this answer

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.

enter image description here

\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}
share|improve this answer

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.

share|improve this answer

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}
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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