2

I'm trying to center two subfigures that have different heights, but I can't seem to be able to allign them.

The original code is like this, and the subfigures are aligned at the bottom

\begin{figure}
\subfloat[Users collection.]{\includegraphics[width=0.4\textwidth]{Images/Database_users.png}\label{fig:database:subfig1}}%
\qquad
\subfloat[Events collection.]{\includegraphics[width=0.4\textwidth]{Images/Database_events.png}\label{fig:database:subfig2}}%
\centering
\caption{Database Architecture}
\label{fig:database}
\end{figure}

I tried the suggestions from the package documentation, but the right side caption alligns to the right, and the figures are overflowing into the rest of the document overlaping the text:

\newsavebox{\tempbox}
\begin{figure}
\sbox{\tempbox}{\includegraphics[width=0.4\textwidth]{Images/Database_users.png}}
\subfloat[Users collection.]{\usebox{\tempbox}\label{fig:database:subfig1}}%
\qquad
\subfloat[Events collection]{\vbox to \ht\tempbox{%
\vfil
\includegraphics[width=0.4\textwidth]{Images/Database_events.png}
\vfil}\label{fig:database:subfig2}}%
\caption{database}\label{fig:database}
\end{figure}

I also tried using \raisebox but the raised figure just disappears from the document:

\begin{figure}
\subfloat[Users collection.]{\raisebox{0.5em}\includegraphics[width=0.4\textwidth]{Images/Database_users.png}\label{fig:database:subfig1}}%
\qquad
\subfloat[Events collection.]{\includegraphics[width=0.4\textwidth]{Images/Database_events.png}\label{fig:database:subfig2}}%
\centering
\caption{Database Architecture}
\label{fig:database}
\end{figure}

Any help is appreciated. Thanks in advance

2

Applying a \raisebox{-.5\height}{} will center the graphic about the baseline.

\documentclass{article}
\usepackage{graphicx,subfig}
\begin{document}
\begin{figure}
\subfloat[Users collection.]{\raisebox{-.5\height}{%
  \includegraphics[height=30pt,width=0.4\textwidth]{example-image-a}}%
  \label{fig:database:subfig1}}%
\qquad
\subfloat[Events collection.]{\raisebox{-.5\height}{%
  \includegraphics[height=50pt,width=0.4\textwidth]{example-image-b}}%
  \label{fig:database:subfig2}}%
\centering
\caption{Database Architecture}
\label{fig:database}
\end{figure}
\end{document}

enter image description here

If you also need the captions aligned, then, add a \rule the height of the tallest image to the other figure(s).

\documentclass{article}
\usepackage{graphicx,subfig}
\begin{document}
\begin{figure}
\subfloat[Users collection.]{\raisebox{-.5\height}{%
  \includegraphics[height=30pt,width=0.4\textwidth]{example-image-a}}%
  \label{fig:database:subfig1}\rule[-25pt]{0pt}{50pt}}%
\qquad
\subfloat[Events collection.]{\raisebox{-.5\height}{%
  \includegraphics[height=50pt,width=0.4\textwidth]{example-image-b}}%
  \label{fig:database:subfig2}}%
\centering
\caption{Database Architecture}
\label{fig:database}
\end{figure}
\end{document}

enter image description here

  • Thank you very much for your help! As a follow-up question to the \rule command: instead of using absolute values (-x) is there a relative way of doing it so it matches the other subfigure? – Ricardo Jesus Apr 30 at 13:52
  • @RicardoJesus It is a lot of typing or copy/pasting, but you can insert in the shorter figure, a \vphantom of the exact command used to typeset the larger figure, as in for figure a, \subfloat[Users collection.]{\raisebox{-.5\height}{% \includegraphics[height=30pt,width=0.4\textwidth]{example-image-a}}% \label{fig:database:subfig1}\vphantom{{\raisebox{-.5\height}{% \includegraphics[height=50pt,width=0.4\textwidth]{example-image-b}}% \label{fig:database:subfig2}}}} – Steven B. Segletes Apr 30 at 13:55
  • @RicardoJesus alternately, you can insert a \rule in both subfigures, whose depth is slightly larger than 1/2 the biggest image (some trial/error may be needed). As in \subfloat[Users collection.]{\raisebox{-.5\height}{% \includegraphics[height=30pt,width=0.4\textwidth]{example-image-a}}% \label{fig:database:subfig1}\rule[-27pt]{0pt}{0pt}}% \qquad \subfloat[Events collection.]{\raisebox{-.5\height}{% \includegraphics[height=50pt,width=0.4\textwidth]{example-image-b}}% \label{fig:database:subfig2}\rule[-27pt]{0pt}{0pt}} – Steven B. Segletes Apr 30 at 13:58
  • 1
    I used the \vphantom command and it worked great! Thank you very much for the in-depth examples. – Ricardo Jesus Apr 30 at 18:20
1

With subcaption package it is easy:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\begin{minipage}[c]{.5\linewidth}
\centering
\includegraphics[width=2cm,height=1cm]{example-image}
\subcaption{A subfigure}\label{fig:1a}
\end{minipage}%
\begin{minipage}[c]{.5\linewidth}
\centering
\includegraphics[width=2cm,height=2cm]{example-image}
\subcaption{Another subfigure}\label{fig:1b}
\end{minipage}
\caption{A figure}\label{fig:1}
\end{figure}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.