Floating minipages and other wizardry

Inspired by a current issue from my friend the LaTeX Noob, I wanted to give a short explanation on how you can combine floats (i.e. figures) and minipages. Why should you care? Well, if you need tikzpicture or images placed besides eachother or beside text. So most people will probably need this at some point ๐Ÿ˜‰ A great resource is the WikiBook, as always. If you want the lengthy account – that’s the way to go. For everybody else, an explanation of my own.

Floats and non-floating boxes

What are floats?

Some fundamental explanations first: A figure is a float. A minipage is not a float but a box which sits at its fixed place. These are two fundamentally different things. When you combine them in a bad way, LaTeX might get fed up at this. So when planning your minipaging or floating situation, ask yourself which effects are really important to you and which aren’t.

Do I even need a float?

A float will self-regulate positioning pretty much. If you need this feature, you need a float. But maybe this is a behaviour you explicitly don’t want anyway. Then maybe you don’t even need a float at all. Beginners often get confused and think they need to use figures everywhere but, strictly speaking, you don’t. You can, for example, just \includegraphics without a figure. Then the thing will just not have floating behaviour and you also might want captions and stuff, so an environment might be in order. But this need not be a figure. But if you don’t want all this, be aware that you might not need a figure at all and save yourself some trouble debugging.

I would suggest you probably go for figures anyway if it’s a long document where positioning might change dramatically after you first insert the picture, minipage or whatever. If, however, your result is pretty static and not a pages long document, you might just be better off leaving out the floats completely. Like, for example, in a poster, positioning isn’t really up for LaTeX to decide. You need it placed in a stable, reliable way and the way you want it and you exert lots of ‘control’ over the whole process.

Floats are better if you are dealing with a thing whose behaviour LaTeX is supposed to regulate acccording to its own best judgement. And as we all know, LaTeX’s judgement is pretty awesome. So this is a feature you might very well want to profit from in most cases. Also, you might want a \listoffigures. Or not. So think about that.

A little rant on “LaTeX is not doing what I want”

Beginners often get fed up with this behaviour because “LaTeX is not doing what I want”. This is probably due to the fact that you are using structures you think are supposed to do XYZ while they actually are programmed to do ABC. This is just a ‘misunderstanding’ between LaTeX and the user and can be remedied by additional knowledge about how some things work internally. So a general good bit of advice would be to look up how a structure you are trying to use is programmed to work internally. Most of the time, looking up the official documentation will already suffice. Probably it is supposed to do something completely different than what you thought it was or wanted it to. This is not LaTeX’s fault. It’s the fault of the user who doesn’t bother to look up those basic functionalities, so don’t blame LaTeX. But of course, if you’re still a beginner, this is a normal thing to do or expect. A common false assumption among programming novices. So don’t worry either. Next time you will know what to do.

A computer will do what you wrote down. Not what you meant. Even and especially, if the two are not the same thing ๐Ÿ˜‰

The objective: placing things side by side

We will follow up with some examples of figures and minipages. By now, you are already informed what the differences are and can make an intelligent choice ๐Ÿ˜‰

Case 1: The figure placed right here

The following is just a figure, but it will be forced to be placed right here (as indicated per the H). Using a small h really only means ‘here if it fits’. Only h! or H actually mean HERE!!!Also, read this great (lenghty) information on floats if you want to know more. Don’t forget to \usepackage{float} if you want to use the H. Edit: Thanks to Karl from LaTeX ref who informed me that h! basically only sometimes accidentally results in “place it HERE”. Use H for reliability.

So depending on your other needs (\listoffigures, etc.), you might as well have not used a floating environment since obviously, you did not intend for the thing to float.

\begin{figure}[H]
    \centering
     \begin{tikzpicture}
    \basesketch\bfseries
    \angles
    \end{tikzpicture}
    \caption{Sketch}
    \label{fig:Sketch}
\end{figure}

Case 2: Figure beside text with wrapfigure

The same thing like with minipage, basically, can be achieved with a wrapfigure, which you might need at some point too. This will make an image float beside text. On this, read the Overleaf tutorial.


\begin{wrapfigure}{R}{0.3\textwidth}
    \centering
    \begin{tikzpicture}[opacity=0.8, scale=0.5]
    \basesketch \angles
    \filldraw[draw=black, fill=lila, fill opacity=0.3] (A) -- (B) -- (C) -- cycle;
    \filldraw[draw=black, fill=myblue, fill opacity=0.3] (A) -- (C) -- (K2) -- (K1) -- cycle;
    \end{tikzpicture}
    \caption{T\textsubscript{1} und T\textsubscript{2}}
    \label{fig:Triangles}
