LaTeX forum ⇒ Graphics, Figures & TablesFigures at the same line with minimum white space between them

Information and discussion about graphics, figures & tables in LaTeX documents.
alexandrosv
Posts: 1
Joined: Fri Jul 16, 2021 11:34 pm

Figures at the same line with minimum white space between them

Postby alexandrosv » Fri Jul 16, 2021 11:41 pm

I have two figures and want to keep them at the same line but with the minimum white space between them. My code is as follows:
\begin{figure}[H]
    \centering
    \begin{subfigure}[b]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{Office.png}
        \caption{}
        \label{fig:IndoorTempOffice}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.45\textwidth}
        \centering
        \includegraphics[width=\textwidth]{Apartment.png}
        \caption{}
        \label{fig:IndoorTempApartment}
    \end{subfigure}
        \caption{This is a caption}
        \label{fig:IndoorTemp}
\end{figure}
Last edited by Stefan Kottwitz on Sat Jul 17, 2021 1:30 pm, edited 1 time in total.
Reason: code marked

Bartman
Posts: 211
Joined: Fri Jan 03, 2020 2:39 pm

Postby Bartman » Sat Jul 17, 2021 1:35 am

I miss a Infominimal working example and the marking of the source code that allows you and others to test the example immediately.

Use the minipage environment instead of the option H of the float package if you don't want the images to float.

I doubt if I have understood your concern correctly, but you could simply remove the \hfill command.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{showframe}

\begin{document}
\begin{center}
\begin{minipage}{\textwidth}
  % The command isn't documented in the manual, 
  % but you can find it in the changelog file.
  \setcaptiontype{figure}
  \centering
  \begin{subfigure}[b]{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth]{example-image-a}
    \caption{}
    \label{fig:IndoorTempOffice}
  \end{subfigure}
%  \hfill
  \begin{subfigure}[b]{0.45\textwidth}
    \centering
    \includegraphics[width=\textwidth]{example-image-b}
    \caption{}
    \label{fig:IndoorTempApartment}
  \end{subfigure}
  \caption{This is a caption}
  \label{fig:IndoorTemp}
\end{minipage}
\end{center}
\end{document}

User avatar
Ijon Tichy
Posts: 526
Joined: Mon Dec 24, 2018 10:12 am

Postby Ijon Tichy » Sat Jul 17, 2021 11:11 am

And if “minimum white space” means no white space, also add a % immediately behind \end{subfigure} so avoid the white space represented by the line end:
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{showframe}

\begin{document}
\begin{center}
\begin{minipage}{\textwidth}
  \captionsetup{type=figure}% This is documented in the manual, section 2.9.
  \centering
  \begin{subfigure}[b]{0.45\textwidth}% Can be increased up to .5\textwidth now.
    \centering
    \includegraphics[width=\textwidth]{example-image-a}
    \caption{}
    \label{fig:IndoorTempOffice}
  \end{subfigure}%<-- added
%  \hfill
  \begin{subfigure}[b]{0.45\textwidth}% Can be increased up to .5\textwidth now.
    \centering
    \includegraphics[width=\textwidth]{example-image-b}
    \caption{}
    \label{fig:IndoorTempApartment}
  \end{subfigure}
  \caption{This is a caption}
  \label{fig:IndoorTemp}
\end{minipage}
\end{center}
\end{document}


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 9 guests