0

Searching this forum, there are many similar questions but I haven't found any addressing my particular issue. Is there an elegant way to centre two horizontally aligned floats(figures) of different widths, so that the distances between the borders of the document and the images are the same? In the picture below I've marked the three distances by "x", "y" and "z". Two acceptable solutions to this would be:

  • x = y = z,
  • x = z and y is arbitrarily defined (for ex. with \quad).

enter image description here

The above figure is produced using the code:

\usepackage[inner=25mm,
            outer=25mm,
            top=30mm,
            bottom=25mm]{geometry}
\usepackage{caption}
\usepackage{subcaption}
----------------------------------------------------------
\begin{figure}[H]
    \label{fig:two_figs}
        \begin{subfigure}[b]{0.5\linewidth}
            \centering
            \includegraphics[width=4cm]{example-image-a}
            \caption{}
            \label{subfig:image_a}
        \end{subfigure}%
        \begin{subfigure}[b]{0.5\linewidth}
            \centering
            \includegraphics[width=8cm]{example-image-b}
            \caption{}
            \label{subfig:image_b}
        \end{subfigure}%
\caption{Complete caption}
\end{figure}

Granted, this can be approximately centred, for example, by changing the first subfigure width to 0.41\linewidth and the second to 0.3\linewidth. However, this seems rather unprecise and can be tedious if one has many such "double-figures" in the document.

Also please keep in mind that the \includegraphics width should not be changed in any way as I always import my figures with scale=1 to assure figure text size compliance with that of the document. In the above example, I used an arbitrary width to get my point across.

2
2

Here are your two requests for spacing:

enter image description here

\documentclass{article}

\usepackage[margin=1in,showframe]{geometry}
\usepackage{subcaption,graphicx}

\begin{document}

\begin{figure}
  % Equal distribution of space between two subfigures (x = y = z)
  \hfill
  \subcaptionbox{\label{subfig:image_a}}{%
    \includegraphics[width=4cm]{example-image-a}%
  }\hfill
  \subcaptionbox{\label{subfig:image_b}}{%
    \includegraphics[width=8cm]{example-image-b}%
  }%
  \hfill\mbox{}%
  \caption{Complete caption}
\end{figure}

\begin{figure}
  % Equal distribution of space outside of subfigures with fixed inner separation (x = y)
  \hfill
  \subcaptionbox{\label{subfig:image_a}}{%
    \includegraphics[width=4cm]{example-image-a}%
  }\qquad%
  \subcaptionbox{\label{subfig:image_b}}{%
    \includegraphics[width=8cm]{example-image-b}%
  }%
  \hfill\mbox{}%
  \caption{Complete caption}
\end{figure}

\end{document}

The main approach uses \subcaptionbox without the (optional) width parameter and using \hfills to spread out the content. An end-of-line \mbox{} ensures there's an anchor against which to spread to. If the caption needs some more horizontal room, you can add equal amounts of \hspace around the \includegraphics, or specify a fixed width.

1

Here's a solution that places \hfill directives on all sides of and between the subfigure environments. It also sets the desired (relative) widths of the subfigures directly, while specifying the (relative) widths of the graphs via width=1\linedwidth.

The framelins in the following screenshot denote the left-hand and right-hand edges of the text block.

enter image description here

\documentclass{article}
\usepackage[inner=25mm, outer=25mm,top=30mm, bottom=25mm]{geometry}
\usepackage{subcaption,graphicx,showframe}
\begin{document}
\begin{figure}
        \hfill
        \begin{subfigure}[b]{0.3\linewidth}
            \includegraphics[width=\linewidth]{example-image-a}
            \caption{}
            \label{subfig:image_a}
        \end{subfigure}%
        \hfill
        \begin{subfigure}[b]{0.5\linewidth}
            \includegraphics[width=\linewidth]{example-image-b}
            \caption{}
            \label{subfig:image_b}
        \end{subfigure}%
        \hfill\null
\caption{Complete caption} \label{fig:two_figs}
\end{figure}
\end {document}
3
  • Correct me if I'm wrong, but this only works if you have foreknowledge of the figure size (ie. width).
    – KeVal
    Jan 3 at 21:36
  • @KeVal - "Correct me if I'm wrong". Sorry, but your assessment is indeed wrong. No knowledge of either the "natural" figure dimensions or of the actual width of the text block is required for this solution. \linewidth is a length parameter that denotes (by default) the width of the text block.; as long as \linewidth is a length greater than 0 -- which will be the case for any nontrivial document -- the solution is guaranteed to work. In contrast, if one sets absolute figure widths, there's no guarantee whatsoever that the figures will actually fit inside the text block.
    – Mico
    Jan 3 at 21:42
  • @KeVal - My solution method gives you a choice over the relative image widths. In the answer above, I somewhat arbitrarily set 0.3\linewidth and 0.5\linewidth, for a ratio of 3 to 5. Any other ratio is fine too. Of course, the sum of the (relative) widths must not exceed 1\linewidth; that's not a burdensome contstaint though, is it?
    – Mico
    Jan 3 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.