# [NTG-context] Metapost label alignment etc.

Aditya Mahajan adityam at umich.edu
Thu Jul 6 00:27:04 CEST 2006

On Fri, 30 Jun 2006, Idris Samawi Hamid wrote:

> Hi, Mojca, Aditya, Hans, and all the MP gurus,
>
> Thank you for all the help and advice.
>
> On Fri, 30 Jun 2006 18:17:23 -0600, Mojca Miklavec
> <mojca.miklavec.lists at gmail.com> wrote:
>
> Wow... Ok, following is my present, still naive code. What I would like to
> be able to do is
>
> 1) In the first figure [square], control the relation of the front and
> back face of the cube by a single parameter. So if I use z for the front
> face coordinates, I can use z' for the rear face and define z' as a
> function of z. Then the angle of the z111--z'111 diagonal wrt the x-axis
> will change, and the label for that axis will have to change as well. I've
> been manually calculating inverse tangents to do this but can it be done
> automatically?

angle(...).

> 2. In the second figure, to in addition control the distance between the 2
> cubes by a single parameter without recalculating the dashed lines mapping
> the two.

See code below.

> I only started yesterday but i can see that MetaPost is going to turn me
> into a junkie if I'm not careful :-)
>
> Any other suggestions for improving/streamlining this is greatly
> appreciated: I think it will make a nice example case for beginners/on the
> wiki when done!

I do not have an ingeneous solution like Mojca. I will do this in a
brute force way like you, but using loops for repetitive tasks and
allowing metapost to do all the calculations for me.

\setuppapersize[letter][letter]
\setupcolors[state=start]
\setuppagenumbering[state=stop]

\def\AXIS#1#2#3{(#1) Axis #2: {\bf #3}}

\forceMPTEXcheck{LABELL}
\forceMPTEXcheck{LABELR}
\forceMPTEXcheck{LABELM}

\startMPenvironment
\ssxx\setupinterlinespace
\stopMPenvironment

\startMPinclusions
vardef LABELL@\#(expr s, p)= label@\#(textext("%
\framed[width=1.5cm,align=left,frame=off,autowidth=force,strut=on]{"
& s &
"}"),p)
enddef ;

vardef LABELR@\#(expr s, p)= label@\#(textext("%
\framed[width=1.5cm,align=right,frame=off,autowidth=force,strut=on]{"
& s
& "}"),p)
enddef ;

vardef LABELM@\#(expr s, p)= label@\#(textext("%
\framed[align=middle,frame=off,autowidth=force,strut=on]{" & s &
"}"),p)
enddef ;

picture my_dash ; my_dash := dashpattern(on 1mm off 1mm) ;

% my_dash is more verbose than p.

\stopMPinclusions

\startstaticMPfigure{square}
numeric u ; u := 1.4cm;
% Always declare the type of a variable. Helps in debugging.

path p; p := unitsquare scaled 5u ;
% Identify the corners
z = llcorner p ;
z = ulcorner p ;
z = urcorner p ;
z = lrcorner p ;
% Identify the corners of the shifted rectangle
for i = 5 upto 8:
z[i] = z[i-4] shifted (2u, 1.25u) ;
endfor ;
% Find the intersection points
z  = whatever[z,z] = whatever[z,z] ;
z = whatever[z,z] = whatever[z,z] ;
% Axis location
% No need to calculate these explicitly, you can as well use
% label(p, 0.5[...]) ;
z = 0.5[z,z] ;
z = 0.5[z,z] ;
z = 0.5[z,z] ;
%  z = (1u,0.15u) ; Not sure which point you had calculated.

drawoptions(withcolor .625red withpen pencircle scaled 0.4pt) ;

draw p ;
draw (z  -- z) ;
draw (z  -- z) ;
draw (z  -- z) ;
draw (z -- z) ;
%  draw (z  -- z) ;
%  draw (z  -- z) ;
%  draw (z  -- z) ;
for i = 2 upto 4 :
draw(z[i] -- z[i+4]) ;
endfor ;
drawoptions(withcolor .625red dashed my_dash withpen pencircle scaled
0.4pt) ;

%  draw (z -- z)  ;
%  draw (z -- z)  ;
%  draw (z -- z) ;
for i = 1,9,10 :
draw(z -- z[i] ) ;
endfor ;

drawoptions(withcolor black) ;
LABELL.llft("(0,0,0,0) Light Condensed Small Sans~Serif", z) ;
LABELL.llft("(0,1,0,0) Light Extended  Small Sans~Serif", z) ;
LABELR.lrt ("(1,1,0,0) Bold  Extended  Small Sans~Serif", z) ;
LABELR.lrt ("(1,0,0,0) Bold  Condensed Small Sans~Serif", z) ;

LABELR.urt("(0,0,1,0)  Light Condensed Large Sans~Serif", z) ;
LABELR.urt("(0,1,1,0)  Light Extended  Large Sans~Serif", z) ;
LABELR.urt("(1,1,1,0)  Bold  Extended  Large Sans~Serif", z) ;
LABELR.urt("(1,0,1,0)  Bold  Condensed Large Sans~Serif", z) ;

LABELM.bot ("\AXIS{x}{1}{Weight}", z);

picture p; p :=
textext("\framed[align=middle,frame=off,autowidth=force,strut=on]%
{\AXIS{y}{2}{Width}}");
p := p rotatedaround (z, 90) ;

label.lft(p,z);

p := textext("\AXIS{z}{3}{Optical Size}");
p := p rotatedaround (z, angle(z-z)) ;
% inv tan(1.5/2)
% Let metapost calculate it for you

% Ugly kludge
label(p,z+(0,2labeloffset));

%  currentpicture := currentpicture scaled 0.9 ;
%  Why? Either change u, or use scaled= option in \usestaticMPfigure

\stopstaticMPfigure

\startstaticMPfigure{2squares}

numeric u ; u := 0.7cm;
path p; p := unitsquare scaled 5u ;
% Identify the corners
z = llcorner p ;
z = ulcorner p ;
z = urcorner p ;
z = lrcorner p ;
% Identify the corners of the shifted rectangle
for i = 5 upto 8:
z[i] = z[i-4] shifted (2u, 1.25u) ;
endfor ;

for i = 11 upto 18:
z[i] = z[i-10] shifted (10u,-2.5u) ;
endfor ;

z  = (3.125u,3.125u) ;
z  = (13.125u,0.625u) ;
z  = (5u,-1.25u) ;

drawoptions(withcolor .625red withpen pencircle scaled 0.4pt) ;

for i = 0,10:
draw (z[i+1]  -- z[i+2] -- z[i+3] -- z[i+4] -- cycle) ;
draw (z[i+5]  -- z[i+6] -- z[i+7] -- z[i+8] -- cycle) ;
draw (z[i+1] -- z[i+5]) ;
draw (z[i+2] -- z[i+6]) ;
draw (z[i+3] -- z[i+7]) ;
draw (z[i+4] -- z[i+8]) ;
endfor ;

% Remove repetitions
%  draw (z  -- z -- z -- z -- cycle) ;
%  draw (z  -- z -- z -- z -- cycle) ;
%  draw (z -- z) ;
%  draw (z -- z) ;
%  draw (z -- z) ;
%  draw (z -- z) ;

% map square to square

drawoptions(withcolor .625red dashed my_dash withpen pencircle scaled
0.4pt) ;
for i = 1 upto 8 :
draw z[i] -- z[i+10] ;
endfor ;

%  draw (z -- z) ;
%  draw (z -- z) ;
%  draw (z -- z) ;
%  draw (z -- z) ;
%  draw (z -- z) ;
%  draw (z -- z) ;
%  draw (z -- z) ;
%  draw (z -- z) ;

% labels

drawoptions(withcolor black) ;

label (textext("Sans Serif"),z) ;
label (textext("Serif"),z) ;

picture p; p := textext("\AXIS{a}{4}{Serif}");
%  p := p rotatedaround (z, -14.036) ;
p := p rotatedaround (z, angle(z-z)) ;

label.bot(p,z);

%  currentpicture := currentpicture scaled 0.9 ;

\stopstaticMPfigure

\starttext

\midaligned{\usestaticMPfigure[square]}

\blank[2*big]

\midaligned{\usestaticMPfigure[2squares]}

\stoptext

Aditya


More information about the ntg-context mailing list