# How to preserve lenghts to use with \includegraphics scaling, when calculating them with pgf

This question is related to my earlier question here: How to define a macro that uses image width and height to define a variable for later use . There you can find a full MWE of the problematic code. Which I skip here for sake of brevity.

I use a mixture of \newlength, \setlength and \pgfmathsetmacro to calculate the optimal height for two figures I want to plot. The code works fine and the resulting images are displayed correclty. However, I get the error message:

ERROR: Illegal unit of measure (pt inserted).


when using the calculated height (here \finalheight) for:

\includegraphics[height=\finalheight] .


My guess is that using e.g.

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


I change a length that has a physical unit, to a mere number. Is that right? However I had a problem using \setlength to calculate ratios, which is why I started to use \pgfmathsetmacro in the first place. Does someone know a clean way to perform the calculation below such that the results \finalheight has a unit?

\newlength\firstheight%
\newlength\firstwidth%
\newlength\secondheight%
\newlength\secondwidth%
\def\firstim{\includegraphics{pathToImage}}%
\def\secondim{\includegraphics{pathToImage}}%
\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}%
\pgfmathsetmacro{\effwidth}{\textwidth-\gapspace}%
\pgfmathsetmacro{\sumratio}{\firstratio+\secondratio}%
\pgfmathsetmacro{\finalheight}{\effwidth/\sumratio}%

-

You are mixing up macro and length assignments. \pgfmathsetmacro results in a numeric output which is void of any unit of measure. If you insist on using \pgfmathsetmacro, then you need to use

\includegraphics[height=\finalheight pt]{<image>}


Alternatively, pgfmath provides \pgfmathsetlength which preserves the dimension component in the calculation. However, this requires you to pre-define additional lengths, as needed. Try the following:

\newlength\firstheight%
\newlength\firstwidth%
\newlength\secondheight%
\newlength\secondwidth%
\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}% <--- Changed
\pgfmathsetmacro{\sumratio}{\firstratio+\secondratio}%
\pgfmathsetlength{\finalheight}{\effwidth/\sumratio}% <--- Changed

-
Thanks that was fast. Both methods work. I've updated the MWE in the other question to include the changes. –  Ascurion Jul 5 '13 at 0:27
There is also \pgfmathsetlengthmacro which sets a macro but also includes the unit pt. –  Qrrbrbirlbel Jul 5 '13 at 2:22