Using the adjustbox
package with the export
option which extends graphicx
with several new keys you can now simply use the max width
key which scales the image down to that width if it is larger but doesn't scale it at all if it is smaller the equal to it. There are also the related keys max height
, max size
(for both width and height) as well as min width
, min height
and min size
.
In your case simply use the following code:
\includegraphics[max width=\linewidth]{<image file name>}
Note that you can't set max width
etc. in a global way using \setkeys{Gin}{max width=...}
. It is implemented in a different way as width
. However, I'm planning to add a possibility for this as well.
If the five images have the same aspect ratio, it's not difficult to solve the problem: let's say that the rectangles have base a and height a/r (with possibly different a each). Let's say we want to leave 12pt of blank space between the two halves and 3pt of space between the small figures.
We have thus to compute dimensions x and y, the bases of the big rectangle and of the small ones respectively, such that
x + 2y = \textwidth
-12pt-3pt
x/r = 2y/r + 3pt
A bit of linear algebra will tell that
x = (\textwidth - 15pt + 3ptr)/2
y = (\textwidth - 15pt - 3ptr)/4
Let's say that r=2; then
\documentclass[a4paper]{article}
\usepackage{graphicx,subfig}
\begin{document}
\begin{figure}
\centering
\subfloat[]{\includegraphics[width=\dimexpr(\textwidth-15pt+3pt*2)/2\relax]{fig1}}%
\hfill
\subfloat[]{\vbox{\offinterlineskip\halign{#\hskip3pt&#\cr
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*2)/4\relax]{fig2}&
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*2)/4\relax]{fig3}\cr
\noalign{\vskip3pt}
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*2)/4\relax]{fig4}&
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*2)/4\relax]{fig5}\cr
}}}
\caption{Test images}
\end{figure}
\end{document}
If the package subcaption is used instead of subfig, change \subfloat[]
into \subcaptionbox{}
(thanks to Axel Sommerfeldt).
I've used \halign
to get a more precise control on the spacing.
If the figure on the left has a different aspect ratio than the ones on the right, it's not difficult either.
\halign
is the TeX primitive on which tabular
is built. There's too much to say about it; the main thing is the structure:
\halign{<template1>&<template2>&...\cr
<body>
}
The tokens up to the first \cr
define the templates for the columns; each is separated from the following by &
. A template consists of three parts:
<before>#<after>
<before>
means tokens that are inserted at the start of each cell, #
stands for the actual cell entry in the body, and similarly <after>
means tokens that are inserted after the entry. For example a template for "centered cells" is
\hfil#\hfil
In our case the templates are trivial: #\hskip3pt
and #
; since all images will have the same width it's not necessary to specify anything else.
Notice that \halign
, by default, doesn't insert intercolumn space; to be precise it inserts \tabalign
glue, but talking about this would take too far away. The default value of the parameter is zero, anyway.
<body>
is similar to the body of a tabular
environment, but rows must be terminated by \cr
and spaces before &
and \cr
are not ignored; those after them are. Also the final row must be terminated by \cr
.
Between rows one can specify spacing (and other things) via \noalign
.
To get precise control on spacing, I've said \offinterlineskip
before \halign
so that TeX will butt all rows together without interline glue. LaTeX does the same, actually. This is done inside a \vbox
, so that the setting will be in force only there.
Best Answer
graphicx
provides the boolean valuekeepaspectratio
. It's explained in the documentation as follow: