3

Given this MWE (compiled under ConTeXt)

\starttext
\startMPpage

%variables
u:=1mm ;
numeric l[];
l[1]:=30 ;
l[2]:=70 ;

draw (0,0) -- (0,100u) ;
%this does not work
%fill unitsquare xscaled (l[2]-l[1])*u yscaled 100*u shifted (l[1]*u,0) withcolor red ;

%this does
fill unitsquare xscaled (l[2]*u-l[1]*u) yscaled 100u shifted (l[1]*u,0) withcolor red ;

\stopMPpage
\stoptext

Could you explain me why

fill unitsquare xscaled (l[2]-l[1])*u yscaled 100*u shifted (l[1]*u,0) withcolor red ;

fails, whereas

fill unitsquare xscaled (l[2]*u-l[1]*u) yscaled 100*u shifted (l[1]*u,0) withcolor red ;

does the job?

From my (not so good in maths) points of view, (l[2]-l[1])*u should be exactly the same, and this misunderstandings leads me often to frustrating errors.

I hope this is not a duplicate, but didn't know how to produce a proper title.

  • 1
    Have you tried xscaled ((l[2]-l[1])*u)? – Ruixi Zhang Sep 2 at 17:11
6

According to the Metapost manual, xscaled is a primary, so it has the same priority as multiplication.

It takes as arguments a pair, path or picture and a number, returning a pair, path or picture respectively; with your “non working” syntax Metapost is doing

unitsquare xscaled (l[2]-l[1])

and then trying to multiply the result by u, which is an invalid operation.

When operations have the same priority (all primaries, for instance), Metapost associates on the left. To get a different association you need parentheses.

The error you get is

>> path
>> 2.83464
! Not implemented: (path)*(known numeric).
<to be read again> 
                   yscaled

Of course ((l[2]-l[1])*u) would work as well, but the outer parentheses cannot be omitted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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