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 have a simple text. Right after the end of the text I use \hfill to get some words right aligned.

 \documentclass[11pt]{article}

    \begin{document}
        Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est
        \hfill
        \textbf{some words}
    \end{document}

The problem is now that when the text ends at the end of the line, the words after \hfill won't be right aligned anymore.

\documentclass[11pt]{article}

\begin{document}
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
    \hfill
    \textbf{some words}
\end{document}

How can I achieve the words to be right aligned even if the text fits the line?

share|improve this question
    
In the second case the \hfill might be part of the previous line, not the some words line. Adding \hspace*{0pt} before \hfill helps, but I'm not 100% sure why exactly. I would also place \textbf{some words} inside a \mbox{...} to avoid line breaks there. However, this might not catch every possibility. Ulrike's answer taken from The TeXBook seems to do this. –  Martin Scharrer Apr 21 '11 at 14:07
2  
Ulrike's answer is good. here's the reason: spaces at the beginning of a line are ignored by TeX. when you have a line that ends at the right margin, the \hfill becomes space at the beginning of the next line, and disappears -- no flush right. there are several ways to get around this problem. the \hspace*{0pt} forces that space to not disappear, and the \hfill overwhelms the space normally inserted at the end of a paragraph. the TeXbook exercise takes another approach, forcing the paragraph to end flush right, so only \hfil is needed. –  barbara beeton Apr 21 '11 at 14:37
add comment

2 Answers

up vote 12 down vote accepted

That's an exercise in the TeXbook.

\documentclass[11pt]{article}
\newcommand\quelle[1]{{%
      \unskip\nobreak\hfil\penalty50
      \hskip2em\hbox{}\nobreak\hfil\textbf{#1}%
      \parfillskip=0pt \finalhyphendemerits=0 \par}}

\begin{document}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
    \hfill
    \textbf{some words}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
\quelle{some words}


\end{document}
share|improve this answer
    
great, this works for me. Thanks! –  kraeki Apr 23 '11 at 12:46
    
exactly what I needed... –  umar Dec 14 '11 at 12:17
add comment

It seems rather hackish. You could use something like

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod 
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.   
At vero eos et accusam et justo duo dolores et ea rebum. \mbox{}~\hfill \textbf{some~words}

Or maybe just use rightflush?

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