[Tex/LaTex] Make a minipage float

floatsminipagepositioning

I am using the minipage environment to put a table and figure next to each other. Now I would like this to 'float' like a normal figure or table, such that LaTeX decides where to put it (using the [htb] options). After some googling, I found that the minipage environment is not a floating one, so it will always be inserted in the same location as the code. This is not always the nicest solution.

Is there a way to prevent/circumvent this, such that also a minipage is treated like a float?

Best Answer

You can use an existing float such as figure instead of minipage to hold your side by side figure and table.

\documentclass{article}

\usepackage{capt-of}


\makeatletter
%\let\ftype@table\ftype@figure
\makeatother


\begin{document}

\listoffigures

\listoftables

\clearpage


\begin{figure}
\begin{minipage}{.4\textwidth}XXX
\captionof{figure}{fff}
\end{minipage}\hfill
\begin{minipage}{.4\textwidth}\rule{2cm}{1cm}
\captionof{table}{ttt}
\end{minipage}
\end{figure}

aaa

\begin{figure}
\begin{minipage}{.4\textwidth}YYY
\captionof{figure}{fff}
\end{minipage}\hfill
\begin{minipage}{.4\textwidth}\rule{2cm}{3cm}
\captionof{table}{ttt}
\end{minipage}
\end{figure}

aaa

\begin{table}\rule{2cm}{7cm}\quad\rule{2cm}{7cm}
\caption{TTT}
\end{table}

aaa

\begin{figure}
\begin{minipage}{.4\textwidth}XXX
\captionof{figure}{fff}
\end{minipage}\hfill
\begin{minipage}{.4\textwidth}\rule{2cm}{1cm}
\captionof{table}{ttt}
\end{minipage}
\end{figure}

aaa

\begin{figure}
\begin{minipage}{.4\textwidth}YYY
\captionof{figure}{fff}
\end{minipage}\hfill
\begin{minipage}{.4\textwidth}\rule{2cm}{3cm}
\captionof{table}{ttt}
\end{minipage}
\end{figure}

aaa


\end{document}

Note however that while this works, LaTeX sees this as a figure while floating boxes around so only keeps it in sequence with figures this means that if some tables are on their own in a table environment they may float out of sequence with tables that are in a figure environment.

The end result is that the list of tables looks like

enter image description here

The simplest fix is to uncomment the commented line so that figures and tables are kept in the same sequence. This has the disadvantage that LaTeX won't then float any figure past any table so it gives less flexibility in the algorithm but it has the advantage of working and placing teh floats in the correct order as shown in the lists:

enter image description here

Related Question