\end{wrapfigure}

Case 3: lifesaving minipages

First, I’ll explain the basic functionality of minipages and then issue some personal tips on how to use them ๐Ÿ˜‰

Options

As you might have noticed, you have options when configuring your minipage: \begin{minipage}[adjusting]{width of the minipage} are the available ones. Meaning minipage has a specified alignment and a predetermined width. Although, you can use relative widths, of course. So it can be {3cm} or {0.3\textwidth} according to your needs. Notice however, that you cannot use 100% textwidth in total while still having everything aligned. Take off 0.03 in total to be sure. So all your minipages which are placed side by side cannot take up a total of 100% of the textwidth.

c = center, t = top and b = bottom are the alignment choices. c is default, I mostly use t. It can be a bit difficult to grasp what they actually do. They specify at which line the content gets aligned. So t means, the alignment will be oriented on the topmost, so the highest line.

Also, we can add more options [t][3cm][b] which additionally says that we have a fixed height of 3cm and the content will be aligned at the bottom. The (multiple) minipages themselves will be aligned at their tops.

Alignment of multiple minipages

Also, if you want multiple minipages aligned at their sides and not below eachother, you can’t have blank lines between them. Inside the minipage environments is no problem, but not following the first one before the second one, for example. See also Sasha Frank’s page and this.

When you check StackOverflow “How to use a figure inside a minipage” – the short answer is: You don’t!


\begin{figure}[ht]
  \begin{minipage}[b]{0.45\textwidth}
  \centering
    \includegraphics[width=\textwidth]{img1}
    \caption{blabla}
    \label{fig:fig1}
  \end{minipage}
  \hspace{0.5cm}
  \begin{minipage}[b]{0.4\textwidth}
    \centering
    \includegraphics[width=\textwidth]{img2}
    \caption{mycaption}
    \label{fig:fig2}
  \end{minipage}
\end{figure}

If you want to make two floats happily float, but in a row beside eachother, you can use the floatrow package, as detailed in this StackOverflow post.

Do I need a float now or what? Combining figure and minipage

If you don’t insist on having a float, people would probably go for minipage to make things sit aligned besides eachother and connected.

It is also not necessary to place your \includegraphics inside a figure, though a lot of beginners think so. Just put your images directly in a minipage if you don’t need floating behaviour. If you do – add a float. But not inside the minipage, this will cause an error. If you are confused about this, imagine a rubber duck floating in your bathtub. You cannot place it into a box because then it will sink. You can, however, make it carry a box on its back. So, this is basically the same thing with floats an minipages ๐Ÿ˜‰

You cannot have a figure (=float) inside a minipage (non-floating box) since it will make the rubber duck sink. Don’t sink your rubber duck! Read up on it hereย (just to warn you, they don’t use rubber ducks for explanation, so don’t be disappointed).

And, by the way, it also doesn’t make sense to have float inside a fixed box. Float means, LaTeX decides where exactly to place elements. But if you put a fixed box, you have already made that decision. Nothing left for LaTeX to decide. Meaning LaTeX will go and sulk at you. If you need LaTeX to calculate where to put things and it is possible your document will change a lot, it’s better to use floats. If you want everything exactly where you want it, float will make you go crazy. You can put tikzpicture inside minipages. It’s confusing, I know.

And remember: If you don’t want the minipages to become “disconnected”, which is usually the case, make sure you don’t put an empty line after one of them. They should be “connected” in the code if you want them connected in the output.

Another example (snippet) from my personal CV template to visualize language skills. As you can see, you might be able to achieve what you want using a tabular as well.

cv-languages

This is how the code looks like:


\newcommand{\icon}[3]{\phantom{x}{#3\color{#2}#1}\phantom{x}}
%------------------- pictogram Fraction: pictoFraction
\newcommand{\pictofraction}[6]{%
\pgfmathparse{#3 - 1}\foreach \n in {0,...,\pgfma<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>thresult}{\icon{#1}{#2}{#6}}%
\pgfmathparse{#5 - 1}\foreach \n in {0,...,\pgfmathresult}{\icon{#1}{#4}{#6}}%
}

\begin{minipage}[t]{\leftcolwidth}
\begin{tabular}{l | ll}
\textbf{German} & C2 & {\phantom{x}\footnotesize mother tongue} \\
\textbf{English} & C2 & \pictofraction{\faCircle}{blue}{3}{blue}{1}{\tiny}\\
   \textbf{Latin}  & C2 & \pictofraction{\faCircle}{blue}{3}{blue}{1}{\tiny}\\
   \textbf{French} & C2 & \pictofraction{\faCircle}{blue}{3}{blue}{1}{\tiny}\\
   \textbf{Ancient Greek} & B2 & \pictofraction{\faCircle}{blue}{3}{grey!30}{1}{\tiny}\\

\end{tabular}
\end{minipage}

There also is the subcaption package (subfigure or subfig are deprecated). Here, you can use sub-floats inside a single float. Here is an example of how to use it.

You can also put minipages inside a float, to make the whole thing float again. And that’s about it ๐Ÿ˜‰

And just for your reference, I included the actual TikZ at the bottom of the post. Just in case you wanted it or whatever ๐Ÿ˜‰

I really love minipages. They are my go-to thing for anything and everything. Sometimes even when there probably would be another (better) option available. Minipages rock! And so do you!

Best,

the LaTeX Ninja

The TikZ

\newcommand{\basesketch}{%
\scriptsize
\node[](Kreismittelpunkt) at (0,0) {};
\node[](K1) at (0,6) {};
\node[](K2) at (4,5) {};

\draw[] (Kreismittelpunkt) -- ++(0:5cm) node[](C){} -- ++(0:5cm) node[](K2){};
\draw[] (Kreismittelpunkt) -- ++(30:4cm)  node[](A){}  --  ++(30:2cm)  node[](B){}  -- ++(30:4cm) node[](K1){};
\draw[] (K2) -- (K1);
\draw[fill=myblue] (K2) circle (2.5pt) node[below=0.5em]{K\textsubscript{2}};
\draw[fill=myblue] (K1) circle (2.5pt) node[above=0.5em]{K\textsubscript{1}};

\draw[fill=myblue] (A) circle (2.5pt) node[above=0.5em]{A};
\draw[fill=myblue] (B) circle (2.5pt) node[above=0.5em]{B};
\draw[fill=myblue] (C) circle (2.5pt) node[below=0.5em]{C};
\draw[] (A) -- (C);
\draw[] (C) -- (B);
\draw[] (B) -- (K2);

% coordinates
\coordinate (K2) at (K2);
\coordinate (K1) at (K1);
\coordinate (A) at (A);
\coordinate (B) at (B);
\coordinate (C) at (C);

\draw[fill=myblue] (Kreismittelpunkt) circle (2.5pt) node[below=0.5em]{K};

% Alternative zu Arc zwischen zwei Punkten
\draw[-,draw=black!70] (K2) to[bend right=12] (K1);
\pgfresetboundingbox}

\newcommand{\angles}{%
\pic[draw=black!70,text=black!70, -,"$\alpha$"] {angle =A--C--Kreismittelpunkt};
\pic[draw=black!70,text=black!70, -,"$\delta$", angle eccentricity=1.3] {angle= B--C--A};
\pic[draw=black!70,text=black!70, -,"$\alpha$"] {angle = K2--C--B};
\pic[draw=black!70,text=black!70, -,"$\alpha$"] {angle = C--B--K2};

\pic[draw=black!70,text=black!70, -,"$\beta$"] {angle = A--B--C};
\pic[draw=black!70,text=black!70, -,"$\beta$"] {angle = K2--B--K1};

\pic[draw=black!70,text=black!70, -,"$\gamma$"] {angle = K1--K2--C};
\pic[draw=black!70,text=black!70, -,"$\gamma$"] {angle = B--K1--K2};

\pic ["\Huge $\cdot$", draw, -] {angle=C--A--B};
\pic ["\Huge $\cdot$", draw, -] {angle=Kreismittelpunkt--A--C};}

Buy me coffee!

If my content has helped you, donate 3โ‚ฌ to buy me coffee. Thanks a lot, I appreciate it!

€3.00

Advertisements

I like LaTeX, the Humanities and the Digital Humanities. Here I post tutorials and other adventures.

2 thoughts on “Floating minipages and other wizardry

  1. Hello Ninja – thanks for the great post. But, one thing: a placement of [h!] does not mean “put the float here”. The ! only means to ignore restrictions on the number of floats and float/non-float text on the page, which sometimes, but nowhere near always, results in a “here” placement. For reliability, [H] must be used.

    FWIW, some colleagues and I tried to write a short description of all this at https://latexref.xyz/Floats.html.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.