# Justify at right margin or in next line

119 views

### npl...@gmail.com

Aug 21, 2006, 6:08:06 PM8/21/06
to
I want to justify some words at the end of a paragraph on the right
margin. For example, if I write:

Here is a line \toright{xxx}

I should get

|Here is a line xxx|

(where the | denote the margins).

But if there is not enough space left of the line, the argument of
\toright should go on the next line, again on the right margin:

|Here is a deliberately longer line |
| xxx|

How should the command \toright{} be defined to do all the above?
The first part would be too easy, (some \hfill would do it), the second
part is beyond my (limited) knowledge of TeX/LaTeX.

Thanks in advance for any help.

### David Kastrup

Aug 21, 2006, 6:15:56 PM8/21/06
to
npl...@gmail.com writes:

\hspace{\fill}\linebreak[0]\hspace*{\fill}

--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
UKTUG FAQ: <URL:http://www.tex.ac.uk/cgi-bin/texfaq2html>

### Jean-Côme Charpentier

Aug 21, 2006, 8:49:44 PM8/21/06
to
David Kastrup a écrit :

> npl...@gmail.com writes:
>
>
>>I want to justify some words at the end of a paragraph on the right
>>margin. For example, if I write:
>>
>>Here is a line \toright{xxx}
>>
>>I should get
>>
>>|Here is a line xxx|
>>
>>(where the | denote the margins).
>>
>>But if there is not enough space left of the line, the argument of
>>\toright should go on the next line, again on the right margin:
>>
>>|Here is a deliberately longer line |
>>| xxx|
>>
>>How should the command \toright{} be defined to do all the above?
>>The first part would be too easy, (some \hfill would do it), the second
>>part is beyond my (limited) knowledge of TeX/LaTeX.
>
>
> \hspace{\fill}\linebreak[0]\hspace*{\fill}

Your solution doesn't work in any case. You don't take acount about the
width of "xxx". Or did I forgotten something?

\documentclass{article}

