1

I am using the minipage environment to align four figures horizontally:

\usepackage[]{caption}
\usepackage[subrefformat=parens]{subcaption}
\captionsetup{compatibility=false}

\begin{figure}
    \begin{minipage}[t]{0.24\linewidth}
        \centering 
        \includegraphics[width=0.95\linewidth]
        {Figures/fig1.png} 
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.24\linewidth}
        \centering 
        \includegraphics[width=0.95\linewidth]
        {Figures/fig2.png}
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.24\linewidth}
        \centering 
        \includegraphics[width=0.95\linewidth]
        {Figures/fig3.png}
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.24\linewidth}
        \centering 
        \includegraphics[width=0.95\linewidth]
        {Figures/fig4.png}
        \subcaption{}
    \end{minipage}
\end{figure}

I'd like to shift these four images to the left by several pixels. I tried two methods below but neither of them worked:

  1. Replacing \centering with \raggedright.
  2. Adding a space (i.e.\bigskip) after each figure inside of minipage.

Any idea how to achieve this?

EDIT:

I apologize for causing confusion. What I really want to do is to adjust the position of figures to make them look more "aligned". Here is what my plots look like:

enter image description here

The figures at the top are cameraman images with color bars, and the figures at the bottom are some plots. They are all aligned in the center. However, because the cameraman images have color bars, they look as if they were a little bit off from the plots at the bottom. I want to adjust their position horizontally to get something like the below:

enter image description here

3 Answers 3

1

Using tabular with columns of the same size, left alignment, and manual offsets would be another approach to achieve what you need, even an effect of centring; additional image components can be included/excluded in offsets.

As I don't have your image files, the code is slightly longer because I draw extra bars in order to show how offsetting can work. You just need to use \includegraphics with proper size. The subcaptiongroup is the environment in which each \caption is turned into sub caption, you can later refer to. \setkeys is a convenient macro to avoid unnecessary repetitions.

Using `tabular` with columns of the same size, left alignment, and manual offsets would be another approach to achieve what you need, even an effect of centring; additional image components can be included/excluded in offsets.

As I don't have your image files, the code is slightly longer because I draw extra bars in order to show how  offsetting can work. You just need to use `\includegraphics` with proper size. The `subcaptiongroup` is the environment in which each `\caption` is turned into sub caption, you can later refer to. `\setkeys` is a convenient macro to avoid unnecessary repetitions.

```latex
\documentclass{article}
\usepackage{array}
\usepackage{graphicx}
\usepackage[subrefformat=parens]{subcaption}
\captionsetup{compatibility=false}

\newlength\offset

\usepackage{xcolor}

\begin{document}
\begin{figure}[tbh]
  \setlength\tabcolsep{0pt}
  \begin{subcaptiongroup}
    \setkeys{Gin}{width=2.2cm,height=1.5cm}
    \setlength\offset{0.5\dimexpr0.25\textwidth-2.2cm}
    \begin{tabular*}{\linewidth}{
      *4{>{\hspace{\offset}}p{0.25\linewidth}}
      }
      \includegraphics{example-image}%
      \textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
      & \includegraphics{example-image-a}%
        \textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
      & \includegraphics{example-image-b}%
        \textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
      & \includegraphics{example-image-c}%
        \textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
    \end{tabular*}
    
    \setlength\offset{0.5\dimexpr0.25\textwidth-2cm}
    \setkeys{Gin}{width=2cm,height=1.5cm}
    \begin{tabular*}{\linewidth}{
      *4{>{\hspace{\offset}}p{0.25\linewidth}}
      }
      \includegraphics{example-image}\caption{}
      & \includegraphics{example-image-a}\caption{}
      & \includegraphics{example-image-b}\caption{}
      & \includegraphics{example-image-c}\caption{}
    \end{tabular*}
  \end{subcaptiongroup}
  \caption{The main caption}
\end{figure}
\end{document}

enter image description here

EDIT.
If you want to change spacing between images, subcaptions and the main captions, use \captionsetup[]{}. If you you want to reduce spacing between tow lines, you have to change both skip and belowskip.
An optional argument if used only affect a certain group, e.g. subfigure. It's convenient to use \captionsetup{} first for some general settings followed by \captionsetup[...]{} for specific settings only related to a certain group. For instance:

\captionsetup{skip=-3pt,position=bottom}

will reduce distance between all (sub)captions and images and set position to the bottom. On the other hand, the following line

\captionsetup[subfigure]{belowskip=-6pt}

will only affect subfigures leaving the "other" settings intact.

Here's the updated the full code

\documentclass{article}
\usepackage{array}
\usepackage{graphicx}
\usepackage[subrefformat=parens]{subcaption}
\captionsetup{position=bottom,skip=0pt}
\captionsetup[subfigure]{belowskip=-6pt}

\newlength\offset

\usepackage{xcolor}


\begin{document}
\begin{figure}[tbh]
  \setlength\tabcolsep{0pt}
  \begin{subcaptiongroup}
    \setkeys{Gin}{width=2.2cm,height=1.5cm}
    \setlength\offset{0.5\dimexpr0.25\textwidth-2.2cm}
    \begin{tabular*}{\linewidth}{
      *4{>{\hspace{\offset}}p{0.25\linewidth}}
      }
      \includegraphics{example-image}%
      \textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
      & \includegraphics{example-image-a}%
        \textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
      & \includegraphics{example-image-b}%
        \textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
      & \includegraphics{example-image-c}%
        \textcolor{gray!30}{\rule{3.5mm}{1.5cm}}\caption{}
    \end{tabular*}
    
    \setlength\offset{0.5\dimexpr0.25\textwidth-2cm}%
    \setkeys{Gin}{width=2cm,height=1.5cm}%
    \begin{tabular*}{\linewidth}{
      *4{>{\hspace{\offset}}p{0.25\linewidth}}
      }
      \includegraphics{example-image}\caption{}
      & \includegraphics{example-image-a}\caption{}
      & \includegraphics{example-image-b}\caption{}
      & \includegraphics{example-image-c}\caption{}
    \end{tabular*}
  \end{subcaptiongroup}
  \caption{The main caption}
\end{figure}
\end{document}

and the screenshot

enter image description here

2
  • Is it possible to remove the vertical space between two tables? I tried using \vspace{-1cm} but it didn't work properly.
    – Ogiad
    Jan 3, 2023 at 18:37
  • 1
    @Ogiad Yes, it is possible. \captionsetup[]{} is the macro you should use to control spacing between (sub)captions and images. See my edited answer.
    – Celdor
    Jan 3, 2023 at 23:36
2

I'd like to shift these four images to the left by several pixels.

I'm assuming that what you really want to achieve is to spread out the subfigures so that they occupy the full width of the textblock. This may be achieved by inserting \hfill between the minipage or subfigure environments. Note that a subfigure environment is nothing but a minipage that knows what to do when it encounters a \caption (or \subcaption) directive.

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage{showframe}  % draw frame around text block
\usepackage[demo]{graphicx} % remove 'demo' option in real document
\usepackage[skip=0.333\baselineskip]{caption}
\usepackage[subrefformat=parens]{subcaption}
\captionsetup{compatibility=false}

\begin{document}
\null

\begin{figure}[h]
    \begin{minipage}[t]{0.24\linewidth}
        \centering 
        \includegraphics[width=0.95\linewidth]{Figures/fig1.png} 
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.24\linewidth}
        \centering 
        \includegraphics[width=0.95\linewidth]{Figures/fig2.png}
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.24\linewidth}
        \centering 
        \includegraphics[width=0.95\linewidth]{Figures/fig3.png}
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.24\linewidth}
        \centering 
        \includegraphics[width=0.95\linewidth]{Figures/fig4.png}
        \subcaption{}
    \end{minipage}
    \caption{Before}
\end{figure}

\begin{figure}[h]
    \begin{subfigure}[t]{0.24\linewidth}
        \includegraphics[width=1\linewidth]{Figures/fig1.png} 
        \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.24\linewidth}
        \includegraphics[width=1\linewidth]{Figures/fig2.png}
        \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.24\linewidth}
        \includegraphics[width=1\linewidth]{Figures/fig3.png}
        \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.24\linewidth}
        \includegraphics[width=1\linewidth]{Figures/fig4.png}
        \caption{}
    \end{subfigure}
    \caption{After}
\end{figure}

\end{document}
2

UPDATED after the question edit.

I would like to achieve two goals: (1) center each "row" in the text area, and (2) align the four "columns".

In the first figure, the "columns" are aligned but the rows are not horizontally centered on the page. This is usually achieved by inserting a \hfill between the minipages.

b

But as you can see in the second figure when the form factor of the images is different, the columns are not aligned. The effect is most noticeable when the width of four minipages is less than 1/4 of the linewidth.

c

The last figure has the rows centered and the columns aligned. This was achieved by using the code from the first figure and centering each row individually

d

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}

\usepackage{subcaption}

\usepackage{showframe} % show margins
\begin{document}
    
\begin{figure}[hbt!]

\begin{minipage}[t]{0.22\linewidth} 
        \centering 
        \includegraphics[width=0.6\linewidth,height=0.6\linewidth]{example-image-a} 
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth,height=0.6\linewidth]
        {example-image-a}
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth,height=0.6\linewidth]
        {example-image-a}
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth,height=0.6\linewidth]
        {example-image-a}
        \subcaption{}
    \end{minipage}
    \smallskip

    \begin{minipage}[t]{0.22\linewidth} 
        \centering 
        \includegraphics[width=0.8\linewidth]{example-image-b} 
        \subcaption{}
    \end{minipage}
        %
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.8\linewidth]{example-image-b}
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.8\linewidth]{example-image-b}
        \subcaption{}
    \end{minipage}
    %
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.8\linewidth]
        {example-image-b}
        \subcaption{}
    \end{minipage}

    \caption{Align, row not centered}
\end{figure}


\begin{figure}[hbt!]
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth,height=0.6\linewidth]{example-image-a} 
        \subcaption{}
    \end{minipage}
    \hfill % added <<<<<
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth,height=0.6\linewidth]
        {example-image-a}
        \subcaption{}
    \end{minipage}
    \hfill % added <<<<<
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth,height=0.6\linewidth]
        {example-image-a}
        \subcaption{}
    \end{minipage}
    \hfill % added <<<<<
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth,height=0.6\linewidth]
        {example-image-a}
        \subcaption{}
    \end{minipage}  
    \smallskip

    \begin{minipage}[t]{0.22\linewidth} % changed <<<<<<<
        \centering 
        \includegraphics[width=0.8\linewidth]{example-image-b} 
        \subcaption{}
    \end{minipage}
    \hfill % added <<<<<
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.8\linewidth]{example-image-b}
        \subcaption{}
    \end{minipage}
    \hfill % added <<<<<
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.8\linewidth]{example-image-b}
        \subcaption{}
    \end{minipage}
    \hfill % added <<<<<
    \begin{minipage}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.8\linewidth]
        {example-image-b}
        \subcaption{}
    \end{minipage}  % changed <<<<<<<

    \caption{Not aligned but row centered using hfill}
\end{figure}


\begin{figure}[hbt!]
    \hfill\mbox{\begin{minipage}[t]{0.22\linewidth} % changed <<<<<<<
            \centering 
            \includegraphics[width=0.6\linewidth,height=0.6\linewidth]{example-image-a} 
            \subcaption{}
        \end{minipage}
        %   
        \begin{minipage}[t]{0.22\linewidth}
            \centering 
            \includegraphics[width=0.6\linewidth,height=0.6\linewidth]
            {example-image-a}
            \subcaption{}
        \end{minipage}
        %   
        \begin{minipage}[t]{0.22\linewidth}
            \centering 
            \includegraphics[width=0.6\linewidth,height=0.6\linewidth]
            {example-image-a}
            \subcaption{}
        \end{minipage}
        %   
        \begin{minipage}[t]{0.22\linewidth}
            \centering 
            \includegraphics[width=0.6\linewidth,height=0.6\linewidth]
            {example-image-a}
            \subcaption{}
        \end{minipage}}\hfill % changed <<<<<<<
        \smallskip
    
    \hfill\mbox{\begin{minipage}[t]{0.22\linewidth} % changed <<<<<<<
            \centering 
            \includegraphics[width=0.8\linewidth]{example-image-b} 
            \subcaption{}
        \end{minipage}
        %   
        \begin{minipage}[t]{0.22\linewidth}
            \centering 
            \includegraphics[width=0.8\linewidth]{example-image-b}
            \subcaption{}
        \end{minipage}
        %
        \begin{minipage}[t]{0.22\linewidth}
            \centering 
            \includegraphics[width=0.8\linewidth]{example-image-b}
            \subcaption{}
        \end{minipage}
        %   
        \begin{minipage}[t]{0.22\linewidth}
            \centering 
            \includegraphics[width=0.8\linewidth]
            {example-image-b}
            \subcaption{}
    \end{minipage}}\hfill % changed <<<<<<<
    \smallskip
    
    \caption{Aligned and centered}
\end{figure}    

\end{document}

Finally, using Mico`s indication (using the subfigure environment) simplifies the code.

e

\begin{figure}[h]

    \begin{subfigure}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth]{example-image-a} 
        \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth]{example-image-a}
        \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth]{example-image-a}
        \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.6\linewidth]{example-image-a}
        \caption{}
    \end{subfigure}
    
    
    \begin{subfigure}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.8\linewidth]{example-image-b} 
        \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.8\linewidth]{example-image-b}
        \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.8\linewidth]{example-image-b}
        \caption{}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.22\linewidth}
        \centering 
        \includegraphics[width=0.8\linewidth]{example-image-b}
        \caption{}
    \end{subfigure}
    \caption{Using subfigure}
\end{figure}
1
  • 1
    I think that the OP wrote that he/she wants to shift the images to the left.
    – Mico
    Dec 24, 2022 at 20:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .