两幅不同大小的图像,并排,自动计算

两幅不同大小的图像,并排,自动计算

以下 Latex 宏取自这个答案。在此宏中,图像宽度和小页面宽度均硬编码为.45。这样做的问题是,生成的两个图像并非完全占据整个页面\textwidth,而是略小一些。实际结果取决于两个图像的纵横比。

相反,我们应该只指定图像之间的填充,然后让 Latex 使用以下计算来计算每幅图像的最佳宽度:

height = maximum of the height of both images
ratio1 = aspect ratio of image1
ratio2 = aspect ratio of image2
scaleFactor = (textwidth - padding) / 
   ((height*ratio1) + (height*ratio2))

width1 = (scaleFactor * height) * ratio1
width2 = (scaleFactor * height) * ratio2

我该如何调整以下宏来实现这一点?

\newsavebox\IBoxA \newsavebox\IBoxB \newlength\IHeight
\newcommand\TwoFig[6]{% Im1 Caption1 Label1 Im2 Cap2 Lab2
  \sbox\IBoxA{\includegraphics[width=0.45\textwidth]{#1}}
  \sbox\IBoxB{\includegraphics[width=0.45\textwidth]{#4}}%
  \ifdim\ht\IBoxA>\ht\IBoxB
    \setlength\IHeight{\ht\IBoxB}%
  \else\setlength\IHeight{\ht\IBoxA}\fi
  \begin{figure}[!htb]
  \minipage[t]{0.45\textwidth}\centering
  \includegraphics[height=\IHeight]{#1}
  \caption{#2}\label{#3}
  \endminipage\hfill
  \minipage[t]{0.45\textwidth}\centering
  \includegraphics[height=\IHeight]{#4}
  \caption{#5}\label{#6}
  \endminipage 
  \end{figure}%
}

更新 这里有两个不同大小的示例图像,可用于演示我的问题。

图片1: 可汗学院虚拟图片 1

图片2: 可汗学院虚拟图片 2

答案1

下面是一个可以更轻松地使用数学运算的示例lualatex。水平线仅用于一些演示:

\documentclass{article}
\usepackage{graphicx}
\newsavebox\IBoxA \newsavebox\IBoxB \newlength\Padding
\setlength\Padding{10pt}
\makeatletter
\newcommand\TwoFig[6]{% Image1 Caption1 Label1 Im2 Cap2 Lab2
  \sbox\IBoxA{\includegraphics{#1}}\sbox\IBoxB{\includegraphics{#4}}%
  \ifdim\ht\IBoxA>\ht\IBoxB
    \savebox\IBoxB{\includegraphics[height=\ht\IBoxA,keepaspectratio]{#4}}%
  \else
    \savebox\IBoxA{\includegraphics[height=\ht\IBoxB,keepaspectratio]{#1}}%
  \fi
  \edef\TotalWidth{\strip@pt\dimexpr\wd\IBoxA+\wd\IBoxB}%
  \edef\LineScale{\directlua{tex.sprint(\strip@pt\linewidth/\TotalWidth)}}%
%
  \begin{figure}[!htb]\centering
    \minipage[t]{\dimexpr\LineScale\wd\IBoxA-0.5\Padding}\centering
      \frame{\includegraphics[width=\linewidth]{#1}}
      \caption{#2}\label{#3}
    \endminipage\hfill
    \minipage[t]{\dimexpr\linewidth-\LineScale\wd\IBoxA-0.5\Padding}\centering
      \frame{\includegraphics[width=\linewidth]{#4}}
      \caption{#5}\label{#6}
    \endminipage 
  \end{figure}%
}
\makeatother
\begin{document}
\noindent\rule{0.5\linewidth}{1pt}\rule{0.5\linewidth}{2pt}

\bigskip
\TwoFig{/tmp/pic0}{Image 1}{fg:im1}%
            {/tmp/pic1}{Image 2}{fg:im2}  

\noindent\hrule

\centering\frame{\includegraphics[scale=0.25]{/tmp/pic0}}\hspace{\Padding}%
\frame{\includegraphics[scale=0.25]{/tmp/pic1}}

\end{document}

在此处输入图片描述

答案2

更新:与以前的想法相同,但现在代码被包裹在一些宏和环境中,更接近问题的意图。

\documentclass{article}

\usepackage{graphicx,calc,lipsum}

% \DivideLength pilfered from http://tex.stackexchange.com/questions/6417/
\makeatletter
\newcommand*{\DivideLengths}[2]{%
  \strip@pt\dimexpr\number\numexpr\number\dimexpr#1\relax*65536/\number\dimexpr#2\relax\relax sp\relax
}
\makeatother

\graphicspath{{/data/graphics/fun/}} % just for my computer

\newlength{\padding}  
\newlength{\actualHeight}
\newlength{\initialHeight}           % arbitrary initial height for both images,
\setlength{\initialHeight}{1in}      % thrown away after computation

\newsavebox{\imgboxA}
\newsavebox{\imgboxB}

\setlength{\overfullrule}{15pt}

\newenvironment{doublefigure}[3][0.05\textwidth]{%
    \setlength{\padding}{#1}%
    \def\imgA{#2}\def\imgB{#3}%
    \sbox{\imgboxA}{\includegraphics[height=\initialHeight]{\imgA}}%
    \sbox{\imgboxB}{\includegraphics[height=\initialHeight]{\imgB}}
    \def\scale{\DivideLengths{\textwidth-\padding}{\wd\imgboxA+\wd\imgboxB}}%
    \setlength{\actualHeight}{\scale\initialHeight}%
    \sbox{\imgboxA}{\includegraphics[height=\actualHeight]{\imgA}}
    \sbox{\imgboxB}{\includegraphics[height=\actualHeight]{\imgB}}
\begin{figure}}
%
{\end{figure}}


\newenvironment{leftfigure}[1][t]{%
  \noindent\begin{minipage}[#1]{\wd\imgboxA}%
  \usebox{\imgboxA}%
}
%
{\end{minipage}\hspace{\padding}\ignorespaces}

\newenvironment{rightfigure}[1][t]{%
  \begin{minipage}[#1]{\wd\imgboxB}%
  \usebox{\imgboxB}%
}
%
{\end{minipage}}


\begin{document}

\lipsum[1]  % just some text to show image width relative to text width

\begin{doublefigure}{getafix}{dilbert-eunuchs}

\begin{leftfigure}
\caption{Getafix}
\label{Getafix}
\end{leftfigure}%
%
\begin{rightfigure}
\caption{Dilbert's trials and tribulations}
\label{Dilbert}
\end{rightfigure}

\end{doublefigure}

\end{document} 

在此处输入图片描述

两个图像的文件名作为参数传递给环境doublefigure,环境还将它们之间的填充作为可选参数。图像放置在保存框内,保存框在缩放后为两个小页面提供宽度,而小页面又被包裹在leftfigurerightfigure环境中。后者也可以定义为具有两个参数(标题和标签)的宏;这是个人喜好问题。我似乎更喜欢环境;这可能使我成为一名环保主义者。

相关内容