Misaligned math expression inside subfig
The baseline for the images (not rotated) is the bottom of the image. But the matrix is vertically centered around the math axis; the base line is not at the bottom, but higher, a little lower than the middle.
The math can be shifted by the depth using \raisebox{\depth}{...}
. The example also removes some unwanted spaces that are added by line ends.
The first and second captions have the letter g
with a descender, which is not present in the first line of the third caption. The example adds a phantom g
to get the correct alignment of the baseline of the first lines of the three captions.
\documentclass{article}
\usepackage{microtype}
\usepackage[utf8]{inputenc}
\usepackage{graphicx} %package to manage images
\usepackage{amsmath} % draw matrices
\usepackage{subfig}
\begin{document}
\begin{figure}[h]
\centering
\subfloat[Undirected graph.]{%
\includegraphics[width=0.3\textwidth]{pic1.png}%
\label{fig:gr_undirected}%
}
\hfill
\subfloat[Undirected graph adjacency list.]{%
\includegraphics[width=0.3\textwidth]{pic2.png}%
\label{fig:gr_undirected_ll}%
}
\hfill
\subfloat[%
\leavevmode % start paragraph mode
\protect\vphantom{g}% add space of descender in first line
\protect\hspace{0pt}% allow hyphenation of next word
Un\-di\-rect\-ed graph ad\-ja\-cen\-cy matrix%
]{%
\raisebox{\depth}{%
\large
$\begin{bmatrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0
\end{bmatrix}$%
}%
\label{fig:gr_undirected_am}%
}
\caption{An undirected graph and its representations.}
\label{fig:gr_undirected_representation}
\end{figure}
\end{document}
Variation with centered matrix
- The matrix is horizontally centered in the space of
.3\textwidth
. - Assuming that the image heights are equal, the height of the second image is measured and the matrix is vertically centered in this space by using another
\raisebox
. The nested\raisebox
can be merged, but the math would be more complicate to understand.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx} %package to manage images
\usepackage{amsmath} % draw matrices
\usepackage{subfig}
\begin{document}
\begin{figure}[h]
\centering
\subfloat[Undirected graph.]{%
\includegraphics[width=0.3\textwidth]{pic1.png}%
\label{fig:gr_undirected}%
}
\hfill
\subfloat[Undirected graph adjacency list.]{%
\sbox0{%
\includegraphics[width=0.3\textwidth]{pic2.png}%
}%
\xdef\GlobalImageHeight{\the\ht0}%
\usebox{0}%
\label{fig:gr_undirected_ll}%
}
\hfill
\subfloat[Undirected graph adjacency matrix]{%
\hbox to 0.3\linewidth{%
\hfill
\raisebox{.5\dimexpr\GlobalImageHeight-\height}{%
\raisebox{\depth}{%
\large
$\begin{bmatrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0
\end{bmatrix}$%
}%
}%
\hfill
}%
\label{fig:gr_undirected_am}%
}
\caption{An undirected graph and its representations.}
\label{fig:gr_undirected_representation}
\end{figure}
\end{document}
I would recommend switching to subcaption
which makes things rather easier in many respects than subfig
. (I know as I recently switched for just this reason.)
For example, \subcaptionbox
can be used to automatically align the baselines of things and to align their captions, even if their captions have different heights and/or depths.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[demo]{graphicx}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{subcaption}
\begin{document}
\begin{figure}% don't use h as the sole specifier
\centering
\subcaptionbox{Undirected graph.\label{fig:gr_undirected}}{\includegraphics[width=0.3\textwidth]{pic1}}
\hfill
\subcaptionbox{Undirected graph adjacency list.\label{fig:gr_undirected_ll}}{\includegraphics[width=0.3\textwidth]{pic2}}
\hfill
\subcaptionbox{Undirected graph adjacency matrix\label{fig:gr_undirected_am}}
{\begin{minipage}{.2\textwidth}
\large
\[\begin{bmatrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0
\end{bmatrix}\]
\end{minipage}}
\caption{An undirected graph and its representations.}
\label{fig:gr_undirected_representation}
\end{figure}
\end{document}
If you wish all three objects to be centered vertically, you may use
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx} %package to manage images
\usepackage{amsmath} % draw matrices
\usepackage{subfig}
\newcommand{\CenterObject}[1]{\ensuremath{\vcenter{\hbox{#1}}}}
\begin{document}
\begin{figure}[h]
\centering
\subfloat[Undirected graph.]{
\CenterObject{\includegraphics[width=0.3\textwidth]{pic1.png}}
\label{fig:gr_undirected}
}
\hfill
\subfloat[Undirected graph adjacency list.]{
\CenterObject{\includegraphics[width=0.3\textwidth]{pic2.png}}
\label{fig:gr_undirected_ll}
}
\hfill
\large
\subfloat[Undirected graph adjacency matrix]{
$\begin{bmatrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0
\end{bmatrix}$
\label{fig:gr_undirected_am}
}
\caption{An undirected graph and its representations.}
\label{fig:gr_undirected_representation}
\end{figure}
\end{document}