8

The following code does what I need:

\hbox to \textwidth{\hfill \vbox{ \hbox{some text} \hbox{some other text} } %vbox } %hbox 

The important things here are:

  1. Lines are not wrapped
  2. Lines are aligned by their left side
  3. The block as a whole is pushed to the right margin
  4. The width of the block is not specified and is calculated based on the longest line in the block

Can I achieve the same using simpler or higher-level tools? In particular, I don't like the need to wrap each line in the block in an \hbox.

1
  • Also, you have an spurious space in } %vbox, which would result in your text not quite being flush with the right margin. Commented May 22, 2012 at 0:41

2 Answers 2

7

tabular Solution:

You can also use a simple tabular environment as shown below. This yields:

enter image description here

Notes:

  • The @{} is used to eliminate the column spacing on the right hand side to ensure that the content is flushed with the margin, and to eliminate the column spacing on the left so that you have as much room as possible for the text.

  • The showframe package is used to show that the text is aligned to the right.

Code:

\documentclass{article} \usepackage{showframe} \begin{document} \hfill \begin{tabular}{l@{}} some text\\ some other text \end{tabular} \end{document} 

varwidth Solution:

Alternatively, you could use the varwidth environment. This yields identical results as above.

Notes:

  • Even though the varwidth environment requires a length, it takes on the natural width of the included content.

Code:

\documentclass{article} \usepackage{showframe} \usepackage{varwidth} \begin{document} \hfill \begin{varwidth}{\linewidth} some text\\ some other text \end{varwidth} \end{document} 

3
  • Thanks Peter. Initially I tried something very similar to your tabular solution, but used flushleft instead of tabular. That didn't work. Could you explain the difference between tabular and flushleft in this regard? Commented May 22, 2012 at 20:44
  • Well, \flushleft will make the content left aligned, but since you need the entire environment on the right side of the page, it needs to be wrapped in something to move it over. Perhaps if you add a MWE to the question with this attempt it might be easier to comment on it. Commented May 22, 2012 at 21:43
  • I created a new question for that: tex.stackexchange.com/questions/57004/… Commented May 23, 2012 at 4:00
0

To throw in a ConTeXt solution:

\starttext \rightaligned{% \framed [align=right, frame=off]{% some text\\ some other text}} \stoptext 

The \framed command takes care of the two typeset lines and the \rightaligned of their horizontal position.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.