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

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

Figures at the same line with minimum white space between them

Post by alexandrosv »

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:

Code: Select all

\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

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

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

Figures at the same line with minimum white space between them

Post by Bartman »

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.

Code: Select all

\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: 640
Joined: Mon Dec 24, 2018 10:12 am

Figures at the same line with minimum white space between them

Post by Ijon Tichy »

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:

Code: Select all

\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}
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply