# How to gather (narrow) floats together horizontally?

I have a document with some narrow (less than half the \textwidth) images. Sometimes there's one such image on the page, and then the figure environment is what suffices. But sometimes there are two such floats on one page, and I'd like to have them put side by side. Does anybody know of a LaTeX package doing something like this? (A ConTeXt solution would be equally welcome.)

Edit: to be more precise, this is my use case: the document is a problem set. Some problems have a graphic associated with them, usually rather narrow. If two problems with a graphic happen to fall on the same page, I'd like to have both figures combined (maybe on the top of the page, maybe near the first problem, this is not that important for me). And if I change the order of problems (which I do quite frequently), or even add some more of them anywhere in the middle, the configuration of images can become totally different, and that's why I'd like the solution to be at least semi-automatic).

-
Would the two narrow floats be logically related to each other, e.g., could they form two panels of one figure environment, or do they just happen to be narrow enough to fit side by side (should one want to place them in such a manner)? –  Mico Nov 15 '11 at 20:57
@Mico: not rlated at all. The document is a problem set, and figures are associated with particular problems. If I change the order of problems, the figures go with them, obviously. And I do not want to waste space (nor I like the look of two narrow figures on their own). –  mbork Nov 15 '11 at 21:00
This seems impossible, since the contents of the floats should be dynamic (either one or two objects inside, if I got you right). But the location of the floats depend on the content, which is unknown. One way out is to not let the objects float or to bind either one or two objects together, nondynamically. However, is not what you ask for. –  Marco Nov 15 '11 at 21:16
OK, surely this can't be done in one pass. But I suspect it might be doable in more passes, in a kind of iterative process. (Of course, some manual tweaking would probably have to be done from time to time - I'm fine with that. It would be enough for me to have an automaton covering, say more than 50% cases or so.) –  mbork Nov 15 '11 at 22:08

In the interim, until someone can find a suitable alternative, here is what I would consider a semi-automatic solution:

You specify the problem-specific figure in a command that would accompany the figure. This way, in the logical layout of the document, things should be kept together. Then you set up a figure environment on a per-page basis, as needed, and merely list the commands (problem figures) in the figure environment. Here is a small MWE that illustrates this:

\documentclass{article}
\usepackage{caption}% http://ctan.org/pkg/caption
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\begin{document}
\begin{enumerate}
\item Here is a problem that is very easy to solve.

\item Here is a problem that is very difficult to solve.
It requires an additional figure in order for you to solve it.
It deals with \texttt{rocks}.
\newcommand{\rockfig}{%
\begin{minipage}{0.5\textwidth}
\centering\includegraphics{figure1}
\captionof{figure}{\texttt{Rocks} figure caption.}
\end{minipage}%
}

\item Here is a problem that is somewhat straight forward to solve.

\item Here is another difficult problem with a graphic description.
It requires an additional figure in order for you to solve it.
It deals with \texttt{sand}.
\newcommand{\sandfig}{%
\begin{minipage}{0.5\textwidth}
\centering\includegraphics{figure2}
\captionof{figure}{\texttt{Sand} figure caption.}
\end{minipage}%
}

\begin{figure}[tb]
\centering \rockfig \sandfig
\end{figure}

\item Here is an easy problem.
\end{enumerate}

\end{document}


This is semi-automatic since the little intervention of listing the commands in a figure environment seems minimal compared to a different, automated figure mechanism. Also, this approach works if you only have one figure within you per-page figure.

An improvement on this would have to, through trial-and-error, establish the following:

1. Whether there are more than two images on a page to break it up into separate "space-saving" figures;
2. Consider whether the height of the image(s) included on the page (whether individual or combined) does/does not push the actual problems onto the next page, requiring a subsequent break-up of the images again (eluding to a possible "oscillation" scenario, as commented by @Andy);
3. Align the figures in the appropriate order if there was a re-shuffle of problems;
4. Be flexible enough to accommodate a single or no image on the page.

While [3] and [4] may be considered as possible/likely, [1] and [2] might cost you a bounty in excess of your (or anybody's) reputation. In general one should consider the trade-off between whether or not the benefit of automation outweighs it's existence.

-
The last sentence alone is worth +1. –  Andy Nov 16 '11 at 8:29

I cannot provide a complete solution, but an idea with some pseudo code. Maybe someone who knows how to read the aux file could add the missing code. The MWE compiles, but has the missing stuff in the comments.

\documentclass{article}

\RequirePackage[paperwidth=170mm, paperheight=240mm, left=40pt, top=40pt, textwidth=280pt, marginparsep=20pt, marginparwidth=100pt, textheight=560pt, footskip=40pt]{geometry}%
\usepackage{lipsum}
\usepackage{caption}

\makeatletter
%
\newsavebox{\@boxone}
\newsavebox{\@boxtwo}

\newenvironment{smallfigure}%
{%
% if figure is first or single on page
\begin{lrbox}{\@boxone}%
% if figure is second on page
% \begin{lrbox}{\@boxtwo}%
\begin{minipage}{135pt}%
\captionsetup{type=figure}}%
{\end{minipage}%
\end{lrbox}%
% if figure is single on page
\usebox{\@boxone}%
% if figure is first on page
% do nothing
% if figure is second on page
% use the two boxes adjacent to each eacher
}
%
\makeatother

\begin{document}
%
\lipsum[1]
\begin{smallfigure}
\rule{115pt}{1em}
\caption{a}
\end{smallfigure}
\lipsum[1]
\begin{smallfigure}
\rule{115pt}{1em}
\caption{b}
\end{smallfigure}
\lipsum[1]
%
\end{document}


Later, the option of a third figure on the same page has to be added.

On a sidenote ;), we put small figures in the margin. Then, they are (almost) always next to text they are mentioned. It might or might not save space, depending on the number of small figures. That works particularly well with the marginfix package. If you want to give that a try, you can replace the \usebox line with \marginpar{\usebox{\@boxone}}% Hence, the weird page layout with ample margin.

-
Oh, and let's hope it does not oscillate. –  Andy Nov 16 '11 at 6:48