\newcommand\toright[1]{%
\hspace{\fill}%
\linebreak[0]%
\hspace*{\fill}\mbox{#1}%
}

\newlength{\lright}
\newcommand\toRight[1]{%
\settowidth{\lright}{#1}%
\hspace*{\lright}%
\linebreak[0]%
\hspace{-\lright}%
\hspace*{\fill}\mbox{#1}%
}
\begin{document}
\begin{center}
\verb+\toright+ solution
\end{center}

Hello \toright{to right}

A line with some material to fill it. Place just only for a very short
text \toright{x}

A line with some material to fill it. Place just only for a very short
text \toright{x and another x}

A line with some material to fill it. No
place for a long text \toright{and this is my long text}

\vspace{1cm}
\begin{center}
\verb+\toRight+ solution
\end{center}

Hello \toRight{to right}

A line with some material to fill it. Place just only for a very short
text \toRight{x}

A line with some material to fill it. Place just only for a very short
text \toright{x and another x}

A line with some material to fill it. No
place for a long text \toRight{and this is my long text}

\end{document}

Jean-Côme Charpentier

### Brooks Moses

Aug 21, 2006, 9:32:09 PM8/21/06
to
Jean-Côme Charpentier wrote:
> David Kastrup a écrit :
>>npl...@gmail.com writes:
>>>How should the command \toright{} be defined to do all the above?
>>>The first part would be too easy, (some \hfill would do it), the second
>>>part is beyond my (limited) knowledge of TeX/LaTeX.
>>
>>\hspace{\fill}\linebreak[0]\hspace*{\fill}
>
> Your solution doesn't work in any case. You don't take acount about the
> width of "xxx". Or did I forgotten something?

You've forgotten something -- TeX takes care of the width of xxx in its
line-break routine. If none of this were here, TeX would break the line
before the "xxx" if it's too long to fit on the current line; in the
case of a break the hspaces just keep the last line of text from being
full-justified, and shove the "xxx" to the right.

The \hspace{\fill} before the breakpoint just means that TeX will
consider it reasonable to break the line there no matter how short it is
-- or, at least, it would mean that if it were \hspace*{fill} as it
should be. Correct that trivial bug, and your sample document will work
perfectly.

- Brooks

--
The "bmoses-nospam" address is valid; no unmunging needed.

### Brooks Moses

Aug 21, 2006, 9:33:47 PM8/21/06
to
Brooks Moses wrote:
> The \hspace{\fill} before the breakpoint just means that TeX will
> consider it reasonable to break the line there no matter how short it is
> -- or, at least, it would mean that if it were \hspace*{fill} as it
> should be. Correct that trivial bug, and your sample document will work
> perfectly.

Er, \hspace*{\fill}. Please correct the bug of omitting the *, but do
not introduce the bug of omitting the \ before "fill"!

### Donald Arseneau

Aug 21, 2006, 11:05:52 PM8/21/06
to
Brooks Moses <bmoses...@cits1.stanford.edu> writes:

> Jean-Côme Charpentier wrote:
> > David Kastrup a écrit :

> >>\hspace{\fill}\linebreak[0]\hspace*{\fill}
>

> The \hspace{\fill} before the breakpoint just means that TeX will consider it
> reasonable to break the line there no matter how short it is -- or, at least,
> it would mean that if it were \hspace*{fill} as it should be.

No, it means it is a reasonable breakpoint as \hspace. If it
were \hspace* TeX would NOT consider it as a possible breakpoint.
There's not much difference either way because the ensuing penalty
is zero.

\hspace*{\fill}\nolinebreak[1]\hspace*{\fill}

which will occasionally fit onto one line by compressing the interword
spaces.

--
Donald Arseneau as...@triumf.ca

### Dirk Schlimm

Aug 22, 2006, 12:05:16 AM8/22/06
to
npl...@gmail.com wrote:

> How should the command \toright{} be defined to do all the above?
> The first part would be too easy, (some \hfill would do it), the second
> part is beyond my (limited) knowledge of TeX/LaTeX.

When I had this problem, I found an answer in the TeXBook, p. 106
(after Exercise 14.30).

This is the slightly modified code, since Knuth's example has two
parameters (he also gives a paragraph of explanations):

\def\signedone #1 {{\unskip\nobreak\hfil\penalty50
\hskip2em\hbox{}\nobreak\hfil#1
\parfillskip=0pt \finalhyphendemerits=0 \par}}

- Dirk

### Brooks Moses

Aug 22, 2006, 12:37:27 AM8/22/06
to
Donald Arseneau wrote:
> Brooks Moses <bmoses...@cits1.stanford.edu> writes:
>>Jean-Côme Charpentier wrote:
>>>David Kastrup a écrit :
>>>>\hspace{\fill}\linebreak[0]\hspace*{\fill}
>>
>>The \hspace{\fill} before the breakpoint just means that TeX will consider it
>>reasonable to break the line there no matter how short it is -- or, at least,
>>it would mean that if it were \hspace*{fill} as it should be.
>
> No, it means it is a reasonable breakpoint as \hspace. If it
> were \hspace* TeX would NOT consider it as a possible breakpoint.
> There's not much difference either way because the ensuing penalty
> is zero.

I wonder if perhaps we're talking past each other (entirely possible; I
don't think I was very clear) or if I'm misunderstanding deeper.

What I mean is that

\this \hspace{\fill}\linebreak[0]\hspace*{\fill} \that

where \this is 3/4 of a line long and \that is 1/2 of a line long, will
result in TeX not deciding to break the line at some point between \this
and \that, instead placing everything on one line (and complaining of an
overfull hbox). At least, that's what happened when I tried it.

I presume the reason for this is that when the line is potentially
broken, the \hspace{\fill} would be at the end of the line and thus
discarded, and so \this by itself is too short to make a valid line. Is
that not correct?

In any case, when I changed the \hspace{\fill} to \hspace*{\fill} and
reran TeX, it then broke the line to put \this and \that on separate
lines, with \fill-sized space on the right and left ends of the lines
respectively.

### Donald Arseneau

Aug 22, 2006, 1:01:23 PM8/22/06
to
Brooks Moses <bmoses...@cits1.stanford.edu> writes:

> \this \hspace{\fill}\linebreak[0]\hspace*{\fill} \that
>
> where \this is 3/4 of a line long and \that is 1/2 of a line long, will
> result in TeX not deciding to break the line at some point between \this and
> \that, instead placing everything on one line (and complaining of an overfull
> hbox). At least, that's what happened when I tried it.

It did? Hmmm....

> I presume the reason for this is that when the line is potentially broken,
> the \hspace{\fill} would be at the end of the line and thus discarded,

The presumption is wrong. Discards happen after a breakpoint, but not
before; thus the need for undiscardable \hspace* after the penaly.

So this is buggy LaTeX in the \(no)linebreak macros, specifically in
\@no@lnbk, which incants:

\@tempskipa \lastskip \unskip
\penalty[...]
\ifdim \@tempskipa >\z@ \hskip \@tempskipa \ignorespaces \fi

This is buggy by design -- it shouldn't be trying to move spaces to after
the breakpoint, and by implementation -- it accidentally removes fill glue
entirely because such has no "natural size".

How did \hspace* survive this removal? It isn't the the protection against
discards (a \vrule) because that only protects the front end, not attack from
the rear. Instead \hspace* inserts TWO spacings -- the requested value
followed by a skip of zero. Why does plain \hspace not do the same?
\vspace does the same for either version.

--
Donald Arseneau as...@triumf.ca

### Donald Arseneau

Aug 22, 2006, 1:03:47 PM8/22/06
to
Brooks Moses <bmoses...@cits1.stanford.edu> writes:

> \this \hspace{\fill}\linebreak[0]\hspace*{\fill} \that
>
> where \this is 3/4 of a line long and \that is 1/2 of a line long, will
> result in TeX not deciding to break the line at some point between \this and
> \that, instead placing everything on one line (and complaining of an overfull
> hbox). At least, that's what happened when I tried it.

It did? Hmmm....

> I presume the reason for this is that when the line is potentially broken,
> the \hspace{\fill} would be at the end of the line and thus discarded,

The presumption is wrong. Discards happen after a breakpoint, but not

before; thus the need for undiscardable \hspace* after the penaly.

So this is buggy LaTeX in the \(no)linebreak macros, specifically in
\@no@lnbk, which incants:

\@tempskipa \lastskip \unskip
\penalty[...]
\ifdim \@tempskipa >\z@ \hskip \@tempskipa \ignorespaces \fi

This is buggy by design -- it shouldn't be trying to move spaces to after
the breakpoint, and by implementation -- it accidentally removes fill glue
entirely because such has no "natural size".

How did \hspace* survive this removal? It isn't the the protection against
discards (a \vrule) because that only protects the front end, not attack from
the rear. Instead \hspace* inserts TWO spacings -- the requested value
followed by a skip of zero. Why does plain \hspace not do the same?
\vspace does the same for either version.

It will work to replace the \linebreak[0] by \allowbreak.

--
Donald Arseneau as...@triumf.ca

### Nikos Platis

Aug 22, 2006, 1:09:40 PM8/22/06
to

I tried them all in the document I am working on, in which the
\toright{} command I was requesting is applied several times; therefore
I had the opportunity to watch them all in action and compare them in
many situations. Below I report my observations, as I believe they may
be of interest to the contributors of this thread.

The best performer was (no surprise here?!) Knuth's solution as reported
by Dirk Schlimm:

> \def\signedone #1 {{\unskip\nobreak\hfil\penalty50
> \hskip2em\hbox{}\nobreak\hfil#1
> \parfillskip=0pt \finalhyphendemerits=0 \par}}

