# Using subfigure environment for arranging image in grids using multiple rows and columns

I am trying to set up a grid-based structure for images in latex document.

Currently, I am using tabular under figure environment to achieve this. However, the outcomes are not well-aligned and flow outside the frame. How to align images properly?

\begin{figure*}
% \setlength{\abovecaptionskip}{6pt}
% \setlength{\belowcaptionskip}{6pt}
\centering
\begin{tabular}{ccc}
& \subfloat[B]{\includegraphics[width=0.30\textwidth]{images/2.png}} &
\subfloat[C]{\includegraphics[width=0.30\textwidth]{images/3.png}} \\
\multirow[t]{-2}{*}{\subfloat[A]{\includegraphics[width=0.40\textwidth]{images/1.png}}} &
\multicolumn{2}{c}{\subfloat[D]{\includegraphics[width=0.60\textwidth]{images/4.png}}} \\
\end{tabular}
\caption{Many figures}
\end{figure*}


The current output is as below. The black lines just indicate frame border enabled using showframe for debugging. By editing the width percentages I am able to improve it but it is not perfect.

Is it possible to do this better using subfigure for example? Also, I want to label the first large vertical image as (a) but it is labeled as (c)

Any help/ documentation/ tutorial is appreciated. Thanks!

UPDATE 1

As requested by @liandriis I have attached the images used (a,b,c,d) Dimensions:

• a: 417 x 476, Imgur: P0NIeKI
• b: 255 x 254, Imgur: 4JROoAO
• c: 301 x 255, Imgur: xcAkKXp
• d: 565 x 203, Imgur: qvlItEf

Update 2 Here's what I was able to obtain based on the method shared by @David_Carlisle

The issue is getting the captions in place. Also, it takes a lot of trial and error to get to the right numbers but is worth it for the perfect positioning achievable in a given document/ environment.

Update 3

So continuing on the recommendation by @David_Carlisle I was able to get all positions perfectly right now. As I mentioned previously it just needed fine-tuning the values of positions. I am still trying to understand what these numbers are (absolute position or offset from where). The issue still I have is 1) The figure count increments both for subfigure and the main figure in a sequence (ie. a,b,c,d,5 instead of a,b,c,d,1). 2) The caption is not visible for subfigures.

\begin{figure}
\begin{picture}(0,240)
\renewcommand\thefigure{\alph{figure}}%
\renewcommand\caption[1]{\refstepcounter{figure}%
\par\centering(\thefigure)\par}%

\put(0,0){\begin{minipage}[b]{.43\textwidth}
\includegraphics[width=\linewidth]{images/a.png}
\caption{zz a test}
\end{minipage}}

\put(228,130){\begin{minipage}[b]{.24\textwidth}
\includegraphics[width=\linewidth]{images/b.png}
\caption{zz b}
\end{minipage}}

\put(370,130){\begin{minipage}[b]{.285\textwidth}
\includegraphics[width=\linewidth]{images/c.png}
\caption{zz c}
\end{minipage}}

\put(228, 0){\begin{minipage}[b]{.56\textwidth}
\includegraphics[width=\linewidth]{images/d.png}
\caption{zz d}
\end{minipage}}

\end{picture}

\caption{Figures zzz}
\end{figure}


Update 4 Based on this answer shared by @Zarko I used minipages and after some finetuning, I was able to achieve the desired outcome. Sharing here for anyone interested:

\begin{figure*}
\begin{minipage}{0.425\textwidth}%
\begin{subfigure}{\linewidth}%
\includegraphics[width=\textwidth]{images/a.png}
\caption{A figure.}
\label{fig:merge-scenario}
\end{subfigure}
\end{minipage}%
\hspace{0.5mm}
\begin{minipage}{0.545\textwidth}%
\begin{minipage}{0.47\textwidth}%
\begin{subfigure}{\linewidth}%
\includegraphics[width=\linewidth]{images/b.png}
\caption{State A}
\label{fig:case-a}
\end{subfigure}
\end{minipage}
\begin{minipage}{0.55\textwidth}%
\begin{subfigure}{\linewidth}%
\includegraphics[width=\linewidth]{images/c.png}
\caption{State A}
\label{fig:case-a}
\end{subfigure}
\end{minipage}
\begin{subfigure}{\textwidth}%
\includegraphics[width=1.03\linewidth]{images/d.png}
\caption{State C}
\label{fig:case-c}
\end{subfigure}
\end{minipage}
\caption{Another figure.}
\end{figure*}


