7

How can I shift the subfigure (j) to the left such that the horizontal space x between subfigure (i) and (j) is equal to all subfigures like in the first or second row.

enter image description here

My Code:

\documentclass{article}

\usepackage{subcaption} \usepackage{graphicx}

\begin{document}

\begin{figure}[h!]
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % first row
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{subfigure}{0.22\textwidth}
        \includegraphics[width=\textwidth]{example-image}
        \caption{First subf}
    \end{subfigure}
    \hfill %%
    \begin{subfigure}{0.22\textwidth}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Second subf}
    \end{subfigure}
    \hfill %%
    \begin{subfigure}{0.22\textwidth}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Third subf}
    \end{subfigure}
    \hfill %%
    \begin{subfigure}{0.22\textwidth}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Fourth subf}
    \end{subfigure}
    \hfill %%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % second row
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{subfigure}{0.22\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Fifth subf}
    \end{subfigure}
    \hfill %%
    \begin{subfigure}{0.22\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Sixth subf}
    \end{subfigure}
    \hfill %%
    \begin{subfigure}{0.22\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Seventh subf}
    \end{subfigure}
    \hfill %%
    \begin{subfigure}{0.22\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Eighth subf}
    \end{subfigure}
    \hfill %%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % third row
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{subfigure}{0.39333333333\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Tenth subf}
    \end{subfigure}
    \hfill %%
    \begin{subfigure}{0.39333333333\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Eleventh subf}
    \end{subfigure}
    \hfill %%

    \caption{Subreferences in \LaTeX.}
    \label{fig:figures}
\end{figure}

\end{document}

Solution (thanks to egreg):

Calculating the horizontal space x between two subfigures in the first or second row like this:

x = ( 1 - 4 * 0.22 ) / 3 = 0.04

And add this code with the calculated value x after tenth subfigure (i):

\hspace{0.04\textwidth}

enter image description here

\documentclass{article}

\usepackage{subcaption} \usepackage{graphicx}

\begin{document}

\begin{figure}[!htp]

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % first row
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{subfigure}{0.22\textwidth}
        \includegraphics[width=\textwidth]{example-image}
        \caption{First subf}
    \end{subfigure}\hfill
    %%
    \begin{subfigure}{0.22\textwidth}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Second subf}
    \end{subfigure}\hfill
    %%
    \begin{subfigure}{0.22\textwidth}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Third subf}
    \end{subfigure}\hfill
    %%
    \begin{subfigure}{0.22\textwidth}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Fourth subf}
    \end{subfigure}

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % second row
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{subfigure}{0.22\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Fifth subf}
    \end{subfigure}\hfill
    %%
    \begin{subfigure}{0.22\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Sixth subf}
    \end{subfigure}\hfill
    %%
    \begin{subfigure}{0.22\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Seventh subf}
    \end{subfigure}\hfill
    %%
    \begin{subfigure}{0.22\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Eighth subf}
    \end{subfigure}

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % third row
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{subfigure}{0.35\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Tenth subf}
    \end{subfigure}\hspace{0.04\textwidth}% (1-4*0.22)/3
    \begin{subfigure}{0.35\textwidth}
        \vspace{10pt}
        \includegraphics[width=\textwidth]{example-image}
        \caption{Eleventh subf}
    \end{subfigure}

    \caption{Subreferences in \LaTeX.}
    \label{fig:figures}

\end{figure}

\end{document}

4 Answers4

5

zref's savepos module allows you to capture the x-coordinates of a point on a page via \zsaveposx{<label>}. This <label> (or marker) can then be used to calculate distances or offsets from other page elements. Extracting the x-coordinate is done via \zposx{<label>}, which is expandable; it returns a number, which should be converted to scaled points (so \zposx{<label>}sp represents a dimension one can work with).

In the example below, I set a SubfigGapL at the right edge of subfigure (e) and SubfigGapR at the left edge of subfigure (f). The difference in their x-coordinates (noting that SubfigGapR will have a larger x-coordinate than SubfigGapL: \dimexpr\zposx{SubfigGapR}sp-\zposx{SubfigGapL}sp).

enter image description here

\documentclass{article}

\usepackage{subcaption} \usepackage{graphicx} \usepackage[savepos]{zref}

