Changes

Jump to navigation Jump to search
m
added \startTEXpage[pagestate=start] %`pagestate=start` is needed to have incremented page numbering of the pdf
__TOC__
MetaPost is a graphical programming language, based on Donald Knuth's MetaFont. Normally MP graphics are converted to PostScript and used with dvips, but ConTeXt can use it directly with PDF (see the
[http://www.pragma-ade.comnl/general/sources/mptopdf.pdf MP to PDF] manual).
MetaPost is ConTeXt's native graphics language. [[MetaFun - MetaPost in ConTeXt]] is a MetaPost module by Hans Hagen that adds a lot of extra features; it is enabled by default, so one could say that MetaFun is ConTeXt's default dialect of MetaPost.
There is one important remark: The MPcalculation and the drawing itself must be inside \starttext ... \stoptext.
 
= Layer sets as individual pages =
The following ConTeXt code example can be used to create a series of layered Metapost graphics and generate multiple pages with different combinations of these layers. Each page represents a different composition of the defined layers. Use cases are when you need to compose manually your layer sets.
 
== Base or Invisible Layer ==
The code begins by defining a base or invisible layer called "base" using the `\startuseMPgraphic` command. In this layer, various components are defined, such as paths, colors, and coordinates. These components will be used to draw the images in subsequent layers, but they're not drawn yet, thus this layer is "invisible".
 
== Layer Definitions ==
After defining the base layer, several other layers are defined using the `\startuseMPgraphic` command. Each layer is given a unique name, in our example the names are such as "layerA" "layerW" or "layerM". These layers are created as images using the `picture` environment. Each image consists of drawn elements. In our example, the elements are filled squares with a specific color and a label.
 
== Finishing Layer ==
The purpose of the finishing layer in our ConTeXt code is to add the final touches to the drawn layers and to clip the graphic to the same frame. It also includes the addition of page numbering to each page with the help of a Metapost variable, in our case "mypagenumber". We need this method because apparently, it is not possible to use the ConTeXt page numbering on a Metapost page. The finishing layer needs to be applied after drawing all the other layers.
 
== Layer Sets ==
The before defined layers can be "set" (selected and combined) manually together, to become a layer set. They are layered on top of each other according to the order of appearance. The last layer is put on top of (and might cover) the previous one.
Some subgroups or intermediate results of our "setting" or "putting together" will be reused again during the process. Selected intermediate results will be shown as such on individual pages, like a photo documentation of your setting procedure. Other intermediate results will be reused without ever being shown on an individual page. To be spared from doing such steps over and over again, they are stored, either as layersetpage plus a number, as a mere layerset, or as a layer subset.
 
All of them are defined using the `\startuseMPgraphic` command. Each of them includes a specific set of the previously defined layers (or if you want, also drawn elements, as the "special!" label in layersetpage4). The layersets which are shown as a page are numbered, starting from one.
 
== Drawing the Pages ==
Finally, the code uses a loop to draw each layerset page onto its own page using the `\dorecurse` command. The loop iterates from 1 to a manually defined number, and for each iteration, it uses the `\useMPgraphic` command to draw the corresponding layersetpage, to which it is also passing the Metapost variable "mypagenumber".
 
The resulting output is a series of pages, each displaying a different selection and combination of the defined layers.
 
<texcode>
\startuseMPgraphic{base} %Here in the base, we define the components we will use to draw our images
numeric u; u = 1cm;
numeric framefactor; framefactor = 3.4;
path TheFrame ; TheFrame := fullsquare xyscaled (framefactor*u, framefactor*u) ; %Needed to have for every page the same dimensions. Use (sqrt(2)*framefactor*u, framefactor*u) for DIN A Landscape ratio of the dimensions.
color stone_color; stone_color = 1/256(199,199,199);
color light_teal_color; light_teal_color = 1/256(154,209,189);
 
z1 = (-u,-u) ;
z2 = (u,-u) ;
z3 = (-u,u) ;
z4 = (u,u) ;
z5 = (-.5u,0);
z6 = (.8u,0);
 
picture layerA;
layerA:=image(
fill fullsquare scaled u shifted(z1) withcolor stone_color;
label("A", z1);
);
 
picture layerW;
layerW:=image(
fill fullsquare scaled u shifted(z2) withcolor stone_color;
label("W", z2);
);
 
picture layerM;
layerM:=image(
fill fullsquare scaled u shifted(z3) withcolor blue;
label("M", z3) withcolor white;
);
 
picture layerC;
layerC:=image(
fill fullsquare scaled 1.5u shifted(z4) withcolor transparent(1, .8, green);
label("C", z4);
);
 
picture layerY;
layerY:=image(
fill fullsquare scaled 2u shifted(z5) withcolor light_teal_color;
label("Y", z5);
);
 
picture layerU;
layerU:=image(
fill fullsquare rotated 30 scaled 2u shifted(z4) withcolor transparent(1, .5, red);
label("U", z4);
);
 
picture layerV;
layerV:=image(
fill fullsquare rotated 10 scaled 1.4u shifted(z6) withcolor transparent(1, .7, yellow);
label("V", z6);
);
\stopuseMPgraphic %base
 
\startuseMPgraphic{finishing} %This is needed to clip the graphic to the same frame and to add the page numbering. Both have to happen after drawing the layers.
draw TheFrame withpen pencircle scaled .25u withcolor magenta ;
draw textext(decimal(\MPvar{mypagenumber})) scaled .1u withcolor magenta ;
setbounds currentpicture to TheFrame ;
%scaling the page to your preferred dimension:
desiredformat := 150;% In pixels (will be converted by default at 72dpi). The shorter dimension of DIN A4 would be 595 pixels.
currentpicture := currentpicture xysized (desiredformat ,desiredformat ); %for square pages. If DIN A Landscape, use (sqrt(2)*desiredformat ,desiredformat)
\stopuseMPgraphic %finishing
 
\startuseMPgraphic{Gamma} %This is a layer subset which will be used more than once. I call it a layer subset and not a layer set because it doesn't include another MPgraphic.
draw layerU ;
draw layerM ;
\stopuseMPgraphic %Gamma
 
\startuseMPgraphic{layersetpage1} %Each layersetpage will become a page containing the defined layer set. The number corresponds to the page number. Start with number 1.
\includeMPgraphic{base} %base has to be included only in layersetpage1. It will be passed on to the next layersetpages via inclusion.
draw layerW ;
draw layerA ;
\stopuseMPgraphic
 
\startuseMPgraphic{layersetpage2} %There should be no gap in the sequence of the numbers of the layersetpages
\includeMPgraphic{layersetpage1} ;
draw layerV ;
\stopuseMPgraphic
 
\startuseMPgraphic{layersetpage3}
\includeMPgraphic{layersetpage2} ;
\includeMPgraphic{Gamma} ;
\stopuseMPgraphic
 
\startuseMPgraphic{layersetpre4} %This is a layer set because it includes another MPgraphic. It will be used more than once, but it will not become a page itself. It's only an intermediate step on the way to the next layersetpage. For that reason, it's not named layersetpage. Because it's coming before the layersetpage4, I call it layersetpre4. If I need another intermediate step stored before layersetpre4, I would call it layersetprepre4. But that's only a naming convention for myself.
\includeMPgraphic{layersetpage1} ;
draw layerY ;
\stopuseMPgraphic
 
\startuseMPgraphic{layersetpage4}
\includeMPgraphic{layersetpre4} ;
label("special!", (z3+z4)/2) rotated 5 withcolor red;
\stopuseMPgraphic
 
 
 
\startuseMPgraphic{layersetpage5}
\includeMPgraphic{layersetpage3} ;
draw layerC ;
\stopuseMPgraphic
 
\startuseMPgraphic{layersetpage6}
\includeMPgraphic{layersetpre4} ;
draw layerC ;
\stopuseMPgraphic
 
\starttext
 
\dorecurse{6}{ %This loop starts from 1 until the set number and draws each layersetpage onto its own page. Don't set a number which is higher than the number of your last layersetpage.
 
%By differentiating finishinglayersetpage#1 from layersetpage#1, we can avoid adding \includeMPgraphic{finishing} to each \startuseMPgraphic{layersetpage<number>}. This allows us to apply the scaling of the page to our desiredformat, because the scaling will done only once per \useMPgraphic{}.
\startuseMPgraphic{finishinglayersetpage#1}
\includeMPgraphic{layersetpage#1} ;
\includeMPgraphic{finishing}
\stopuseMPgraphic
 
%uncomment if needed:
% \setupTEXpage[background=color, backgroundcolor=gray]
\startTEXpage[pagestate=start] %`pagestate=start` is needed to have incremented page numbering of the pdf
\useMPgraphic{finishinglayersetpage#1}{mypagenumber=#1}
\stopTEXpage
}
\stoptext
</texcode>
[[File:layer_sets.png|400px]]
= Other Links =
12

edits

Navigation menu