Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}%
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.