18

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.)

2

4 Answers 4

8

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

0
7
\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

2
  • 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, 2011 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.
    – user2478
    Jan 20, 2011 at 20:36
3

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

1
  • 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, 2011 at 17:14
2

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}
5
  • how does this deal with the issues that Jørgen Tesman mentions in his answer?
    – Seamus
    Apr 6, 2011 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. Apr 6, 2011 at 13:17
  • give an example where it does not work without \hbox{}
    – user2478
    Apr 6, 2011 at 13:25
  • @Herbert: See my updated example. In the first case the '2001-2002' is not right aligned when the \hbox{} is removed. Apr 6, 2011 at 13:42
  • ok, a misunderstanding. I thought that "on a line on its own" was meant for a \when without additional text.
    – user2478
    Apr 6, 2011 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .