# Pictures on horizontal page in a 2x2 grid with equal distances between them and the margins

I would like to add four pictures with slightly different aspect ratios on two rows on one horizontal page so that the distances between each picture and to the outer margin of the page are equal or at least very close to be equal in size.

How can I make LaTeX do the calculations?

This is my MWE:

\documentclass{article}
\usepackage[margin=0cm, top=0cm, bottom=0cm, outer=0cm, inner=0cm, landscape, a4paper]{geometry}
\pagestyle{empty}

\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\captionsetup[subfigure]{labelformat=empty}
\captionsetup{labelformat=empty}
\centering
\begin{subfigure}[t]{0.5\textheight}
\centering
\includegraphics[height=6cm]{example-image-a}
\caption[]%
{{\small}}
\label{}
\end{subfigure}
\begin{subfigure}[t]{0.5\textheight}
\centering
\includegraphics[height=6cm]{example-image-b}
\caption[]%
{{\small}}
\label{}
\end{subfigure}
\vskip\baselineskip
\begin{subfigure}[t]{0.475\textwidth}
\centering
\includegraphics[height=7cm]{example-image-c} % a pdf
\caption[]%
{{\small}}
\label{}
\end{subfigure}
\begin{subfigure}[t]{0.475\textwidth}
\centering
\includegraphics[height=7cm]
{example-image} % a pdf
\caption[]%
{{\small }}
\label{}
\end{subfigure}
\caption[]
{\small}
\label{}
\end{figure}

\end{document}


This can be achieved by using subfig package instead of subcaption package. With this you can define equal spacing from the borders using the combination of \hfill, \null and \hspace{...}. An MWE is given below:

\documentclass{article}
\usepackage[margin=0cm, top=0cm, bottom=0cm, outer=0cm, inner=0cm, landscape, a4paper]{geometry}
\pagestyle{empty}

\usepackage{graphicx}
%\usepackage{subcaption} %I take this out
\usepackage{subfig}
\begin{document}

\begin{figure}
\captionsetup[subfigure]{labelformat=empty}
\captionsetup{labelformat=empty}
\centering\hfill
\subfloat[][]{\includegraphics[height=6cm,keepaspectratio]{example-image-a}\label{figure1}}\hspace{2cm}
\subfloat[][]{\includegraphics[height=7cm,keepaspectratio]{example-image-b}\label{figure2}}\hfill\null\\
\hfill
\subfloat[][]{\includegraphics[height=7cm,keepaspectratio]{example-image-c}\label{figure3}}\hspace{2cm}
\subfloat[][]{\includegraphics[height=6cm,keepaspectratio]{example-image}\label{figure4}}\hfill\null
\end{figure}

\end{document}


In this I covered and ensured all the figures start at the same location using \hfill at the beginning. Similarly, the same technique applies at the end as well. Moreover, the inter-image spacing is handled by \hspace{...} to get an equal spacing. Note that I added a \null character to influence the spacing i.e., the ending of each figures in both the rows w.r.t the border per se. This will give you:

PS: Note that there are much more elegant ways to achieve this using Tikz, for example. But, this is the simplest I could think of ;)

Note: For exotic aspectratio/sizes of figures, you can use the \hspace{...} as your tuning knob to set things right.

• Thank you, Raaja. This help me solve my question. I will ask a similar question to this for tikz soon. ;) – Til Hund Feb 7 at 14:47
• @Til Hund I shall look forward to that ;) – Raaja Feb 7 at 14:48

This solution uses saveboxes 0-3 to measure the widths and \dimen0 to calculate the width of the left and right margins in the first line.

Note that box and length registers 0-9 are not used by standard LaTeX. One should still only use them inside a group (in this case, figure), so as to preserve their contents.

\documentclass{article}
\usepackage[margin=0cm, top=0cm, bottom=0cm, outer=0cm, inner=0cm, landscape, a4paper]{geometry}
\pagestyle{empty}

\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\captionsetup[subfigure]{labelformat=empty}
\captionsetup{labelformat=empty}
\sbox0{\includegraphics[height=6cm]{example-image-a}}% measure widths
\sbox1{\includegraphics[height=6cm]{example-image-b}}%
\sbox2{\includegraphics[height=7cm]{example-image-c}}% a pdf
\sbox3{\includegraphics[height=7cm]{example-image}}% a pdf
\centering
\begin{subfigure}[t]{\wd0}
\centering
\usebox0
\caption[]%
{{\small}}
\label{}
\end{subfigure}% the extra space will mess up calculations
\hfil
\begin{subfigure}[t]{\wd1}
\centering
\usebox1
\caption[]%
{{\small}}
\label{}
\end{subfigure}%
\par\vskip\baselineskip
\dimen0=\dimexpr \linewidth-\wd0-\wd1\relax% compute size of \hfil in previous line
\divide\dimen0 by 3
\hspace*{\dimen0}% left margin
%\makebox[\dimexpr \linewidth-2\dimen0][c]{% or center a box the same width
\begin{subfigure}[t]{\wd2}
\centering
\usebox2
\caption[]%
{{\small}}
\label{}
\end{subfigure}%
\hfill% overpowers \centering
\begin{subfigure}[t]{\wd3}
\centering
\usebox3
\caption[]%
{{\small }}
\label{}
\end{subfigure}\hspace*{\dimen0}% right margin
\caption[]
{\small}
\label{}
\end{figure}

\end{document}


• Thank you, John Kormylo, for adding another possible solution to my question. It is very much appreciated! :) – Til Hund Feb 8 at 12:16