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.

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.

share|improve this question
    
Also, you have an spurious space in } %vbox, which would result in your text not quite being flush with the right margin. –  Peter Grill May 22 '12 at 0:41
add comment

2 Answers

up vote 7 down vote accepted

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}

share|improve this answer
    
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? –  Roman Cheplyaka May 22 '12 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. –  Peter Grill May 22 '12 at 21:43
    
I created a new question for that: tex.stackexchange.com/questions/57004/… –  Roman Cheplyaka May 23 '12 at 4:00
add comment

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.

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.