# Align minipage on right side

I was thinking that this is a small matter, but I can't seem to get my head around this.

I want to create a legend, which sits on the right side of the page. I created a minipage environment around the environment that I want to have on my right side and using \begin{flushright}. However, I can't add a title inside this minipage environment so that it gets displayed on top of the surrounded environment.

The MWE is just an example. I don't use a table in my case. So how do align my title right above the other environment, in this case the table?

MWE:

\documentclass[paper=a4,landscape]{scrartcl}
\usepackage{longtable}

\begin{document}

\begin{minipage}[t]{0.95\textwidth}
Some Title.
\begin{flushright}
\begin{longtable}{ll}
Bla & Bla \\
Blubb & Blubb
\end{longtable}

\end{flushright}
\end{minipage}

\end{document}

-

I, in fact, would use a tabular environment for the title and the text; for the text, a p{<length>} column (and @{} to remove the extra space); inside the tabular, the title can be centered using \multicolumn:

\documentclass{scrartcl}
\usepackage{showframe}
\usepackage{lipsum}

\begin{document}

\hfill\begin{tabular}{@{}p{.5\linewidth}@{}}
\multicolumn{1}{@{}c@{}}{Some title} \\
\lipsum[4]
\end{tabular}

\end{document}


If this structure will be used several times, an environment can be defined; in the following example, the newly defined myenv environment has one mandatory argument (the title) and an optional argument (the width used to typeset the text with a default value of 0.5\linewidth):

\documentclass{scrartcl}
\usepackage{showframe}
\usepackage{lipsum}

\newenvironment{myenv}[2][.5\linewidth]
{\par\hfill\tabular{@{}p{#1}@{}}
\multicolumn{1}{@{}c@{}}{#2} \\ }
{\endtabular\par}

\begin{document}

\begin{myenv}{Some title}
\lipsum*[4]
\end{myenv}

\begin{myenv}[.8\linewidth]{Some title}
\lipsum*[4]
\end{myenv}

\end{document}


If the title spans more than one line, one could load the array package in the preamble:

\usepackage{array}


and then say

\newenvironment{myenv}[2][.5\linewidth]
{\par\hfill\tabular{@{}p{#1}@{}}
\multicolumn{1}{@{}>{\centering}p{#1}@{}}{#2} \\ }
{\endtabular\par}

-

You can use the varwidth environment from the varwidth package to create a box around the natural width of the content and use \hfill:

## Notes:

• I used the the showframe package package to show the margins (and you can see that the table is aligned to the right margin).
• The first \hfill ensures that the varwidth environment is aligned with the right margin.
• The subsequent \hfill are used to ensure that the title is centered.
• The @{} was added to eliminate the column padding to the right of the last column in the table.

## Code:

\documentclass{scrartcl}
\usepackage[paperwidth=9.0cm,showframe]{geometry}
\usepackage{varwidth}
\usepackage{longtable}

\begin{document}
\hfill
\begin{varwidth}{\linewidth}
\hfill\null Some Title.\hfill\null
\begin{longtable}{ll@{}}
Bla & Bla \\
Blubb & Blubb
\end{longtable}
\end{varwidth}
\end{document}

-
Hmm. But the title is still not above the longtable environment (?). –  cherrung Jun 20 '12 at 0:27
If you want the title above just insert a blank line after the title. Not sure if that is what you are looking for. –  Peter Grill Jun 20 '12 at 0:31
If I add a linebreak after some title it is not on the right side anymore. It jumps to the left. I want to output that Gonzalo Medina's solution outputs. –  cherrung Jun 20 '12 at 0:37
@cherrung: Sorry, I misunderstood that requirement -- have updated the solution to correct the placement of the title. –  Peter Grill Jun 20 '12 at 6:23