子图内的数学表达式未对齐

子图内的数学表达式未对齐

我正在尝试让矩阵与数字对齐,但我不知道该怎么做。矩阵总是比数字低得多。

\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}

在此处输入图片描述

答案1

图像(未旋转)的基线是图像的底部。但矩阵垂直居中于数学轴;基线不在底部,而是较高,比中间略低。

可以使用 来按深度移动数学运算\raisebox{\depth}{...}。该示例还删除了行尾添加的一些不需要的空格。

第一和第二个标题的字母带有g降部,而第三个标题的第一行没有降部。本示例添加了虚线g以使三个标题的第一行基线正确对齐。

\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}

结果

中心矩阵的变化

  • 该矩阵在 的空间中水平居中.3\textwidth
  • 假设图像高度相等,则测量第二幅图像的高度,并使用另一个矩阵在此空间中垂直居中 \raisebox。嵌套\raisebox可以合并,但数学会更复杂,难以理解。
\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}

结果

答案2

我建议切换到subcaption,这在很多方面比 更容易subfig。(我知道,因为我最近就是因为这个原因而切换的。)

例如,\subcaptionbox可以用来自动对齐事物的基线并对齐其标题,即使它们的标题具有不同的高度和/或深度。

\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}

自动对齐

答案3

如果你希望所有三个对象垂直居中,你可以使用

    \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}      

在此处输入图片描述

相关内容