7

Problem

I am trying to align minipages on the top but to no avail, I thought that the [t] option is exactly meant for that, but apparently not. The [c] option seems to work better, except for the first minipage which contains yet another box.

What am I doing wrong?

Update

Thanks for the great answer so far. I should mention that a solution should also respect my geometry setting. Taking @egreg's solution for instance:

\documentclass{article}
\usepackage{xcolor}
\usepackage[a4paper,margin=0pt]{geometry}
\begin{document}
\noindent%
\begin{minipage}[t][.1\textheight]{.1\linewidth}
  \vspace{-\fboxsep}
  \colorbox{gray}{%
    \makebox[\dimexpr \linewidth - 2\fboxsep \relax]{%
      \color{white}%
      \huge S%
    }}%%
\end{minipage}%
\colorbox{orange}{%
\begin{minipage}[t][.1\textheight]{.1\linewidth}
    \vspace{0pt}
    \huge A
\end{minipage}}
\end{document}

You will see that the minipages are aligned, but are offset to the page border. Any remedy for that?

enter image description here

Reprex

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\noindent%
\begin{minipage}[c][.1\textheight][t]{.1\linewidth}%
    \colorbox{blue}{%
        \makebox[\dimexpr \linewidth - 2\fboxsep \relax]{%
            \color{white}%
            \huge S
    }}%%
\end{minipage}%
\colorbox{gray}{%
\begin{minipage}[c][.1\textheight][t]{.1\linewidth}%
    \huge A
\end{minipage}}%
\colorbox{yellow}{%
\begin{minipage}[c][.1\textheight][t]{.1\linewidth}%
    a
\end{minipage}}%
\colorbox{orange}{%
\begin{minipage}[c][.1\textheight][t]{.1\linewidth}%
    A
\end{minipage}}\\[5cm]
\noindent%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
    \colorbox{blue}{%
        \makebox[\dimexpr \linewidth - 2\fboxsep \relax]{%
            \color{white}%
            \huge S
    }}%%
\end{minipage}%
\colorbox{gray}{%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
    \huge A
\end{minipage}}%
\colorbox{yellow}{%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
    a
\end{minipage}}%
\colorbox{orange}{%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
    A
\end{minipage}}
\end{document}

Unwanted Result

enter image description here

3 Answers 3

5

The top alignment is with respect to the first item in the minipage. If it's a line of text, it will refer to the baseline. However, you can use an invisible item, for instance \vspace{0pt} to force alignment at the “very top”.

However, for the minipage containing a \colorbox, you also need to take into account the \fboxsep.

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

\begin{minipage}[t][.1\textheight]{.1\linewidth}
  \vspace{-\fboxsep}
  \colorbox{blue}{%
    \makebox[\dimexpr \linewidth - 2\fboxsep \relax]{%
      \color{white}%
      \huge S%
    }}%%
\end{minipage}%
\colorbox{gray}{%
\begin{minipage}[t][.1\textheight]{.1\linewidth}
    \vspace{0pt}
    \huge A
\end{minipage}}%
\colorbox{yellow}{%
\begin{minipage}[t][.1\textheight]{.1\linewidth}%
    \vspace{0pt}
    a
\end{minipage}}%
\colorbox{orange}{%
\begin{minipage}[t][.1\textheight]{.1\linewidth}%
    \vspace{0pt}
    A
\end{minipage}}

\end{document}

enter image description here

As the two contrasting colors can make an optical illusion of nonalignment, I also provide a magnified picture.

enter image description here

5
  • I love this solution. A small nuisance then though when removing the margins, see update of my question.
    – thothal
    Commented Feb 25, 2022 at 22:16
  • 1
    @thothal You need \vspace*{-\topskip} before the minipages.
    – egreg
    Commented Feb 25, 2022 at 22:29
  • I treid that but it did not work :(
    – thothal
    Commented Feb 25, 2022 at 22:35
  • 1
    @thothal \nointerlineskip\vspace*{-\topskip}
    – egreg
    Commented Feb 25, 2022 at 22:49
  • Great! That's it - thanks
    – thothal
    Commented Feb 25, 2022 at 22:58
7

The notion of [t]op alignment relies on metrics associated with the font size in use. By changing the font size for each minipage, you mess up the ability for this to occur.

The best I can do is use a single font size setting (in this case \huge) for the complete line and then change the font size within each minipage. However, even this won't work as desired, because the colorbox will only extend to the top of each character (plus \fboxsep). Thus, you will see that I add a \strut to each minipage upon entry (which will be set in the current \huge size) followed by the change to the new font size.

The net effect of this is that the colored boxes will be top aligned, but the text in each box will share the same baseline. This may or may not satisfy your need.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\noindent\huge%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
    \colorbox{blue}{%
        \makebox[\dimexpr \linewidth - 2\fboxsep \relax]{%
            \color{white}%
            \strut S
    }}%%
\end{minipage}%
\colorbox{gray}{%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
     \strut A
\end{minipage}}%
\colorbox{yellow}{%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
    \strut\normalsize a
\end{minipage}}%
\colorbox{orange}{%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
    \strut\normalsize A
\end{minipage}}
\end{document}

enter image description here

If I remove each of the 4 \struts, the baselines will remain aligned, but the top of each colorbox will conform to the tallest element in each box (+ \fboxsep).

SUPPLEMENT

Now, having said that it can't be done, I will proceed to do it a different way using the \belowbaseline macro of stackengine. The first minipage requires a different shift, because the colorbox is inside rather than outside the minipage.

\documentclass{article}
\usepackage{xcolor,stackengine}
\begin{document}
\noindent%
X
\belowbaseline[-\dimexpr\ht\strutbox+\fboxsep]{%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
    \colorbox{blue}{%
        \makebox[\dimexpr \linewidth - 2\fboxsep \relax]{%
            \color{white}%
            \huge S
    }}%%
\end{minipage}}%
\colorbox{gray}{%
\belowbaseline[-\ht\strutbox]{%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
     \huge A\\b
\end{minipage}}}%
\colorbox{yellow}{%
\belowbaseline[-\ht\strutbox]{%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
     a\\b\\c\\d
\end{minipage}}}%
\colorbox{orange}{%
\belowbaseline[-\ht\strutbox]{%
\begin{minipage}[t][.1\textheight][t]{.1\linewidth}%
     A\\b\\c\\d
\end{minipage}}}
\end{document}

enter image description here

4

This is another approach based on package xcoffins.

It is a package created to facilitate the precise layout of stuff on the page: text, tables, images, graphics, etc., in a very simple way. Handy to construct cover and title pages.

There are three steps:

(1) Fill in the content in the special boxes (\Ri to \Riv). Please note that exactly your content was used.

(2) Assemble the boxes together. In this simple case just the top-left corner of a box with the top-right corner of the previous box. (The first box to an empty box \Framex).

(3) Typeset the whole set.

Choosing different corners to join makes the reassembly easy.

See https://tex.stackexchange.com/a/576386/161015 for a more extensive explanation.

a

\documentclass{article}
\usepackage{xcolor}

\usepackage{xcoffins} %added <<<<<<<<<<

% Create
\NewCoffin\Framex
\NewCoffin\Ri
\NewCoffin\Rii
\NewCoffin\Riii
\NewCoffin\Riv


\begin{document}

% Fill  
\SetHorizontalCoffin\Ri{%
    \begin{minipage}[c][.1\textheight][t]{.1\linewidth}%
    \colorbox{blue}{%
    \makebox[\dimexpr \linewidth - 2\fboxsep \relax]{%
    \color{white}%
        \huge S
    }}%%
    \end{minipage}%
}%% 

\SetHorizontalCoffin\Rii{%.
    \colorbox{gray}{%
    \begin{minipage}[c][.1\textheight][t]{.1\linewidth}%
        \huge A
    \end{minipage}
}%  
}   


\SetHorizontalCoffin\Riii{%.
    \colorbox{yellow}{%
    \begin{minipage}[c][.1\textheight][t]{.1\linewidth}%
        a
    \end{minipage}
    }%  
}   

\SetHorizontalCoffin\Riv{%
    \colorbox{orange}{%
    \begin{minipage}[c][.1\textheight][t]{.1\linewidth}%
        A
    \end{minipage}}
}

  %%  Assembly] *************************
    \JoinCoffins\Framex[l,t]\Ri[l,t]
    \JoinCoffins\Framex[\Ri-r,\Ri-t]\Rii[l,t]
    \JoinCoffins\Framex[\Rii-r,\Rii-t]\Riii[l,t]
    \JoinCoffins\Framex[\Riii-r,\Riii-t]\Riv[l,t]
     
    % Typeset the assembly      
    \noindent\TypesetCoffin\Framex 
    
    \SetHorizontalCoffin\Framex{}
    
    \JoinCoffins\Framex[l,t]\Ri[l,t]
    \JoinCoffins\Framex[\Ri-r,\Ri-t]\Rii[l,b]% \Rii changed to b
    \JoinCoffins\Framex[\Rii-r,\Rii-b]\Riii[l,t]% changed to b
    \JoinCoffins\Framex[\Riii-r,\Riii-t]\Riv[l,b]% \Riv changed to b
    
    \bigskip
    
    Rearranged\bigskip
    
    \noindent\TypesetCoffin\Framex 

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .