我尝试在两个子图中显示水平和垂直图片,两个子图相互叠放。如何才能同时显示两个子图而不完全扭曲其中一个子图?如果其中一个或两个子图的尺寸发生变化,我完全没问题,只要它们没有倾斜。
这是我的代码和结果:
\begin{figure}[!htb]
\centering %%% not \center
\subfigure[minor Eiffeltower]{\label{fig:b}\includegraphics[width=0.45\textwidth]{content/images/Eiffeltower1.jpg}}
\subfigure[major Eiffeltower]{\label{fig:a}\includegraphics[width=0.45\textwidth]{content/images/Eiffeltower2.jpg}}
\caption{Picture is stolen from: $https://upload.wikimedia.org/wikipedia/commons/a/a8/Tour_Eiffel_Wikimedia_Commons.jpg$}
\end{figure}
答案1
这是一个宏,它计算两张图片的宽度,使它们在文档中的高度相同。调用它后,\PicWidthA
将包含第一张图片的宽度和\PicWidthB
第二张图片的宽度。
我subcaption
在这里使用了该包,因为subfigure
您使用的包已经贬值了。
代码:
\documentclass[a4paper]{article}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{lipsum}
\makeatletter
\newdimen\@picAx
\newdimen\@picAy
\newdimen\@picBx
\newdimen\@picBy
\newdimen\PicWidthA
\newdimen\PicWidthB
\newsavebox\imagebox
% #1: width of both pictures
% #2: gap between picture
% #3:
% #4:
\newcommand{\CalcPicWidths}[4]{%
% get heights and widths of pictures
\sbox{\imagebox}{\includegraphics{#3}}%
\@picAx\wd\imagebox
\@picAy\ht\imagebox
\sbox{\imagebox}{\includegraphics{#4}}%
\@picBx\wd\imagebox
\@picBy\ht\imagebox
% clear box
\sbox{\imagebox}{}%
% get final wdth of both picture (= #1 - #2)
\@tempdima#1\relax
\advance\@tempdima-#2\relax
% width of pictures, second scaled for same height as first
\PicWidthA\@picAx
\setlength{\PicWidthB}{\@picBx * \ratio{\@picAy}{\@picBy}}%
% width of both pictures
\@tempdimb\PicWidthA
\advance\@tempdimb\PicWidthB
% final widths
\setlength{\PicWidthA}{\PicWidthA * \ratio{\@tempdima}{\@tempdimb}}%
\setlength{\PicWidthB}{\PicWidthB * \ratio{\@tempdima}{\@tempdimb}}%
}
\makeatother
\begin{document}
\lipsum[1]
\begin{figure}[htb]
\CalcPicWidths{\textwidth}{5pt}{example-image-16x9.png}{example-image-9x16.png}%
\begin{subfigure}[t]{\PicWidthA}
\centering
\includegraphics[width=\linewidth]{example-image-16x9.png}
\caption{Subfigure A}\label{fig:1a}
\end{subfigure}%
\hfill
\begin{subfigure}[t]{\PicWidthB}
\centering
\includegraphics[width=\linewidth]{example-image-9x16.png}
\caption{Subfigure B}\label{fig:1b}
\end{subfigure}
\caption{A figure}\label{fig:1}
\end{figure}
\lipsum[2]
\end{document}
结果如下: