如何定义一个使用图像宽度和高度来定义变量以供以后使用的宏

如何定义一个使用图像宽度和高度来定义变量以供以后使用的宏

好的,标题可能有点神秘,我的问题有点长,所以请耐心等待。

我想要做的是:我有两张具有不同纵横比的图像,我想使用 将它们并排放置subfloat。两张图像一起应该填满\textwidth并且另一方面具有相同的高度。还应该允许两张图像之间有间隙。从分析上讲,两张图像只有一个物理高度(比如\finalheight),这样所有条件都得到满足:

\finalheight = (\textwidth - gap_width) / (width_im_1/height_im_1 + width_im_2/height_im_2)

其中注释_im_1_im_2分别指第一幅和第二幅图像。计算后\finalheight,可以在图形命令中使用,如下所示:

\begin{figure}
\begin{centering}
  \subfloat[]{\includegraphics[height=\finalheight]{fistimage}}
  \hfill
  \subfloat[]{\includegraphics[height=\finalheight]{secondimage}}
\end{centering}
\end{figure} 

我已经有了一个可以在文档主体中运行的代码,即:

\newlength\firstheight
\newlength\firstwidth
\newlength\firstratio
\newlength\secondheight
\newlength\secondwidth
\newlength\secondratio
\newlength\gapspace
\newlength\finalheight
\newlength\effwidth
\newlength\sumratio

\def\first{\includegraphics{pathToFirstImage}}
\setlength{\firstheight}{\heightof{\first}}
\setlength{\firstwidth}{\widthof{\first}}
\pgfmathsetmacro{\firstratio}{\firstwidth/\firstheight}

\def\second{\includegraphics{pathToSecondImage}}
\setlength{\secondheight}{\heightof{\second}}
\setlength{\secondwidth}{\widthof{\second}}
\pgfmathsetmacro{\secondratio}{\secondwidth/\secondheight}

\setlength{\gapspace}{0.5cm}

\setlength{\effwidth}{\textwidth-\gapspace}
\setlength{\sumratio}{\firstratio+\secondratio}

\pgfmathsetmacro{\finalheight}{\effwidth/\sumratio}

但是,我现在尝试将上面的代码打包为宏:

\newcommand{\calcoptimalheight}{3}[0.5cm]{
...
 }

仅修改以下几行:

\def\first{\includegraphics{#2}}
\def\second{\includegraphics{#3}}
\setlength{\gapspace}{#1}  .

然而,这导致

"! Illegal unit of measure (pt inserted)" 

尝试评估时出错

\def\first{\includegraphics{#2}}  . 

现在我终于有疑问了:是否有某些原因导致\includegraphics无法在宏中以这种方式使用。如果不是,那么使用宏来设置变量的方法(如上所述)是完全错误的。我已经使用 LaTeX 很长时间了,但我自己从未过多地使用过宏或自定义变量,并且无法在网上搜索到有关此问题的帮助。

编辑

包括下面答案中的建议,代码可以工作,问题在于定义中的语法错误\newcommand{3}而不是[3]。不幸的是,错误消息将我引向了错误的路径,我应该看到这一点。在加入了来自使用 pgf 计算时,如何保留 \includegraphics 缩放使用的长度错误消息也解决了。为了方便大家理解,我添加了一个最小工作示例,其中包括来自此问题和其他问题的答案的解决方案:

\documentclass[12pt, twoside, paper=A4]{scrbook}
%
\usepackage{calc}
\usepackage{pgf}
\usepackage{graphicx}
\usepackage{subfig}
%
\newcommand{\optimalheight}[3][0.5cm]{%
  \newlength\firstheight%
  \newlength\firstwidth%
  \newlength\secondheight%
  \newlength\secondwidth%
  \newlength\effwidth%
  \newlength\finalheight%
  \def\firstim{\includegraphics{<imageA>}}%
  \def\secondim{\includegraphics{<imageB>}}%
  \setlength{\firstheight}{\heightof{\firstim}}%
  \setlength{\firstwidth}{\widthof{\firstim}}%
  \pgfmathsetmacro{\firstratio}{\firstwidth/\firstheight}%
  \setlength{\secondheight}{\heightof{\secondim}}%
  \setlength{\secondwidth}{\widthof{\secondim}}%
  \pgfmathsetmacro{\secondratio}{\secondwidth/\secondheight}%
  \pgfmathsetmacro{\gapspace}{0.5cm}%
  \pgfmathsetlength{\effwidth}{\textwidth-\gapspace}%
  \pgfmathsetmacro{\sumratio}{\firstratio+\secondratio}%
  \pgfmathsetlength{\finalheight}{\effwidth/\sumratio}% 
 }
%   
\newcommand{\subfloatsSameheight}[3][0.5cm]{%
  \optimalheight{#2}{#3}%
  \subfloat[a)]{\includegraphics[height=\finalheight]{#2}}%
  \hfill%
  \subfloat[b)]{\includegraphics[height=\finalheight]{#3}}%
}
%
\begin{document}
%
\begin{figure}[]
 \begin{centering}
   \subfloatsSameheight{<imageA>}{<imageB>}
   \caption[]{}
   \label{}
 \end{centering}
\end{figure} 
%
\end{document}

答案1

请始终展示产生问题的完整文档。您可以展示一些代码并显示错误消息,但不要展示产生错误的代码。

TeX 是一种宏扩展语言,您几乎总是可以将任何命令序列放入宏中,并且它们的工作方式相同。在这里,如果没有使用 catcode/verbatim 技巧,这当然是可能的。

\def\first{\includegraphics{#2}}
\def\first{\includegraphics{#3}}
\setlength{\gapspace}{#1} 

那条中间线大概应该是定义线\second

\setlength{\effwidth}{\textwidth-\gapspace}

通常是一个语法错误,但正如你所说它可以工作,大概你已经加载了calc包?(同样,如果你显示包含所有相关包的完整文档,那就容易多了)

您显示的语法\newcommand不正确

\newcommand{\calcoptimalheight}{3}[0.5cm]{

不是语法错误,而是3整个定义主体,你想要

\newcommand{\calcoptimalheight}[3][0.5cm]{%

您可能还想%在每一行的末尾添加。

相关内容