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}