# minipage flow into next line if too big

I'm trying to create minipages (or any kind of a box) that have a width of either 0.5\textwidth or \textwidth depending on the size of its contents. I want the smaller minipages (with 0.5\textwidth) to be next to eachother, the longer ones should break in the next line automatically.

What i'm trying to do is broken down in this example:

\documentclass{article}

\begin{document}
\section*{Section}
\begin{minipage}{0.49\textwidth}
Minipage with small contents
\end{minipage}
\hfill{}
\begin{minipage}{0.49\textwidth}
Minipage with small contents
\end{minipage}
\hfill{}
\begin{minipage}{0.49\textwidth}
Minipage with small contents
\end{minipage}
\hfill{}
\begin{minipage}{\textwidth}
Minipage with a lot of very very long content which should overflow in the next line because it is not able to fit into the previous line.
\end{minipage}
\hfill{}
\end{document}


The result is the following where the last longer minipage should be in the next line beneath the 3rd minipage leaving an empty space where the long minipage is now:

(I'm generating the .tex-file with some program code so the order is not important, it doesn't matter if the minipages are sorted down and then left or down, left, down, left, ... because I can easily change the code.)

If possible I also wanted the minipage before the longer minipage to increase its size to \textwidth too but this is not the main part of this question.

I'm fairly new to LaTex, if there are any pre defined environments or anything that would be a better choice than minipages please feel free to let me know.

Key changes to the OP's MWE:

1. use % between minipages to avoid stray spaces.

2. Use \hfill\allowbreakto allow line breaks.

3. No need to shrink minipage to .49\textwidth.

4. align minipages with [b] alignment.

5. apply a \strut before the end of each minipage to achieve proper line spacing.

The MWE:

\documentclass{article}
\usepackage[showframe,pass]{geometry}
\begin{document}
\section*{Section}
\begin{minipage}[b]{0.5\textwidth}
Minipage with small contents
\strut\end{minipage}%
\hfill\allowbreak%
\begin{minipage}[b]{0.5\textwidth}
Minipage with small contents
\strut\end{minipage}%
\hfill\allowbreak%
\begin{minipage}[b]{0.5\textwidth}
Minipage with small contents
\strut\end{minipage}%
\hfill\allowbreak%
\begin{minipage}[b]{\textwidth}
Minipage with a lot of very very long content which should overflow in the next line because it is not able to fit into the previous line.
\strut\end{minipage}%
\hfill\allowbreak%
\begin{minipage}[b]{0.5\textwidth}
Minipage with small contents
\strut\end{minipage}%
\hfill\allowbreak%
\begin{minipage}[b]{0.5\textwidth}
Minipage with small contents
\strut\end{minipage}%
\hfill\allowbreak%
\begin{minipage}[b]{0.5\textwidth}
Minipage with small contents
\strut\end{minipage}%
\hfill\allowbreak%
\begin{minipage}[b]{\textwidth}
Minipage with a lot of very very long content which should overflow in the next line because it is not able to fit into the previous line.
\strut\end{minipage}%
\end{document}


• Works perfectly, thank you. It's not very important but in my original code changing the width of the minipage to 0.5\textwidth will push all the minipagess to a separate line. I just re-did the 0.49 again, I was just wondering what could cause this. Nov 3, 2017 at 12:31
• @miile7 If you are missing any line-ending % characters between the end of one minipage and the beginning of the next, an unwanted space character will be added. Nov 3, 2017 at 12:36