1

I'm trying to insert 4, in a 2 by 2 sort of matrix with this code

\begin{figure}[H] \centering
    \includegraphics[width=0.48\textwidth, keepaspectratio]{figure/s0}
    \hspace{2mm}
    \includegraphics[width=0.48\textwidth, keepaspectratio]{figure/s1}
    \hfill
    \includegraphics[width=0.48\textwidth, keepaspectratio]{figure/s2}
    \hspace{2mm}
    \includegraphics[width=0.48\textwidth, keepaspectratio]{figure/s3}
    \hfill
\end{figure}

I made these images with Photoshop with dimension 400x250px. Why are not correctly aligned? Is it a code's problem? Because I checked with Photoshop and all of them have the same dimensions.

enter image description here

Shika93
  • 371

1 Answers1

2

Your code has several unwanted spaces. It's better to do it like

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\begin{figure}[htp]

\centering

\includegraphics[width=0.48\textwidth, keepaspectratio]{example-image}%
\hspace{2mm}%
\includegraphics[width=0.48\textwidth, keepaspectratio]{example-image-a}

\vspace{2mm}\vspace{-\lineskip}

\includegraphics[width=0.48\textwidth, keepaspectratio]{example-image-b}%
\hspace{2mm}%
\includegraphics[width=0.48\textwidth, keepaspectratio]{example-image-c}

\end{figure}

\end{document}

Assuming you want 2mm between each image, horizontally and vertically, do two paragraphs and add the vertical space between them. A fix of -\lineskip is also needed. The fill at either side is already provided by \centering.

Note % in order not to add unwanted spaces.

enter image description here

What happens with your code?

Between the first two images you have a space of 2mm plus the normal interword space; this almost fills a line, so the first \hfill is discarded together with the interword space after the second image (due to the end-of-line). Because of \centering, the entire line is moved to the right margin.

The opposite happens in the second line, which is pushed to the left margin by the final centering glue.

egreg
  • 1,121,712