\begin{document}

\begin{figure}[h!] %%%%%%%%%%%%%%%%%%%%%%%%%%%% % first row %%%%%%%%%%%%%%%%%%%%%%%%%%%% \subcaptionbox{First subf}[.22\linewidth][c]{% \includegraphics[width=\linewidth]{example-image}}% \hfill \subcaptionbox{Second subf}[.22\linewidth][c]{% \includegraphics[width=\linewidth]{example-image}}% \hfill \subcaptionbox{Third subf}[.22\linewidth][c]{% \includegraphics[width=\linewidth]{example-image}}% \hfill \subcaptionbox{Fourth subf}[.22\linewidth][c]{% \includegraphics[width=\linewidth]{example-image}}%

\addvspace{10pt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%% % second row %%%%%%%%%%%%%%%%%%%%%%%%%%%% \subcaptionbox{Fifth subf}[.22\linewidth][c]{% \includegraphics[width=\linewidth]{example-image}}% \zsaveposx{SubfigGapL}% Save x-coordinate at right edge of image \hfill \zsaveposx{SubfigGapR}% Save x-coordante at left edge of image \subcaptionbox{Sixth subf}[.22\linewidth][c]{% \includegraphics[width=\linewidth]{example-image}}% \hfill \subcaptionbox{Seventh subf}[.22\linewidth][c]{% \includegraphics[width=\linewidth]{example-image}}% \hfill \subcaptionbox{Eighth subf}[.22\linewidth][c]{% \includegraphics[width=\linewidth]{example-image}}%

\addvspace{10pt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%% % third row %%%%%%%%%%%%%%%%%%%%%%%%%%%% \mbox{}\hfill \subcaptionbox{Tenth subf}[0.3933\linewidth][c]{% \includegraphics[width=\linewidth]{example-image}}% % Gap exactly the same as gap between above image(s) \hspace{\dimexpr\zposx{SubfigGapR}sp-\zposx{SubfigGapL}sp}% \subcaptionbox{Eleventh subf}[0.39333\linewidth][c]{% \includegraphics[width=\linewidth]{example-image}}% \hfill \mbox{}

\caption{Subreferences in \LaTeX.} \end{figure}

\end{document}

I changed the input slightly, using the more convenient \subcaptionbox{<caption>}[<width>][<alignment>]{<stuff>} notation. But you can also achieve this with the subfigure environment.

Since \zsaveposx uses TeX's \label-\ref system to store/retrieve coordinates, you'll need to compile twice with every change of the coordinate locations (including on the first compilation).

Werner
  • 603,163
  • Couldn't one just argue that because the graphs in rows 1 and 2 each have width 0.22\textwidth, the space between them must be 0.04\textwidth, as (1-4*0.22)/3=0.04. Hence, just insert \hspace{0.04pt} between subfigures i and j, and continue to insert \hfill to the left of subfigure i and \hfill\null to the right of subfigure j. (\null is defined in the LaTeX kernerl as \mbox{}.) – Mico Feb 11 '23 at 10:56
  • 1
    @Mico: Nice, and yes. My approach is more general through measuring (if, for example, you didn't know the size of the images). – Werner Feb 11 '23 at 16:23
5

Like this:

enter image description here

Code:

\documentclass{article}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage[most]{tcolorbox}

\begin{document}
\thispagestyle{empty}
\begin{tcbitemize}[raster columns=4,colback=white,colframe=white ,no borderline] \tcbitem \includegraphics[width=\linewidth]{example-image} \tcbitem \includegraphics[width=\linewidth]{example-image} \tcbitem \includegraphics[width=\linewidth]{example-image} \tcbitem \includegraphics[width=\linewidth]{example-image} \tcbitem \includegraphics[width=\linewidth]{example-image} \tcbitem \includegraphics[width=\linewidth]{example-image} \tcbitem \includegraphics[width=\linewidth]{example-image} \tcbitem \includegraphics[width=\linewidth]{example-image} \end{tcbitemize} \begin{tcbitemize}[raster columns=2,colback=white,colframe=white ,no borderline] \tcbitem \includegraphics[width=\linewidth]{example-image} \tcbitem \includegraphics[width=\linewidth]{example-image} \end{tcbitemize}
\end{document}

ADD: If you want a figure environnement (for reference) and subfigures (manual letter for each), with some spice style consider this code:

\documentclass{article}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage[most]{tcolorbox}

\begin{document}
\thispagestyle{empty}
\begin{figure}[h]
\begin{tcbitemize}[raster columns=4,colback=cyan!20,colframe=cyan] \tcbitem \includegraphics[width=\linewidth]{example-image} Sub (a) \tcbitem \includegraphics[width=\linewidth]{example-image} (b) \tcbitem \includegraphics[width=\linewidth]{example-image} (b) \tcbitem \includegraphics[width=\linewidth]{example-image} (d) \tcbitem \includegraphics[width=\linewidth]{example-image} (e) \tcbitem \includegraphics[width=\linewidth]{example-image} (f) \tcbitem \includegraphics[width=\linewidth]{example-image} (g) \tcbitem \includegraphics[width=\linewidth]{example-image} (h) \end{tcbitemize} \begin{tcbitemize}[raster columns=2,colback=white,colframe=cyan] \tcbitem \includegraphics[width=\linewidth]{example-image} (i) \tcbitem \includegraphics[width=\linewidth]{example-image} (l) \end{tcbitemize} \caption{A set of figures} \end{figure}
\end{document}

New output:

enter image description here

5

If you would like to keep the sizes of the final two graphs to be the same as of those of the first eight, and if you would like to center the two final graphs horizontally, the following solution may be of interest. (See below for a solution that lets the final two charts be considerably larger than the first eight.) Because the width of each subfigure in rows 1 and 2 is 0.22\textwidth, the width of the space between the subfigures works out 0.04\textwidth -- 0.04 = (1-4*0.22)/3. The ninth subfigure therefore needs to be shifted 0.26\textwidth (0.22+0.04=0.26) to the right. Do continue to use \hspace{0.04\textwidth} as the spacer between subfigures i and j.

Note that I've also removed the \vspace directives from the ten subfigure environments and, instead, inserted them at the start of rows 2 and 3.

enter image description here

\documentclass{article}
\usepackage{subcaption,graphicx}
\begin{document}

\begin{figure}[h!]

    %%% first row

    \begin{subfigure}{0.22\textwidth}
    \includegraphics[width=\linewidth]{example-image}
    \caption{First subf}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.22\textwidth}
      \includegraphics[width=\linewidth]{example-image}
      \caption{Second subf}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.22\textwidth}
      \includegraphics[width=\linewidth]{example-image}
      \caption{Third subf}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.22\textwidth}
      \includegraphics[width=\linewidth]{example-image}
      \caption{Fourth subf}
    \end{subfigure}%


    %%% second row

    \vspace{10pt} % &lt;-- new
    \begin{subfigure}{0.22\textwidth}
      \includegraphics[width=\linewidth]{example-image}
      \caption{Fifth subf}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.22\textwidth}
      \includegraphics[width=\linewidth]{example-image}
      \caption{Sixth subf}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.22\textwidth}
      \includegraphics[width=\linewidth]{example-image}
      \caption{Seventh subf}
    \end{subfigure}%
    \hfill
    \begin{subfigure}{0.22\textwidth}
      \includegraphics[width=\linewidth]{example-image}
      \caption{Eighth subf}
    \end{subfigure}


    %%% third row

    \vspace{10pt} % &lt;-- new
    \hspace{0.26\textwidth}%
    \begin{subfigure}{0.22\textwidth}
      \includegraphics[width=\linewidth]{example-image}
      \caption{Ninth subf}
    \end{subfigure}%
    \hspace{0.04\textwidth}%
    \begin{subfigure}{0.22\textwidth}
      \includegraphics[width=\linewidth]{example-image}
      \caption{Tenth subf}
    \end{subfigure}

