Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Does anybody come up with a way to automatically arrange a few figures or boxes in a larger rectangle or page?

Here it is a real world example: One is producing a beamer presentation. One has 4 or 5 ready made figures, of different aspect ratios. And wants to distribute the figures regardless of any special order but want the slide to more or less fill up and the height or width of each figure to adjust "nicely" and not necessarily regular or rectangular.

The idea is to minimize the unused space and/or make the pictures align/resize nicely. But Doing this manually requires a lot of error and trial and keeping track of similar forced width or height of nearby images.

For example, Google Images and Google+ uses this technique to optimize the space taken by images and posts:


Fake code (will display 5 things automatically arranged, sequential order may be lost):

\begin{arrangedfigure}[5] %five boxes/images
\includegraphics{fig1} &
\includegraphics{fig2} &
\includegraphics{fig3} &
\minipage{text} &

Related questions, dealing with this but not in an automatic way:

Special arrangement of subfigures

What is the best way of arranging multiple minipages on a page?

How to arrange a figure consisting of four subfigures?

share|improve this question
This may be a helpful related solution, with LuaTeX used for a waterfilling problem with text. tex.stackexchange.com/questions/160811/… –  cslstr Mar 31 '14 at 19:26
@cslstr, definitely along the lines. I'll try to translate to my case. –  alfC Mar 31 '14 at 19:39
Several showcase experiments have been introduced here: tex.stackexchange.com/questions/64340/… –  Malipivo Apr 7 '14 at 8:49
Just a tip: I am using the shapepar package for text paragraph formatting, but I haven't tested it with graphics. It might be worth a try. –  Malipivo Apr 7 '14 at 9:17
This is a simple example of pictures behaving like words: \documentclass[a4paper]{article} \usepackage{shapepar} \usepackage{mwe} \def\incl#1 {\includegraphics[height=2.5ex]{example-image-#1} }% or let's use \baselineskip \begin{document} \shapepar{\rectangleshape{1}{2}}\incl16x10 \incl16x9 \incl1x1 \incl4x3 \incl10x16 \incl9x16 \incl16x10 \incl16x9 \incl1x1 \incl4x3 \incl10x16 \incl9x16 \incl16x10 \incl16x9 \incl1x1 \incl4x3 \incl10x16 \incl9x16 \incl4x3 \incl10x16 \incl9x16 \incl4x3 \incl10x16 \incl9x16 \end{document} –  Malipivo Apr 7 '14 at 11:00

1 Answer 1

I'm using the method developed in Making the text fit in a specific space in Latex to explore various aspects to this question. I am not rearranging any items, but am assuming that I have a string of N images that I am being asked to stuff into a certain size rectangle.

I am treating each image as text, and allowing line breaking to create the rows. So far, I've tried three version.

1) The raw version, relative figure sizes stay as given

2) make all figures same height

3) manually tweak the scaling, in an informal effort to avoid vast disparities in figure height and width, and to not leave last line largely un-full.

I have drawn a box around the image to help us gauge whether the boxes are overfull. REVISED to use \raggedright and \hfill to successfully avoid overrunning margins.


%\testaspect\\%                  COMMENT THIS LINE TO REMOVE ASPECT-RATIO PRINTS
%V\\%                            COMMENT THIS LINE TO REMOVE COMMENT
%                               CHANGE \framebox TO \makebox TO REMOVE FRAME
      \scaleto{\begin{minipage}[b]{\currentwidth}\raggedright #3\end{minipage}}{#1}%




  }{3\textheight}{1ex}%May have to increase next-to-last argument if divide by 0
\fancyhead[c]{Method 1: All images remain the same relative size}

\fancyhead[c]{Method 2: All images scaled to the same height}

\fancyhead[c]{Method 3: Manually Scale images}

enter image description here

enter image description here

enter image description here

EDIT: To answer the OP's question about vertical centering of the figures, it can be done by defining the images as centered about the baseline. In the case of \includegraphics, it would require a \raisebox of minus 1/2 the image height. In the case of my MWE, the down shift of \rules can be obtained with the optional argument.

share|improve this answer
My thoughts: I think that the empty space in the right bottom corner is one of the key problems. I believe that horizontal (between figures) and vertical spaces (between rows) are fixed and therefore this problem is so unique in opposition to common output from the shapepar package, because we must manipulate with individual figures during or after the optimization. I was thinking about having the same area for all the figures and if a change in either direction (making some figure bigger or smaller) is needed then we would need to start penalizing it. –  Malipivo Apr 7 '14 at 19:57
@Malipivo I thank you for your thoughts and I tend to agree with you. I will give the problem more thought. Perhaps treating the images as text is not the best way... –  Steven B. Segletes Apr 7 '14 at 22:36
This looks pretty good, I didn't tried yet. In this figures as "text" based solution. Is is possible to center the figures vertically in each line? –  alfC Apr 12 '14 at 1:48
@alfC Yes, in my MWE, if the \rules are all centered about their baseline (e.g,, via the optional argument \rule[-1in]{3in}{2in}). For real figures, you would need to import it in a manner such as \newsavebox\AAfig \setbox0=\hbox{\includegraphics[options]{filename}} \savebox{\AAfig}{\raisebox{-.5\ht0}{\box0}} \def\AA{\textcolor{green}{\usebox{\AAfig}}}. One strange thing, however. When I shifted the \rules halfway down as I describe, it still works, but affected the horizontal justification, for reasons I can't understand. –  Steven B. Segletes Apr 12 '14 at 2:57
@StevenB.Segletes, If I try to use this with beamer I get ! Arithmetic overflow. \calc@next@digit ... \multiply \calc@numerator 10 \calc@Acount \calc@numerat.... I will award the bounty at the moment, since this is the closest to an answer. –  alfC Apr 13 '14 at 23:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.