# How to define a macro that uses image width and height to define a variable for later use

Ok, the title might be a bit cryptic and my question is a bit lengthy, so please bear with me.

What I want to do is: I have two images with different aspect ratios that I want to place next to each other using subfloat. Together both images should fill the \textwidth and on the other hand have the same height. Also a gap between both images should be allowed. Analytically there is only one physical height (say \finalheight) for both images, such that all conditions are fullfilled:

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


where the annotations _im_1 and _im_2 refer to the first and the second image, respectively. Once calculated the \finalheight can then used in the figure command as follows:

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


I have already a code that works within the body of the document i.e.:

\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}


However, I now tried to package the above code as a macro:

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


with only these lines modified:

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


This however results in an

"! Illegal unit of measure (pt inserted)"


error when trying to evaluate

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


Now finally my question: Is there some reason that \includegraphics may not be used this way in a macro. If not is the approach altogether wrong to use a macro to set a variable as explained above. I've been using LaTeX for quite some time, but I never messed too much with macros or custom variables myself and could not find help for this issue searching the web.

Edit:

Inculding the suggestions from the answer below the code works, the problem was the wrong syntax in the \newcommand definition: {3} instead of [3]. Unfortunately, the error message sent me on the wrong path, I should have seen that. After inculsion of the improvemts from How to preserve lenghts to use with \includegraphics scaling, when calculating them with pgf also the error message is solved. In case it is useful for somebody I add an minimal working example including the solutions from the answers to this and the other question:

\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}

-
Welcome to TeX.SX! – Sean Allred Jul 3 '13 at 15:26
Analytically, W1 = (k2/(k1+k2)) \textwidth, where k1 and k2 are the image aspect ratios (k=height/width) and W1 is width of image 1. Obviously W2=\textwidth-W1. – Steven B. Segletes Jul 3 '13 at 17:15

Please always show a complete document that generates the problem. You show some code and you show an error message, but not the code that generates the error.

TeX is a macro expansion language you can almost always put any sequence of commands in a macro and they work the same way. Here, where no catcode/verbatim trickery is used it is certainly possible.

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


That middle line should presumably be defining \second.

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


would normally be a synax error but as you say it works, presumably you have loaded calc package? (Again it is much easier if you show a complete document with all relevant packages)

The syntax for \newcommand that you show is incorrect

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


Is not a syntax error but 3 is the entire body of the definition, you want

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


You probably want to add % at the end of every line as well.

-
Thanks for your quick answer! I'll keep your advice in mind,next time I'll provide a more complete example. Unfortunaltely, your proposed modifications do not change the outcome. I'll prepare a minimal, updated example and add it to the original question. – Ascurion Jul 3 '13 at 16:26
Actually I have been mistaken, producing the minimal example I found that the code runs fine, altough the error message persists. – Ascurion Jul 3 '13 at 17:10