\caption{Subfigures in \LaTeX.} \label{fig:figures} \end{figure}

\end{document}


Addendum: If you do want the two final subfigures to have width 0.4\textwidth instead of 0.22\textwidth, I suggest you change the code for the third row as follows (the code for the first two rows needn't be changed):

    %%% third row, chart widths '0.4\textwidth'
\vspace{10pt}
\hfill 
\begin{subfigure}{0.4\textwidth}
  \includegraphics[width=\linewidth]{example-image}
  \caption{Ninth subf}
\end{subfigure}%
\hspace{0.04\textwidth}%
\begin{subfigure}{0.4\textwidth}
  \includegraphics[width=\linewidth]{example-image}
  \caption{Tenth subf}
\end{subfigure}%
\hfill\null 


Yet another thought: If you want the each of the final two subfigures to span the widths of the pairs of the two smaller charts above them, including the whitespace gap that separates the smaller graphs, all you have to change is set the widths of final two charts to 0.48\textwidth. Why? Because 2*0.22+0.04=0.48.

    %%% third row, chart widths '0.48\textwidth'
\vspace{10pt} 
\begin{subfigure}{0.48\textwidth} % 2*0.22+0.04=0.48
  \includegraphics[width=\linewidth]{example-image}
  \caption{Ninth subf}
\end{subfigure}%
\hfill
\begin{subfigure}{0.48\textwidth}
  \includegraphics[width=\linewidth]{example-image}
  \caption{Tenth subf}
\end{subfigure}

The resulting 10-panel figure will the look like this:

enter image description here

Mico
  • 506,678
2

You do know what's the needed space, namely 0.04\textwidth.

You're adding unwanted spaces, though (in front of all \hfill commands, to be precise).

For left alignment of the last row just add \hspace{0.04\textwidth} between the two images.

\documentclass{article}

\usepackage{subcaption} \usepackage{graphicx}

\begin{document}

\begin{figure}[!htp]

%%%%%%%%%%%%%%%%%%%%%%%%%%%% % first row %%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{subfigure}{0.22\textwidth} \includegraphics[width=\textwidth]{example-image} \caption{First subf} \end{subfigure}\hfill %% \begin{subfigure}{0.22\textwidth} \includegraphics[width=\textwidth]{example-image} \caption{Second subf} \end{subfigure}\hfill %% \begin{subfigure}{0.22\textwidth} \includegraphics[width=\textwidth]{example-image} \caption{Third subf} \end{subfigure}\hfill %% \begin{subfigure}{0.22\textwidth} \includegraphics[width=\textwidth]{example-image} \caption{Fourth subf} \end{subfigure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%% % second row %%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{subfigure}{0.22\textwidth} \vspace{10pt} \includegraphics[width=\textwidth]{example-image} \caption{Fifth subf} \end{subfigure}\hfill %% \begin{subfigure}{0.22\textwidth} \vspace{10pt} \includegraphics[width=\textwidth]{example-image} \caption{Sixth subf} \end{subfigure}\hfill %% \begin{subfigure}{0.22\textwidth} \vspace{10pt} \includegraphics[width=\textwidth]{example-image} \caption{Seventh subf} \end{subfigure}\hfill %% \begin{subfigure}{0.22\textwidth} \vspace{10pt} \includegraphics[width=\textwidth]{example-image} \caption{Eighth subf} \end{subfigure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%% % third row %%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{subfigure}{0.39333333333\textwidth} \vspace{10pt} \includegraphics[width=\textwidth]{example-image} \caption{Tenth subf} \end{subfigure}\hspace{0.04\textwidth}% (1-4*0.22)/3 \begin{subfigure}{0.39333333333\textwidth} \vspace{10pt} \includegraphics[width=\textwidth]{example-image} \caption{Eleventh subf} \end{subfigure}

\caption{Subreferences in \LaTeX.} \label{fig:figures}

\end{figure}

\end{document}

enter image description here

If you want to center the last row, add \hspace*{\fill} in front of the left image and at the end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% third row
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\hspace*{\fill}%
\begin{subfigure}{0.39333333333\textwidth}
    \vspace{10pt}
    \includegraphics[width=\textwidth]{example-image}
    \caption{Tenth subf}
\end{subfigure}\hspace{0.04\textwidth}% (1-4*0.22)/3
\begin{subfigure}{0.39333333333\textwidth}
    \vspace{10pt}
    \includegraphics[width=\textwidth]{example-image}
    \caption{Eleventh subf}
\end{subfigure}\hspace*{\fill}

enter image description here

egreg
  • 1,121,712