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.

I don't know if this is a duplicate or not but we can close it if it is.

I have been trying to learn ConTeXt. I specially love the heading and page number formattings that I see in the documentations that I have downloaded, achieved through the use of Metapost/Metafun. I have been trying to achieve this effect in \chapter (produced in LaTeX through egreg's gmp package.

enter image description here

To this end I have copied and pasted codes from several files found from here. This is the output I am getting for now.

enter image description here

with this code.

\setuppapersize[A4][A4]
\setuplayout[topspace=0.5in, backspace=1in, header=24pt, footer=36pt,
  height=middle, width=middle]

\def\MPclipOne#1#2#3#4#5%
  {\startuseMPgraphic{clip:one}
     w := #1;  width  := 100;  wfactor := w/width;
     h := #2;  height := 100;  hfactor := h/height;
     %
     color lightred;  lightred  := (.90,.50,.50);
     color lightgray; lightgray := (.95,.95,.95);
     color gray;      gray      := (.50,.50,.50);
     %
     def random_delta (expr d) =
       d-(uniformdeviate 2d)
     enddef;
     %
     z1 = (0,height);
     z2 = (0,0);
     z3 = (width,0);
     z4 = (width,height);
     %
     z5 = (width+random_delta(.2width),height+random_delta(.2height));
     z6 = (.5width+random_delta(.1width),height+random_delta(.1height));
     %
     pickup pencircle
       xscaled (#3/wfactor)
       yscaled (#3/(2*hfactor))
       rotated 30;
     %
     draw z5..z1..z2..z3..z4..z6 withcolor #4;
     %
     pickup pencircle
       xscaled (#3/wfactor)
       yscaled (#3/hfactor);
     %
     draw z1 withcolor #5;
     draw z2 withcolor #5;
     draw z3 withcolor #5;
     draw z4 withcolor #5;
     draw z5 withcolor #5;
     draw z6 withcolor #5;
     %
     newwidth  := (xpart (urcorner currentpicture)) -
                  (xpart (llcorner currentpicture));
     newheight := (ypart (urcorner currentpicture)) -
                  (ypart (llcorner currentpicture));
     %
     currentpicture := currentpicture
      xscaled (w/newwidth) yscaled (h/newheight);
   \stopuseMPgraphic
   \useMPgraphic{clip:one}{}}

\defineoverlay
  [KopAchtergrond]
  [\MPclipOne{\overlaywidth}{\overlayheight}{10pt}{gray}{red}]

\def\HeadCommand#1#2%
  {\alignedline{\v!outer}{\v!left}
     {\framed
        [\c!background=KopAchtergrond,
         \c!width=\v!fit,
         \c!height=\v!fit,
         \c!frame=\v!off,
         \c!strut=\v!no,
         \c!offset=24pt,
         \c!align=\v!middle]
        {\doifmode{*sectionnumber}
           {#1\kern.5em% strut niet geset, zou wel moeten
            \hbox{\color[headrule]{\vrule\!!width1pt\!!height1.5\ht\strutbox\!!depth1.25\dp\strutbox}}%
            \kern.5em}%
         #2}}}

\setuphead
  [\v!chapter]
  [\c!command=\HeadCommand,
   \v!appendix\c!label=none,
   \c!page=,
   \c!before=\vskip36pt plus 6pt minus 6pt,
   \c!after=\vskip24pt]

\starttext

\chapter{Sample chapter}

\stoptext

Please make me understand what is happening here and how to achieve my desired effect?

share|improve this question
add comment

1 Answer 1

up vote 14 down vote accepted
+100

You are missing a \unprotect .. \protect block, and a \unexpanded.

\setuppapersize[A4][A4]
\setuplayout[topspace=0.5in, backspace=1in, header=24pt, footer=36pt,
  height=middle, width=middle]

\unprotect
\def\MPclipOne#1#2#3#4#5%
  {\startuseMPgraphic{clip:one}
     w := #1;  width  := 100;  wfactor := w/width;
     h := #2;  height := 100;  hfactor := h/height;
     %
     color lightred;  lightred  := (.90,.50,.50);
     color lightgray; lightgray := (.95,.95,.95);
     color gray;      gray      := (.50,.50,.50);
     %
     def random_delta (expr d) =
       d-(uniformdeviate 2d)
     enddef;
     %
     z1 = (0,height);
     z2 = (0,0);
     z3 = (width,0);
     z4 = (width,height);
     %
     z5 = (width+random_delta(.2width),height+random_delta(.2height));
     z6 = (.5width+random_delta(.1width),height+random_delta(.1height));
     %
     pickup pencircle
       xscaled (#3/wfactor)
       yscaled (#3/(2*hfactor))
       rotated 30;
     %
     draw z5..z1..z2..z3..z4..z6 withcolor #4;
     %
     pickup pencircle
       xscaled (#3/wfactor)
       yscaled (#3/hfactor);
     %
     draw z1 withcolor #5;
     draw z2 withcolor #5;
     draw z3 withcolor #5;
     draw z4 withcolor #5;
     draw z5 withcolor #5;
     draw z6 withcolor #5;
     %
     newwidth  := (xpart (urcorner currentpicture)) -
                  (xpart (llcorner currentpicture));
     newheight := (ypart (urcorner currentpicture)) -
                  (ypart (llcorner currentpicture));
     %
     currentpicture := currentpicture
      xscaled (w/newwidth) yscaled (h/newheight);
   \stopuseMPgraphic
   \useMPgraphic{clip:one}{}}

\defineoverlay
  [KopAchtergrond]
  [\MPclipOne{\overlaywidth}{\overlayheight}{10pt}{gray}{red}]

\unexpanded\def\HeadCommand#1#2%
  {\alignedline{\v!outer}{\v!left}
     {\framed
        [\c!background=KopAchtergrond,
         \c!width=\v!fit,
         \c!height=\v!fit,
         \c!frame=\v!off,
         \c!strut=\v!no,
         \c!offset=24pt,
         \c!align=\v!middle]
        {\doifmode{*sectionnumber}
           {#1\kern.5em% strut niet geset, zou wel moeten
            \hbox{\color[headrule]{\vrule\!!width1pt\!!height1.5\ht\strutbox\!!depth1.25\dp\strutbox}}%
            \kern.5em}%
         #2}}}

\setuphead
  [\v!chapter]
  [\c!command=\HeadCommand,
   \v!appendix\c!label=none,
   \c!page=,
   \c!before=\vskip36pt plus 6pt minus 6pt,
   \c!after=\vskip24pt]
\protect

\starttext

\chapter{Sample chapter}

\stoptext

But this is really old ConTeXt code. Here the communication between TeX and Metapost is through macro parameters. I would just use key-value parameters for the communication. Here is one possible solution, where I use dotcolot key to specify the color of the dot.

\setupexternalfigures[location={local,global}]

\startuseMPgraphic{randomcircle}
   width  := 100;  wfactor := OverlayWidth/width;
   height := 100;  hfactor := OverlayHeight/height;
   %
   color dotcolor; dotcolor := \MPcolor{\framedparameter{dotcolor}};

   def random_delta (expr d) =
     d-(uniformdeviate 2d)
   enddef;
   %
   z1 = (0,height);
   z2 = (0,0);
   z3 = (width,0);
   z4 = (width,height);
   %
   z5 = (width, height)    randomized (0.2width, 0.2height);
   z6 = (0.5width, height) randomized (0.1width, 0.1height);
   %
   pickup pencircle
     xscaled (OverlayLineWidth/wfactor)
     yscaled (OverlayLineWidth/(2*hfactor))
     rotated 30;
   %
   draw z5..z1..z2..z3..z4..z6 withcolor OverlayLineColor;
   %
   pickup pencircle
     xscaled (OverlayLineWidth/wfactor)
     yscaled (OverlayLineWidth/hfactor);
   %

   draw z1 withcolor dotcolor;
   draw z2 withcolor dotcolor;
   draw z3 withcolor dotcolor;
   draw z4 withcolor dotcolor;
   draw z5 withcolor dotcolor;
   draw z6 withcolor dotcolor;
   %
   newwidth  := (xpart (urcorner currentpicture)) -
                (xpart (llcorner currentpicture));
   newheight := (ypart (urcorner currentpicture)) -
                (ypart (llcorner currentpicture));
   %
   currentpicture := currentpicture
    xscaled (OverlayWidth/newwidth) yscaled (OverlayHeight/newheight);
\stopuseMPgraphic

\defineoverlay
  [randomcircle]
  [\useMPgraphic{randomcircle}]

Now the overlay randomcircle can be used with any command that accepts an overlay. For example, a frame and a figure:

enter image description here

which was obtained using:

\definecolor[gray][r=0.5, g=0.5, b=0.5]


\starttext

\startTEXpage[offset=10mm]

  \dontleavehmode
  \framed[background=randomcircle, dotcolor=red, framecolor=gray, offset=24pt, rulethickness=10pt, frame=off]
         { Hello World}
  \hskip 6em 
  \externalfigure[cow.pdf]
                 [width=3cm, 
                  background=randomcircle, dotcolor=red, framecolor=gray,
                  backgroundoffset=24pt, rulethickness=10pt, frame=off]

\stopTEXpage

\stoptext

Now, getting the graphic around the section heading is simply a matter of setting the command key of \setuphead.

share|improve this answer
    
Thanks! I was suspecting it was very old code since the repository for the files where I downloaded the code is dated 2010. But I couldn't any of the updated codes anywhere. Anyway, thank you very much and am anticipating your updated answer! –  hpesoj626 Apr 10 '13 at 3:45
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.