3

How can I center align the two subfigures and align the caption (a), (b) for the two figures like this?
Assume that the image sizes are unknown. (or probably I can input it manually)
I saw some previous posts relating to this but they used minipages which I couldn't apply successfuly to my subfigures.

enter image description here

\documentclass[journal]{IEEEtran}
\usepackage[]{subfig}
\usepackage{stfloats}
\usepackage{float}
\usepackage{graphicx}

\begin{document} % \begin{figure}[!t] \centering \subfloat[]{\includegraphics[width=0.25\linewidth, height=30pt]{example-image-a} \label{fig_first_case}} \hfil \subfloat[]{\includegraphics[width=0.25\linewidth, height=60pt]{example-image-b} \label{fig_second_case}} \caption{Example of two figures. (a) The first figure. (b) The second figure} \label{fig_subfig} \end{figure} % \end{document}

emnha
  • 445
  • 3
  • 11

3 Answers3

2

This is easy with the floatrow package:

    \documentclass[journal]{IEEEtran}
    \usepackage[]{subfig}
    \usepackage{stfloats}
    \usepackage{graphicx}
    \usepackage{floatrow}
\begin{document}

\begin{figure*}[!t]
\floatsetup{heightadjust=all, valign=c}
\centering
\ffigbox{\begin{subfloatrow}
\ffigbox{\caption{}\label{fig_first_case}}{\includegraphics[width=0.25\linewidth, height=30pt]{example-image-a}
}
\hfil
\ffigbox{\caption{}\label{fig_second_case}}{\includegraphics[width=0.25\linewidth, height=60pt]{example-image-b}
}
\end{subfloatrow}}{\caption{Example of two figures. (a) The first figure. (b) The second figure}}
\end{figure*}

\end{document} 

enter image description here

Bernard
  • 271,350
  • Thanks, how can I keep the same caption style as the original like Fig. 1 instead of Fig. 1:? The caption style is not compatible with IEEEtran. – emnha Aug 31 '22 at 15:50
  • I got some other warning as well from overleaf: Package caption Warning: Unknown document class (or package), standard defaults will be used. See the caption package documentation for explanation. Package caption Warning: \setcaptionsubtype without \DeclareCaptionSubType. This is not designed to work; you could try replacing thesubfig' package with subcaption' to resolve this problem which occurred on input line 18. See the caption package documentation for explanation – emnha Aug 31 '22 at 16:02
  • floatrow cooperates with the caption package . You might try \captionsetup{labelsep=none}. – Bernard Aug 31 '22 at 16:07
  • Where should I put this? – emnha Sep 01 '22 at 04:15
  • @emnha: either in the preamble, or just after the beginnning of the environment if it is to be used only once. – Bernard Sep 01 '22 at 08:05
2

You should call subfig with the caption=false option, otherwise the caption style of IEEEtran would be overridden: this will annoy the copy editors of the journal you're submitting to (and maybe cause rejection).

For just one case you can do it manually:

\documentclass[journal]{IEEEtran}
\usepackage[caption=false]{subfig}
\usepackage{graphicx}

\MakeRobust{\subref}

\newsavebox{\leftimage} \newsavebox{\rightimage} \newlength{\imageheight} \newcommand{\placeimage}[1]{% \raisebox{\dimexpr(\imageheight-\height)/2}{\usebox{#1}}% }

\begin{document}

\begin{figure*}[!t] \centering

\sbox{\leftimage}{\includegraphics[width=0.25\linewidth, height=30pt]{example-image-a}} \sbox{\rightimage}{\includegraphics[width=0.25\linewidth, height=60pt]{example-image-b}}

\setlength{\imageheight}{\ht\leftimage} \ifdim\ht\rightimage>\imageheight \setlength{\imageheight}{\ht\rightimage} \fi

\subfloat[]{\placeimage{\leftimage}\label{fig_first_case}}\hfil \subfloat[]{\placeimage{\rightimage}\label{fig_second_case}}

\caption{% Example of two figures. \subref{fig_first_case}~The first figure. \subref{fig_second_case}~The second figure. } \label{fig_subfig} \end{figure*}

\end{document}

enter image description here

Both images are measured and the larger vertical size is determined. Then the boxes containing the images are raised by the appropriate amount.

egreg
  • 1,121,712
  • This is very nice but I have some problem with reference hope you can help. It's related to my previous post. https://tex.stackexchange.com/questions/655002/how-to-refer-subfigure-with-parentheses-as-fig-1a-instead-of-fig-1a I used \renewcommand\thesubfigure{(\alph{subfigure})} as Zxcvasdf mentioned to avoid double parenthesis like ((a)) and also refer to figure as Fig. 1(a) instead of Fig. 1a but now I got the problem like Example of two figures. ((a)) The first figure. ((b)) The second figure. – emnha Sep 01 '22 at 19:17
  • @emnha That's a known problem with subfig. – egreg Sep 01 '22 at 20:01
  • How can I fix this? \renewcommand\thesubfigure{(\alph{subfigure})} that Zxcvasdf suggested work for reference but it doesn't remove double parenthesis in the caption of your solution. – emnha Sep 01 '22 at 20:06
  • @emnha Did you look at https://tex.stackexchange.com/q/16291/4427 ? – egreg Sep 01 '22 at 20:07
  • I did and used \renewcommand\thesubfigure{(\alph{subfigure})} that Zxcvasdf suggested and it fixed the problem of reference in the text. However, it doesn't work in figure caption in your code. – emnha Sep 01 '22 at 20:40
  • I solved it with \subref*{} in the caption! – emnha Sep 01 '22 at 22:40
1

In case IEEE has a problem with floatrow...

The tricky ibt was matching the spacing used by \subfloat (NOT \abovecaptionskip or \belowcaptionskip).

\documentclass[journal]{IEEEtran}
\usepackage{graphicx}
\usepackage{subfig}% for \thesubfigure

\begin{document} % \begin{figure*}[!t] \centering \tabcolsep=0.0833\linewidth% 0.5\linewidth / 6 (3 equal gaps) \belowcaptionskip=-\baselineskip \begin{tabular}{@{}cc@{}} \raisebox{-0.5\height}{\includegraphics[width=0.25\linewidth, height=30pt]{example-image-a}} & \raisebox{-0.5\height}{\includegraphics[width=0.25\linewidth, height=60pt]{example-image-b}} \ \refstepcounter{subfigure}\label{fig_first_case}\raisebox{-1ex}{(\thesubfigure)} & \refstepcounter{subfigure}\label{fig_second_case}\raisebox{-1ex}{(\thesubfigure)} \end{tabular}

\caption{Example of two figures. (a) The first figure. (b) The second figure} \label{fig_subfig} \end{figure*} % \end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • How can I keep the caption style as Fig. 1 instead of Fig. 1: which is not compatible with IEEEtran? – emnha Sep 01 '22 at 04:10
  • You can use \caption{} to get Fig.1 with no colon, but adding a colon before the title is IEEE standard. – John Kormylo Sep 01 '22 at 13:32