TransWikia.com

Alignment of multiple figures in rows and columns

TeX - LaTeX Asked by SemtexB on January 5, 2021

I got seven images I want to show in a single figure. Two large ones shall be aligned in the first row in two columns. The second row shall consist of four half size images below the first image (located in row=0 and col=0), where the half size image are aligned on a 2×2 grid, which only occupies one row though. The last image is of the same size as the first two and shall be aligned below the second image (located in row=0 and col=1). I already got this, which pretty much does what I need, but with some minor drawbacks:

documentclass[12pt,a4paper,twoside, draft]{article}
usepackage{graphicx}
usepackage{subcaption} %to have subfigures available

begin{document} 
begin{figure}[h!]
     centering
    begin{subfigure}[t]{0.49textwidth}
        includegraphics[width=textwidth]{img1.png}
        caption{caption of first image}
    end{subfigure}
    hfill
    begin{subfigure}[t]{0.49textwidth}
        includegraphics[width=textwidth]{img2.png}
        caption{caption of second imagesecond line}
    end{subfigure}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%second row
    begin{subfigure}[ht]{0.49textwidth}
            begin{subfigure}[t]{0.49textwidth}
                includegraphics[width=textwidth]{img3.png}
            end{subfigure}
            %
            begin{subfigure}[t]{0.49textwidth}
                includegraphics[width=textwidth]{img4.png}
            end{subfigure}

            begin{subfigure}[t]{0.49textwidth}
                includegraphics[width=textwidth]{img5.png}
            end{subfigure}
            %
            begin{subfigure}[t]{0.49textwidth}
                includegraphics[width=textwidth]{img6.png}
            end{subfigure}
        caption{caption of the four small imagessecond linethird line}
    end{subfigure}
    hfill
    begin{subfigure}[ht]{0.49textwidth}
        includegraphics[width=textwidth]{img7.png}
    caption{caption of last imagesecond line} 
    end{subfigure}
    caption{caption of main figure}
end{figure}
end{document}

With drawbacks I mean
1) the top border of the images in the second row is not nicely aligned. How can I fix this?
2) the four images in the 2×2 grid show space when being next to each other, but are too close above of each other. How can I get some space between first and second row in the 2×2 grid.

EDIT: I just noticed the problems might only partially appear here, since all captions fit in one line. If the captions of the first two images occupy a different amount of lines then 1) will appear.

3 Answers

The [t] option uses the baseline of the first line (same as minipage). You can see the baseline using leavevmoderlap{rule{textwidth}{1pt}}. So subfigures using [t] are actually aligned by the bottoms of the images, not the tops. To align the tops you would need raisebox{-height}{...} on every image.

documentclass[12pt,a4paper,twoside, draft]{article}
usepackage{graphicx}
usepackage{subcaption} %to have subfigures available

begin{document} 
begin{figure}
     centering
    begin{subfigure}[t]{0.49textwidth}
        raisebox{-height}{includegraphics[width=textwidth]{img1.png}}
        caption{caption of first image}
    end{subfigure}
    hfill
    begin{subfigure}[t]{0.49textwidth}
        raisebox{-height}{includegraphics[width=textwidth]{img2.png}}
        caption{caption of second imagesecond line}
    end{subfigure}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%second row
    begin{subfigure}[t]{0.49textwidth}
        raisebox{-height}{includegraphics[width=0.49textwidth]{img3.png}}
        raisebox{-height}{includegraphics[width=0.49textwidth]{img4.png}}%
        vspace{.6ex}
        raisebox{-height}{includegraphics[width=0.49textwidth]{img5.png}}
        raisebox{-height}{includegraphics[width=0.49textwidth]{img6.png}}
        caption{caption of the four small imagessecond linethird line}
    end{subfigure}
    hfill
    begin{subfigure}[t]{0.49textwidth}
        raisebox{-height}{includegraphics[width=textwidth]{img7.png}}
    caption{caption of last imagesecond line} 
    end{subfigure}
    caption{caption of main figure}
end{figure}
end{document}

demo

Correct answer by John Kormylo on January 5, 2021

I typically use subfloat instead of subfig and don't nest them. Try this MWE code:

usepackage{subfig}

%%%%%%%%%%%%

begin{figure}[htb]
caption{Caption of Figure}
label{fig:figlabel}
begin{center}
subfloat[Subcaption a]      {
includegraphics[width=.45linewidth]{image1}
}quad
subfloat[Subcaption b]      {
includegraphics[width=.45linewidth]{image2}
}
subfloat[Subcaption c]      {
includegraphics[width=.45linewidth]{image3}
}quad
subfloat[Subcaption d]      {
includegraphics[width=.45linewidth]{image4}
}
end{center}

I typically use and quad to respectively do vertical and horizontal spacing. You can adjust the width of the graphic accordingly if you want 3 images per row, 1 image per row, etc.

Answered by Tyler R. on January 5, 2021

Your code is not compilable so I am not sure if I got your right. Do you want this?

% arara: pdflatex

documentclass[12pt,a4paper,twoside]{article}
usepackage{subcaption}
usepackage[demo]{graphicx}

begin{document} 
    begin{figure}
        centering
        begin{subfigure}[t]{0.49textwidth}
            includegraphics[width=textwidth]{img1}
            caption{caption of first image}
        end{subfigure}
        hfill
        begin{subfigure}[t]{0.49textwidth}
            includegraphics[width=textwidth]{img2}
            caption{caption of second image}
        end{subfigure}

        begin{subfigure}[t]{0.49textwidth}
            begin{subfigure}[t]{0.49textwidth}
                includegraphics[width=textwidth]{img3}
            end{subfigure}
            %
            begin{subfigure}[t]{0.49textwidth}
                includegraphics[width=textwidth]{img4}
            end{subfigure}vspace{.6ex}

            begin{subfigure}[t]{0.49textwidth}
                includegraphics[width=textwidth]{img5}
            end{subfigure}
            %
            begin{subfigure}[t]{0.49textwidth}
                includegraphics[width=textwidth]{img6}
            end{subfigure}
            caption{caption of the four small images}
        end{subfigure}
        hfill
        begin{subfigure}[t]{0.49textwidth}
            includegraphics[width=textwidth]{img7}
            caption{caption of last image} 
        end{subfigure}
        caption{caption of main figure}
    end{figure}
end{document}

enter image description here

Note that you have to leave a blank line if you want a line break. I removed your %%%%% for this.

Also I have never seen the [ht] qualifier for a subfigure. I am not sure but I guess, this does not exist.

Answered by LaRiFaRi on January 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP