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