Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to write a simple command that will put some text right-aligned, but bump it to the next line if it doesn't fit well. Since an image is worth a thousand words:

example

The "Normally" case is correct - that's what it should do. But if the line gets too long, I'd like it to bump to the next line, as shown in "Desired". Currently, what I have does "Failure". Here's what I've come up with so far:

\newcommand{\when}[1]{\hfill\mbox{\textit{#1}}}

Note: I had to manually push the "Desired" to the right with a \\ \strut, but I'd like the macro to automate this, and to not have to pick out the cases where I need to do that by hand. (Simply adding \\ \strut to the macro results in the wrong output for the "Normally" case.)

share|improve this question
    
This is nearly exactly what the \signed macro in the TeXbook (chapter 14, p. 106) does... –  cgnieder Jun 11 '12 at 20:42
    
Related/Follow-up Question: Bump right-aligned text to next line iff no room. –  Peter Grill Jan 13 '13 at 7:45
add comment

4 Answers

There are some cases where both Herbert's and Thorsten Donig's answers can be problematic. One situation is when the stuff at the end needs a new line, but that line also starts a new page. This makes an especially awkward widow, since it would start a page flushed right. The other potential problem involves hyphenation. Herbert's solution does not work properly on lines that are started by hyphenated words (it starts a new line even if there is enough space to flush right), and Thorsten Donig's solution seems to break hyphenation of the preceding word.

\documentclass{article}
\usepackage[papersize={10cm,5cm},textwidth=5cm,vmargin=5mm]{geometry}
\usepackage{calc,parskip}
\newcommand{\atend}{\makebox[2em][l]{\leaders\hbox{b}\hfill}}
\newcommand{\fillpage}{\vspace*{\textheight}\vspace*{-2\baselineskip}\vspace{-\parskip}}
\overfullrule=2pt
\pagestyle{empty}

\begin{document}
\fillpage

\newcommand\comfyfill[1]{{% = Herbert's \When
  \leftskip=0ptplus1fil\rightskip=-\leftskip\parfillskip=\leftskip
  \hfill \phantom{ } \textit{\mbox{#1}}\par}}

\makebox[\textwidth-2em]{\hrulefill a}\comfyfill{\atend}

\makebox[\textwidth-\widthof{charac- }]{\hrulefill} characterisation \comfyfill{\atend}

\makebox[\textwidth-\widthof{charac- }]{\hrulefill} charac- terisation \comfyfill{\atend}

\newpage
\fillpage

\renewcommand*{\comfyfill}[1]{% = Thorsten Donig's \signed
  \unskip\hspace*{1em plus 1fill}
  \nolinebreak[3]%
  \hspace*{\fill}\mbox{\emph{#1}}
  \parfillskip0pt\par
}

\makebox[\textwidth-2em]{\hrulefill a}\comfyfill{\atend}

\makebox[\textwidth-\widthof{charac- }]{\hrulefill} characterisation \comfyfill{\atend}

\makebox[\textwidth-\widthof{charac- }]{\hrulefill} charac- terisation \comfyfill{\atend}

\end{document}

Here is the output from Herbert's solution: Herbert's \When

An here is the output from Thorsten Donig's: enter image description here

share|improve this answer
add comment

Your original code almost does it. You only need to add an empty \hbox{} (or shorter \null) at the left to make it work on a line on its own. The \hfill macro doesn't work directly at the beginning of the line.

This works fine with the some example text similar to the one you provided:

\documentclass{article}
\newcommand{\when}[1]{\hbox{}\hfill\mbox{\textit{#1}}}
\begin{document}
\subsubsection*{Headline}
text text text text text text text text text text text text text text text text text \when{2001-2002}
\subsubsection*{Headline}
text text text text text text text text text text text text text \when{2001-2002}
\end{document}
share|improve this answer
    
how does this deal with the issues that Jørgen Tesman mentions in his answer? –  Seamus Apr 6 '11 at 12:59
1  
@Seamus: It doesn't. I'm just trying to answer the original question which asks about a simple macro. I'm realizing that it wont work in all and every case, but it points out what was missing in the OP's original approach. –  Martin Scharrer Apr 6 '11 at 13:17
    
give an example where it does not work without \hbox{} –  Herbert Apr 6 '11 at 13:25
    
@Herbert: See my updated example. In the first case the '2001-2002' is not right aligned when the \hbox{} is removed. –  Martin Scharrer Apr 6 '11 at 13:42
    
ok, a misunderstanding. I thought that "on a line on its own" was meant for a \when without additional text. –  Herbert Apr 6 '11 at 13:46
add comment
\documentclass[a5paper]{article}

\newcommand\When[1]{{%
  \leftskip=0ptplus1fil\rightskip=-\leftskip\parfillskip=\leftskip
  \hfill \phantom{ } \textit{\mbox{#1}}\par}}

\parindent=0pt

\begin{document}
\hrulefill

long long long long long long blub bla bla \When{2002--2010}
long long long long long long blub bla bla blaaah \When{2002--2010}
long long long long long long blub bla bla bla bla blaaaah \When{2002--2010}

long long long long long long blub bla bla bla bla blaaaah 
long long long long long long blub bla bla bla bla blaaaah 

\end{document}

alt text

share|improve this answer
    
Thanks! A lot of those commands are foreign to me (I just did a lot of reading on glues and skips and ahhhh!) - could you mind explain what you're doing there? –  Thanatos Jan 20 '11 at 17:12
    
the problem is the third line where you have a linebreak but without stretching the text to aligned right. The other two examples are no problem. \leftskip=0ptplus1fil means that TeX can insert any space on the left side in a line. This we need for the year if it moved to anothe rline, which is then the last line in a paragraph and filled with \parfillskip in this case the same as \leftskip. –  Herbert Jan 20 '11 at 20:36
add comment

A possible solution could look like this. (I had no other idea for the macro name so I have chosen this one).

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}

\newcommand*{\signed}[1]{%
  \unskip\hspace*{1em plus 1fill}
  \nolinebreak[3]%
  \hspace*{\fill}\mbox{\emph{#1}}
  \parfillskip0pt\par
}

\begin{document}
  The quick brown fox jumps over the lazy dog. \signed{2001--2002}

  With the new command the signature text should be shifted to the new line. \signed{2001--2002}
\end{document}

alt text

share|improve this answer
    
Thanks! A lot of those commands are foreign to me (I just did a lot of reading on glues and skips and ahhhh!) - could you mind explain what you're doing there? –  Thanatos Jan 20 '11 at 17:14
add comment

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.