如何将子图高度与其他子图匹配?

如何将子图高度与其他子图匹配?

我试图让两个长宽比明显不同的子图具有相同的高度,方法是计算其中一个子图的高度,然后按照以下方法使用该高度计算另一个子图的高度这个答案但它们之间的长度被设置为零。我的理解是,这与 subfigure 环境有关。有什么办法可以让它工作吗?

两张图供参考:

笑脸缩小图像

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\newlength{\imageh}
\newlength{\imaged}
\newlength{\imagew}

\newcommand{\setimageh}[1]{
 \settoheight{\imageh}{\usebox{#1}}
}

\newcommand{\setimagew}[1]{
 \settowidth{\imagew}{\usebox{#1}}
}

\newcommand{\setimaged}[1]{
 \settodepth{\imaged}{\usebox{#1}}
}

\makeatletter
\newcommand{\ImageDimensions}[2]{
   % create and save the box
   \@ifundefined{Image}{\newsavebox{\Image}}{\usebox{\Image}}
  \savebox{\Image}{\includegraphics[width=#1]{#2}}
  \centering\usebox{\Image}\
  \setimageh{\Image}
  \setimagew{\Image}
  \setimaged{\Image}
}
\makeatother

\begin{document}

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.45\linewidth}
    \ImageDimensions{\linewidth}{smiley.png}
    \caption{this picture works\\height: \the\imageh}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.45\linewidth}
    \includegraphics[height=\imageh]{smiley-narrow.png}
    \caption{this picture doesnt show up\\
    height: \the\imageh}
    \end{subfigure}
\end{figure}

\end{document}

输出: 失败的方法

有一种解决方法,就是计算长度然后手动设置,但这需要编译,而编译许多数字会花费一些时间。

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.45\linewidth}
    \ImageDimensions{\linewidth}{smiley.png}
    \caption{this picture works\\height: \the\imageh}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.45\linewidth}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % manual set of height referencing compiled output of height
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \includegraphics[height=156pt]{smiley-narrow.png}
    \caption{this is a workaround\\
    height set manually}
    \end{subfigure}
\end{figure}

相关内容