\raggedright behavior with minipage and tabbing

In the following 3 examples a minipage with a nested tabbing environment is used to show several aligned lines of text. (This is part of a larger example.)

Output of pdflatex :

The first example is centered due to the center environment around it and shows the expected behavior. I do not understand the next two examples, however:

1. Why is the second example flushed left just because text is added before the tabbing?
2. Why does the \raggedright have to be placed before the minipage (as it does not have any effect when placed inside the minipage but before the tabbing)?

\documentclass{article}

\begin{document}

\begin{center}

Example 1: Centered due to missing text:\\

\begin{minipage}[t][1.5cm][t]{\textwidth}
\raggedright % Has no influence
\begin{tabbing}
\hspace{2cm} \= \hspace{5cm} \kill
AAAA: \> test\\
BBBBBBBB: \> test\\
\end{tabbing}
\end{minipage}

{\raggedright Example 2: Flushed left by adding text:\\}%

\begin{minipage}[t][2cm][t]{\textwidth}
\begin{tabbing}
\hspace{2cm} \= \hspace{5cm} \kill
AAAA: \> test\\
BBBBBBBB: \> test\\
\end{tabbing}
\end{minipage}

{\raggedright Example 3: Flushed left without text by setting the minipage to \textbackslash raggedright:\\}

\raggedright
\begin{minipage}[t][1.5cm][t]{\textwidth}
\begin{tabbing}
\hspace{2cm} \= \hspace{5cm} \kill
AAAA: \> test\\
BBBBBBBB: \> test\\
\end{tabbing}
\end{minipage}
\end{center}

\end{document}

-
–  Claudio Fiandrino Feb 5 '13 at 14:04
A minipage is set to the width of the tabbing inside only if tabbing is the only contents of the minipage. –  egreg Feb 5 '13 at 16:16

A minipage or parbox results to have the width of an inner tabbing environment only if that tabbing is the only thing inside the minipage.

\begin{minipage}{\textwidth}
\begin{tabbing}
\hspace{2cm} \= \hspace{5cm} \kill
AAAA: \> test\\
BBBBBBBB: \> test\\
\end{tabbing}
\end{minipage}


The width of the minipage will be \textwidth. If instead you say

\begin{minipage}{\textwidth}
\begin{tabbing}
\hspace{2cm} \= \hspace{5cm} \kill
AAAA: \> test\\
BBBBBBBB: \> test\\
\end{tabbing}
\end{minipage}


then the result will be what you want (possibly with space missing). You can use the varwidth environment of the package with the same name:

\documentclass{article}
\usepackage{varwidth}
\begin{document}

\begin{center}
Example of text just to see that the centering will be correct

\begin{varwidth}{\textwidth}
\begin{tabbing}
\hspace{2cm} \= \hspace{5cm} \kill
AAAA: \> test\\
BBBBBBBB: \> test\\
\end{tabbing}
\end{varwidth}
\end{center}
\end{document}


-
Thanks for the explanation. I still wonder why a tabbing environment 'overwrites' the width of the minipage. For me this is highly unintuitive and I can not find a reason why it behaves in this way. Could you perhaps point me to the relevant documentation? –  Thomas Auzinger Feb 7 '13 at 11:52
It's a feature present in LaTeX since the beginning (at least of version 2). It's very useful for packing a tabbing environment so it can be placed in a "nonstandard" way with the help of minipage or \parbox (for instance to frame it). –  egreg Feb 7 '13 at 12:10
Thanks for the help! –  Thomas Auzinger Feb 14 '13 at 12:12

The problem is that you're setting the minipage width to \textwidth and so the minipage is getting set to the width of the page. You can see this if you frame it in a minipage. If you know your longest line, then you can set the width of the minipage to the width of the longest line.

Using calc you can write:

  \begin{minipage}[t][2cm][t]{\widthof{ BBBBBBBB: test}}