我有两个外部图形,我想使用subcaption
-package 的高度相同。为了获取第一个文件的高度,我使用\settoheight
。在使用它的子图中,值设置正确。但是,当我离开子图时,值似乎设置为零。这是为什么?我怎样才能将图像高度从左侧子图传递到右侧子图?
平均能量损失
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\newlength{\figwidth}
\newlength{\figheight}
\newlength{\figdim}
\begin{document}
\begin{figure}[htbp]
\begin{subfigure}{0.59\linewidth}
% Length
\setlength{\figwidth}{\linewidth}
\setlength{\figheight}{5cm}
\settoheight\figdim{\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}}
\setlength{\figheight}{\figdim}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}
\caption{\the\figdim, \the\figheight, left}%
\label{fig:left}
\end{subfigure}%
\hfill
\begin{subfigure}{0.39\linewidth}
% Length
\setlength{\figwidth}{\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-b}
\caption{\the\figdim, \the\figheight, right}%
\label{fig:right}
\end{subfigure}%
\caption{Test}
\label{fig}
\end{figure}
\end{document}
输出
有关的
更新
遵循@Werner 的方法:
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\newlength{\figwidth}
\newlength{\figheight}
\newlength{\figdim}
\begin{document}
\begin{figure}[htbp]
% Length
\setlength{\figwidth}{0.59\linewidth}
\setlength{\figheight}{5cm}
\settoheight\figdim{\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}}
\setlength{\figheight}{\figdim}
\begin{subfigure}{0.59\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}
\caption{\the\figheight, left}%
\label{fig2:left}
\end{subfigure}%
\hfill
\begin{subfigure}{0.39\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-b}
\caption{\the\figheight, right}%
\label{fig2:right}
\end{subfigure}%
\caption{Test}
\label{fig2}
\end{figure}
\end{document}
不幸的是,必须将宽度因子(x*\linewidth
)改变两次。
更新 2
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\newlength{\figwidth}
\newlength{\figheight}
\newlength{\figdim}
\begin{document}
\begin{figure}[htbp]
\begin{subfigure}{0.59\linewidth}
% Length
\setlength{\figwidth}{0.7\linewidth}
\setlength{\figheight}{5cm}
\settoheight\figdim{\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-16x9}}
\global\setlength{\figwidth}{\figwidth}
\global\setlength{\figheight}{\figdim}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-16x9}
\caption{\the\figdim, \the\figheight, left}%
\label{fig3:left}
\end{subfigure}%
\hfill
\begin{subfigure}{0.39\linewidth}
% Length
\setlength{\figwidth}{\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-16x10}
\caption{\the\figdim, \the\figheight, right}%
\label{fig3:right}
\end{subfigure}%
\caption{Test}
\label{fig3}
\end{figure}
\end{document}
答案1
这是范围限定的结果 - 如果在组内进行长度更改,则一旦退出该组,更改就会被恢复。下面是一个显示此情况的最小示例:
\documentclass{article}
\newlength{\mylength}
\begin{document}
\setlength{\mylength}{100pt}
\the\mylength
\begingroup
\setlength{\mylength}{200pt}
\the\mylength
\endgroup
\the\mylength
\end{document}
由于您正在环境内捕获图像尺寸subfigure
,因此一旦退出该环境,这些长度就会丢失。
解决方案是收集环境内figure
和subfigure
环境外的信息:
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\newlength{\figwidth}
\newlength{\figheight}
\newlength{\figdim}
\begin{document}
\begin{figure}[htbp]
% Length
\setlength{\figwidth}{\linewidth}
\setlength{\figheight}{5cm}
\settoheight\figdim{\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}}
\setlength{\figheight}{\figdim}
\begin{subfigure}{0.59\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}
\caption{\the\figdim, \the\figheight, left}%
\end{subfigure}%
\hfill
\begin{subfigure}{0.39\linewidth}
% Length
\setlength{\figwidth}{\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-b}
\caption{\the\figdim, \the\figheight, right}%
\end{subfigure}%
\caption{Test}
\end{figure}
\end{document}