It worked perfectly in every situation, and it seems to leave a little
space to the left of the text pushed at the right margin, which is
equally desirable (in case the space left on the line is almost equal to
the length of the text push right).

The solution of Jean-Côme Charpentier

> \newlength{\lright}
> \newcommand\toRight[1]{%
> \settowidth{\lright}{#1}%
> \hspace*{\lright}%
> \linebreak[0]%
> \hspace{-\lright}%
> \hspace*{\fill}\mbox{#1}%
> }

also performed very well. The only shortcoming that I observed was that
sometimes, when the text 'on the right' had to go to the next line, the
last line of the text 'on the left' was stretched to almost the length
of the line, even though it probably should not: in a plain article
compare the following paragraph with both definitions of \toright{}

=======================================================
Here is a sample line. Here is a sample line. Here is a sample line.
\toright{My name}
=======================================================

The other solutions by David Castrup (as corrected by Brooks Moses) and
by Donald Arseneau

> \newcommand{\toright}[1]{\hspace*{\fill}\linebreak[0]\hspace*{\fill}\mbox{#1}}

> \newcommand{\toright}[1]{\hspace*{\fill}\nolinebreak[1]\hspace*{\fill}\mbox{#1}}

gave identical results for my document. The only shortcoming here was
that the text 'on the right' was sometimes moved to the next line even
though there was plenty of space on the last line of the paragraph. This
is not easy to replicate, so I am sending below a minimal document that
demonstrates it.

Many thanks,
Nikos Platis

=======================================================
\documentclass[a4paper,12pt]{article}

\usepackage{times}

\usepackage{geometry}
\geometry{
lmargin=2.8cm,
rmargin=2.2cm
}

\usepackage{enumitem}
\setenumerate[1]{align=right,leftmargin=*,widest=00}

%\def\toright #1 {{\unskip\nobreak\hfil\penalty50
% \hskip2em\hbox{}\nobreak\hfil#1
% \parfillskip=0pt \finalhyphendemerits=0 \par}}

\newcommand{\toright}[1]{\hspace*{\fill}\linebreak[0]\hspace*{\fill}\mbox{#1}}

\begin{document}

\begin{enumerate}
\item
\begin{enumerate}
\item
Describe how multi-tasking may be used within the system, with
reference to a specific example.\toright{3 points}
\end{enumerate}
\end{enumerate}

\end{document}
=======================================================