• An answer might heavily depend on the aspect ratios of the involved images. Could you provide them? – leandriis Jan 10 at 20:09
• Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Alessandro Cuttin Jan 10 at 20:29
• @Zarko yes it is very similar to what I need. How will I go about the 2 column part in the bottom right side of my layout? I am trying to see if I can do that with the minipage based solution provided in the post you shared. – DrSpill Jan 10 at 20:55
• @Zarko while I appreciate that the original question has a similar problem the same answer doesn't help address the issue here, so I would not call this a duplicate. Here we are trying to spread sub-images both in columns and rows. Spreading in just rows is straightforward with minipages (at least in my understanding). But how would we do this for multicolumn (This is not answered in that question)? – DrSpill Jan 11 at 5:04

When grouping images, if they are not artificial rectangles as here you are often best to adjust "by eye" rather than just mechanically position them without taking account of any optical misalignments due to white space or contrasting areas in the images.

Here I positioned them at hand chosen coordinates. I also scaled the image for both height and depth just to obtain images of different aspect ratios. Normally best to just scale in one direction to avoid distortion.

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\begin{figure}

\begin{picture}(0,200)
\renewcommand\thefigure{\alph{figure}}%
\renewcommand\caption[1]{\refstepcounter{figure}%
\par\centering(\thefigure)\par}%

\put(0,-10){\begin{minipage}[b]{.2\textwidth}
\includegraphics[width=\linewidth, height=6cm]{example-image}
\caption{zz a}
\end{minipage}}

\put(80,120){\begin{minipage}[b]{.2\textwidth}
\includegraphics[width=\linewidth, height=2cm]{example-image}
\caption{zz b}
\end{minipage}}

\put(160,120){\begin{minipage}[b]{.5\textwidth}
\includegraphics[width=\linewidth, height=2cm]{example-image}
\caption{zz c}
\end{minipage}}

\put(80,0){\begin{minipage}[b]{.7\textwidth}
\includegraphics[width=\linewidth, height=3cm]{example-image}
\caption{zz d}
\end{minipage}}

\end{picture}

\caption{Figures zzz}

\end{figure}

\noindent X\dotfill X
\end{document}

• This method looks good. Thanks for sharing. Particularly I am able to pixel perfect the alignment of the grid structure by adjusting those numbers. However, I am having issues positioning the captions properly. Even in your example, I do not see the caption. Is there something missing? Also would be great if you can give a little bit intuition on those numbers. I assumed it is width, height but the number didn't make sense. I feel it is offsets instead, but offset from where? – DrSpill Jan 11 at 4:21
• Please refer to my updates 2 and 3, on this. – DrSpill Jan 11 at 5:31

Thanks for all the inputs provided so far @DavidCarlisle, @Zarko and also this useful answer https://tex.stackexchange.com/a/494135/204652

I am compiling my updates as an answer here as it exactly solves the question. The key idea is to use mini pages effectively in order to arrange elements inside a figure environment. Unlike the previous answer, this example shows the case where a single image is spread over multiple rows or columns. This approach can be generalized for any other layout.

\usepackage{subcaption}
\usepackage{graphicx}
\begin{figure*}
\begin{minipage}{0.425\textwidth}%
\begin{subfigure}{\linewidth}%
\includegraphics[width=\textwidth]{images/a.png}
\caption{A figure.}
\label{fig:merge-scenario}
\end{subfigure}
\end{minipage}%
\hspace{0.5mm}
\begin{minipage}{0.545\textwidth}%
\begin{minipage}{0.47\textwidth}%
\begin{subfigure}{\linewidth}%
\includegraphics[width=\linewidth]{images/b.png}
\caption{State A}
\label{fig:case-a}
\end{subfigure}
\end{minipage}
\begin{minipage}{0.55\textwidth}%
\begin{subfigure}{\linewidth}%
\includegraphics[width=\linewidth]{images/c.png}
\caption{State A}
\label{fig:case-a}
\end{subfigure}
\end{minipage}
\begin{subfigure}{\textwidth}%
\includegraphics[width=1.03\linewidth]{images/d.png}
\caption{State C}
\label{fig:case-c}
\end{subfigure}
\end{minipage}
\caption{Another figure.}
\end{figure*}


This results in the following layout:

Note that the numbers used in the code are hard-coded to match the aspect ratio of the images. This need to be appropriately tuned as needed.

Hope the above helps anyone interested in arranging images in grids in Latex.