[Tex/LaTex] How to set the distance between figures with subcaption

spacing

How I can control the distance between figures without modify the width?

For example suppose I have 3 images of width 0.20\linewidth. If I add another image of width 0.20\linewidth it will fit on the same row, but I want this 4th image to go to the 2nd row, and set in 0.1\linewidth the distance between the 3 first images.

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{mwe}
\usepackage{subcaption}

\begin{document}
\begin{figure}[h!]
\centering
\subcaptionbox{way 1\label{AA.eps}}
{\includegraphics[width=.32\linewidth]{example-image-a.jpg}}
\subcaptionbox{Forma 2\label{AA.eps}}
{\includegraphics[width=.32\linewidth]{example-image-b.jpg}}
\subcaptionbox{Forma 3\label{AA.eps}}
{\includegraphics[width=.32\linewidth]{example-image-c.jpg}}
%
\subcaptionbox{Forma 4\label{AA.eps}}
{\includegraphics[width=.32\linewidth]{example-image-a.jpg}}
\subcaptionbox{Forma 5\label{AA.eps}}
{\includegraphics[width=.48\linewidth]{example-image-c.jpg}}
%\subcaptionbox{AA\label{AA.eps}}
%{\includegraphics[width=.0\linewidth]{AA.eps}}
\caption{5 formas de generar vecindarios.}\label{fig:5formas}
\end{figure}


\end{document} 

Best Answer

Instead of using a comment character % to separate the figure construction in the code, use an empty line and specify the vertical distance using \vspace (or something similar):

enter image description here

\documentclass{article}
\usepackage{mwe,subcaption}

\begin{document}
\begin{figure}[ht]
  \centering
  \subcaptionbox{A}
    {\includegraphics[width=.32\linewidth]{example-image-a}}
  \subcaptionbox{B}
    {\includegraphics[width=.32\linewidth]{example-image-b}}
  \subcaptionbox{C}
    {\includegraphics[width=.32\linewidth]{example-image-c}}

  \vspace{\baselineskip}

  \subcaptionbox{A}
    {\includegraphics[width=.32\linewidth]{example-image-a}}
  \subcaptionbox{B}
    {\includegraphics[width=.48\linewidth]{example-image-c}}
  \caption{A figure caption}
\end{figure}

\end{document} 

Above the empty line following the three sub-figures moves TeX into vertical mode. Then, \vspace{\baselineskip} inserts an empty/blank line in the output, after which the second set of the two sub-figures are inserted (horizontally).

Note that it's more intuitive to specify a vertical length when using \vspace rather than a horizontal length (like \linewidth).

Related Question