9

I am trying to get the matrix to align with the figures, but I can't figure out how to do it. The matrix always appears much lower than the figures.

\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.]{        
        \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}

enter image description here

10

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}

Result

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}

Result

  • Thank you very much. In my testing the \raisebox command can move the matrix higher. I should maybe modify the original question, but is there a way to actually center the matrix vertically? Or specify it's size so that it too takes up 0.3/textwidth? – Khan Feb 21 '17 at 23:15
  • oh, please make that last caption ragged right, to get rid of the horrid spacing! – barbara beeton Feb 22 '17 at 1:28
  • @barbarabeeton The spacing should now be better by using package microtype and hyphenation hints. – Heiko Oberdiek Feb 22 '17 at 5:42
  • @kristoh The updated answer adds a variation that vertically centers the matrix using the height of the second image and horizontally centers it using the space of .3\textwidth. – Heiko Oberdiek Feb 22 '17 at 5:56
6

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}

align automatically

  • Very nice, I was not aware of the subcaption package. Is it possible to also vertically center the matrix like in the variation of @heiko-oberdiek answer? – Khan Feb 22 '17 at 10:19
  • @kristoh I think you would need to calculate the heights of the other items on the line and then put the matrix into a box with the relevant height. I'm not an expert with this, by any means, though. – cfr Feb 22 '17 at 23:38
  • @kristoh There is also the floatrow package. However, it is extremely buggy and not likely ever to be less so. Hence, I would not recommend it. – cfr Feb 22 '17 at 23:39
3

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}      

enter image description here

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.