4

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

1 Answer 1

4

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%
\newlength\effwidth% <--- Added
\newlength\finalheight% <--- Added
\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
2
  • Thanks that was fast. Both methods work. I've updated the MWE in the other question to include the changes.
    – Ascurion
    Commented Jul 5, 2013 at 0:27
  • There is also \pgfmathsetlengthmacro which sets a macro but also includes the unit pt. Commented Jul 5, 2013 at 2:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .