[Tex/LaTex] Align minipage on right side

horizontal alignmentminipage

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}

Best Answer

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}

enter image description here

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}

enter image description here

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}
Related Question