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.
Many thanks in advance for your help.
Best Answer
Key changes to the OP's MWE:
use
%
betweenminipage
s to avoid stray spaces.Use
\hfill\allowbreak
to allow line breaks.No need to shrink
minipage
to.49\textwidth
.align
minipage
s with[b]
alignment.apply a
\strut
before the end of eachminipage
to achieve proper line spacing.The MWE: