From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/24731 Path: news.gmane.org!not-for-mail From: Mojca Miklavec Newsgroups: gmane.comp.tex.context Subject: Re: Metapost -- What am I doing wrong Date: Mon, 26 Dec 2005 00:02:59 +0100 Message-ID: <6faad9f00512251502s560cd871i541b9453ab906e79@mail.gmail.com> References: Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: sea.gmane.org 1135551793 25502 80.91.229.2 (25 Dec 2005 23:03:13 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sun, 25 Dec 2005 23:03:13 +0000 (UTC) Original-X-From: ntg-context-bounces@ntg.nl Mon Dec 26 00:03:11 2005 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from ronja.vet.uu.nl ([131.211.172.88] helo=ronja.ntg.nl) by ciao.gmane.org with esmtp (Exim 4.43) id 1EqetR-0003MM-Ll for gctc-ntg-context-518@m.gmane.org; Mon, 26 Dec 2005 00:03:09 +0100 Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id B2E89127BA; Mon, 26 Dec 2005 00:03:08 +0100 (CET) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 20572-03; Mon, 26 Dec 2005 00:03:04 +0100 (CET) Original-Received: from ronja.vet.uu.nl (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 63E691279C; Mon, 26 Dec 2005 00:03:03 +0100 (CET) Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id B549D1279C for ; Mon, 26 Dec 2005 00:03:01 +0100 (CET) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 20543-03-2 for ; Mon, 26 Dec 2005 00:03:00 +0100 (CET) Original-Received: from nproxy.gmail.com (nproxy.gmail.com [64.233.182.202]) by ronja.ntg.nl (Postfix) with ESMTP id 7D84912798 for ; Mon, 26 Dec 2005 00:03:00 +0100 (CET) Original-Received: by nproxy.gmail.com with SMTP id o25so314257nfa for ; Sun, 25 Dec 2005 15:02:59 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=fCSNg77/6Mhji+J67XUf91HLNJZTjmE4Ifai4tSmhLI8O14cqSu1X4KUuSb3NN4aXgyz4se4nkD+R6GwtK8SdytI3/v47NhZ2o93VMVX8R1vfKHQue79X8SBdhNgdYHeH/3GUXPF/yfRn9w4OinsKtsLHiShZp1O1Eh+k7vG7Is= Original-Received: by 10.48.245.12 with SMTP id s12mr207566nfh; Sun, 25 Dec 2005 15:02:59 -0800 (PST) Original-Received: by 10.48.223.20 with HTTP; Sun, 25 Dec 2005 15:02:59 -0800 (PST) Original-To: mailing list for ConTeXt users In-Reply-To: Content-Disposition: inline X-Virus-Scanned: amavisd-new at ntg.nl X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.5 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: ntg-context-bounces@ntg.nl Errors-To: ntg-context-bounces@ntg.nl X-Virus-Scanned: amavisd-new at ntg.nl Xref: news.gmane.org gmane.comp.tex.context:24731 Archived-At: David Arnold wrote: > All, > > Can someone tell me what I am doing wrong here? \useMPgraphic is meant to include whole figures inside text, not for including portions of other metapost code inside metapost. What you can do is to define new functions inside \startMPinclusions ... \stopMPinclusions or in an external file. I don't doubt that implementing support for your way of thinking in metafun is doable, but I don't know if it is worth the effort or not. Here's an approximate transliteration into working code (I don't claim that it's most elegant and I didn't know how to pass a function as an argument, but I hope you can get an idea how to proceed. With slightly more arguments/parameters to the defined functions, you can get rid of hardcoding "fives" in your code for example.) \setupcolors[state=3Dstart] \definecolor[gridlines][s=3D0.7] \startMPinclusions =09color gridlines; gridlines=3D\MPcolor{gridlines}; =09% draw the grid 2n x 2n with unit length u =09def draw_axis(expr n, u) =3D =09=09% draw the grid =09=09for i=3D-5 upto 5: =09=09=09draw ((i,-5)--(i,5)) scaled u; =09=09=09draw ((-5,i)--(5,i)) scaled u; =09=09endfor; =09=09%draw axes =09=09drawdblarrow (-5.2u,0)--(5.2u,0); =09=09drawdblarrow (0,-5.2u)--(0,5.2u); =09=09%label axes =09=09label.rt(btex $x$ etex, (5.2u,0)); =09=09label.top(btex $y$ etex, (0,5.2u)); =09=09label.bot(btex $5$ etex, (5u,0)); =09=09label.lft(btex $5$ etex, (0,5u)); =09enddef; =09def scale_and_draw_f(expr p, u) =3D =09=09%scale function path =09=09%p:=3Dp scaled u; =09=09%draw function path =09=09drawdblarrow (p scaled u) withcolor blue; =09enddef; =09% I don't know how to give the function as an argument, =09% so I cheated and copied it here once more =09vardef create_function_path_f =3D =09=09%define clipping path =09=09path cpath; =09=09cpath:=3D(-5,-5)--(5,-5)--(5,5)--(-5,5)--cycle; =09=09%define function =09=09vardef f(expr x)=3D =09=09=09x*x =09=09enddef; =09=09%create function path =09=09path p; p:=3D(-5,f(-5)); =09=09for x=3D-5 step .1 until 5: =09=09=09p:=3Dp--(x,f(x)); =09=09endfor; =09=09p:=3Dp--(5,f(5)); =09=09%clip function path to clipping path =09=09p:=3Dp cutbefore cpath; =09=09p:=3Dreverse p; =09=09p:=3Dp cutbefore cpath; =09=09p =09enddef; \stopMPinclusions \starttext \startMPpage =09%define function =09vardef f(expr x)=3D =09=09x*x =09enddef; =09path p; =09p :=3D create_function_path_f; =09%initialize scale =09numeric u; 10u=3D2in; =09draw_axis(n, u); =09scale_and_draw_f(p, u); =09%draw plotted points =09for x=3D-2 step 1 until 2: =09=09drawdot ((x,f(x)) scaled u) withpen pencircle scaled 4pt withcolor bl= ue; =09endfor; \stopMPpage \stoptext > %Ouput=3Dpdf > > \setupcolors[state=3Dstart] > > \definecolor[gridlines][s=3D0.7] > > \startMPinclusions > color gridlines; gridlines=3D\MPcolor{gridlines} > \stopMPinclusions > > > \startMPgraphic{create function path f 5-by-5} > > %define clipping path > path cpath; > cpath:=3D(-5,-5)--(5,-5)--(5,5)--(-5,5)--cycle; > > %create function path > path p; p:=3D(-5,f(-5)); > for x=3D-5 step .1 until 5: > p:=3Dp--(x,f(x)); > endfor; > p:=3Dp--(5,f(5)); > > %clip function path to clipping path > p:=3Dp cutbefore cpath; > p:=3Dreverse p; > p:=3Dp cutbefore cpath; > > \stopMPgraphic > > \startMPgraphic{scale and draw f 5-by-5} > > %scale function path > p:=3Dp scaled u; > > %draw function path > drawdblarrow p withcolor blue; > > \stopMPgraphic > > \startMPgraphic{draw axis 5-by-5} > > %draw the grid > for k=3D-5u step 1u until 5u: > draw (k,-5u)--(k,5u) withcolor gridlines; > draw (-5u,k)--(5u,k) withcolor gridlines; > endfor; > > %draw axes > drawdblarrow (-5.2u,0)--(5.2u,0); > drawdblarrow (0,-5.2u)--(0,5.2u); > > %label axes > label.rt(btex $x$ etex, (5.2u,0)); > label.top(btex $y$ etex, (0,5.2u)); > label.bot(btex $5$ etex, (5u,0)); > label.lft(btex $5$ etex, (0,5u)); > > \stopMPgraphic > > \starttext > > \startMPpage > > %define function > vardef f(expr x)=3D > x*x > enddef; > > \useMPgraphic{create function path f 5-by-5} > > %initialize scale > numeric u; 10u=3D2in; > > \useMPgraphic{draw axis 5-by-5} > > \useMPgraphic{scale and draw f 5-by-5} > > %draw plotted points > for x=3D-2 step 1 until 2: > drawdot ((x,f(x)) scaled u) withpen pencircle scaled 4pt withcolor > blue; > endfor; > > \stopMPpage > > \stoptext