0

Below I have 4 subfigures, but I have a problem if the third subfigure text exceeded one line, the 4th figure will shift down a little bit as you see below. Not sure how to fix this please?

enter image description here

\documentclass[conference]{IEEEtran}
\IEEEoverridecommandlockouts
% The preceding line is only needed to identify funding in the first footnote. If that is unneeded, please comment it out.
\usepackage{cite}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{tabularx}
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

\title{AlgorithmTemplate} % \documentclass[12pt]{article} \usepackage{fullpage} \usepackage{times} \usepackage{fancyhdr} \usepackage[ruled,vlined]{algorithm2e} \include{pythonlisting}

\begin{document}

sa

\begin{figure} \centering \begin{subfigure}[b]{0.45\linewidth} \centering \includegraphics[width=\linewidth]{testfig.png} \caption{a} \label{fig: a} \end{subfigure} \hfill \begin{subfigure}[b]{0.45\linewidth} \centering \includegraphics[width=\linewidth]{testfig.png} \caption{b } \label{fig:b} \end{subfigure} \hfill \begin{subfigure}[b]{0.45\linewidth} \centering \includegraphics[width=\linewidth]{testfig.png} \caption{I have some text here that will shift the 4th figure to bottom} \label{fig:c} \end{subfigure} \hfill \begin{subfigure}[b]{0.45\linewidth} \centering \includegraphics[width=\linewidth]{testfig.png} \caption{d} \label{fig:d} \end{subfigure} \caption{d} \label{fig:three graphs} \end{figure}

\end{document}

Avv
  • 157
  • Change all [b]s to [t] at subfigures. Also, remove the middle \hfill and leave an empty space. This will create a new paragraph and move other two figures to the next line. You might need to add an extra vertical space, e.g. \bigskip or \vspace{v-sep}, where vsep could any length s.a. 12pt, 1cm etc. – Celdor Oct 30 '22 at 19:04
  • Note that [t] means the first baseline, which is at the bottom of the image. You can use \hfill\llap{\rule{\textwdith}{0.4pt}} to show the baseline location. – John Kormylo Oct 30 '22 at 20:34
  • @JohnKormylo. Thanks. I got error, Undefined control sequence. \hfill\llap{\rule{\textwdith}{0.4pt}}. Where I should add this please? Should I add it just before \end{figure}? – Avv Oct 31 '22 at 12:44
  • 1
    Sorry, typo. wdith should be width. – John Kormylo Oct 31 '22 at 12:55

1 Answers1

2

Change the bottom alignment [b] to the top [t] at each \subfigure. Then, if you want to have a grid of 4x4 images, remove the middle \hfill and add an empty space. This is will move the other 2 figures to the next line. You might need to increase a vertical spacing. Therefore, use \vspace{...} with any length s.a. 12pt, 1cm etc. or \bigskip.

enter image description here

\documentclass[conference]{IEEEtran}
\IEEEoverridecommandlockouts
% The preceding line is only needed to identify funding in the first footnote. If that is unneeded, please comment it out.
\usepackage{cite}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{tabularx}
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

\title{AlgorithmTemplate} % \documentclass[12pt]{article} \usepackage{fullpage} \usepackage{times} \usepackage{fancyhdr} \usepackage[ruled,vlined]{algorithm2e} \include{pythonlisting}

\usepackage{kantlipsum}

\begin{document} \kant[1]

\begin{figure}[tbh] \centering \begin{subfigure}[t]{0.45\linewidth} \centering \includegraphics[width=\linewidth]{example-image} \caption{a} \label{fig: a} \end{subfigure}% \hfill% \begin{subfigure}[t]{0.45\linewidth} \centering \includegraphics[width=\linewidth]{example-image} \caption{b } \label{fig:b} \end{subfigure}

\bigskip%

\begin{subfigure}[t]{0.45\linewidth}
    \centering
    \includegraphics[width=\linewidth]{example-image}
    \caption{I have some text here that will shift the 4th figure to bottom}
    \label{fig:c}
\end{subfigure}%
\hfill%
\begin{subfigure}[t]{0.45\linewidth}
    \centering
    \includegraphics[width=\linewidth]{example-image}
    \caption{d}
    \label{fig:d}
\end{subfigure}
\caption{Main}
\label{fig:three graphs}

\end{figure}

\kant[2-4] \end{document}

Celdor
  • 9,058
  • Can you add more about the difference between [t] (top) and [b] (bottom) please? – Avv Oct 31 '22 at 12:58
  • 1
    These options determine a vertical alignment of an object w.r.t. a surrounding environment, with [c] being usually the default (See the special notes on the pos parameter on LaTeX/Boxes). If e.g. you place a tabular in a paragraph and add the option [t], LaTeX will position the table such that a baseline of its topmost part (text, rule etc.) is aligned with a baseline of a paragraph. The link has more detailed explanation of those concepts. – Celdor Oct 31 '22 at 13:37
  • 1
    It's worth noting that macros/environments such as tabular, subfigure, minipage, parbox etc. can contain other elements with their own baselines. The positional options are available to instruct LaTeX where the main baseline is going to be. Hence, [t] or [b] would refer to the topmost or the bottom-most element, respectively. – Celdor Oct 31 '22 at 13:51
  • Thank you. The baseline though is not defined in the document. I am not sure what it does mean exactly? – Avv Oct 31 '22 at 13:55
  • 1
    Each element in latex is a box, which latex place in a sequence. Each box has a baseline; it's a property. This is not necessarily a bottom point of a box: baseline of p, g etc. is set slightly upwards to gain depth and compensate for the bottom parts. When LaTeX places h and g next to each other, it aligns them along baselines, so it knows a box of g needs to be slightly lowered to correctly line up two letters or their baselines. Try the link I provided. – Celdor Oct 31 '22 at 14:48