5

I want to do some silly Metapost stuff with chapter titles, so set a command to begin with:

\setuphead[chapter][command=\ChapterTitle]

And on that custom command I threw the following to pass both number and title to a MetaPost drawing that should return a separate page:

\unexpanded\def\ChapterTitle#1#2{%
\setMPtext{number}{#1}
\setMPtext{title}{#2}
\startMPpage
\includeMPgraphic{chaptertitle}
\stopMPpage

Said drawing has a background and handles those variables.

\startuseMPgraphic{chaptertitle}{number,title}
StartPage;
fill unitsquare xyscaled( PaperWidth, PaperHeight ) withcolor red;
% Funky typographic stuff with number and title and more graphic thingies
StopPage;
\stopuseMPgraphic

All put together as a MWE:

\showframe

\setuppapersize[A8][A5,landscape]
\setuppaper[nx=4,ny=2]
\setuplayout[page][location=middle,marking=on]
\setuparranging[XY]

\setupbodyfont[8pt]

\startuseMPgraphic{chaptertitle}{number,title}
StartPage;
fill unitsquare xyscaled( PaperWidth, PaperHeight ) withpen pencircle scaled 1bp withcolor red;
StopPage;
\stopuseMPgraphic

\unexpanded\def\ChapterTitle#1#2{%
\setMPtext{number}{#1}
\setMPtext{title}{#2}
\startMPpage
\includeMPgraphic{chaptertitle}
\stopMPpage
}

\setuphead[chapter][command=\ChapterTitle]

\starttext
\startchapter[title={A chapter}]
\input ward
\stopchapter
\startchapter[title={Another chapter}]
\input tufte
\stopchapter
\startchapter[title={Another chapter}]
\input knuth
\stopchapter
\stoptext

I thought the \startMPpage...\stopMPpage commands must return a separate page, with no layout and adjusted to its drawing's size, but when coupled with the command key on \setuphead put the drawing inside a regular page, somewhat aligned to the textarea:

Output of MWE

As a workaround, I tried wrapping the code in the custom command with a makeup page, but can't set it to work. If I define a custom makeup, say

\definemakeup[chaptertitle]

and set a layout for it so it would be used automatically with that makeup,

\definelayout[chaptertitle][backspace=\zeropoint,header=\zeropoint,topspace=\zeropoint,margin=\zeropoint]

it will be the same thing, as it seems the layout and the makeup are not coupling, but then I won't know how to make that happen). And if I use the standard makeup just the top offset disappears but can't figure how to get rid of the left one.

And all of that makeup stuff is somewhat hacky. Played around with other arguments on \setuphead just in case, and (for my surprise) the margin one did affected the left offset, though it seems the left offset is equal to the backspace width plus something I can't guess. Still a workaround.

Not sure if I'm doing some rookie mistake or missing something silly, but how can I make those MetaPost page titles actually draw themselves as regular \startMPpage...\stopMPpages and not within regular pages?

0

2 Answers 2

2
+500

Something like this?

\showframe
\usecolors[svg]
\setuppapersize[A8][A5,landscape]
\setuppaper[nx=4,ny=2]
\setuplayout[page][location=middle,marking=on]
\setuparranging[XY]
\setupbodyfont[8pt]
\startuseMPgraphic{ChapterBG}
StartPage;
fill Page withcolor red;

%%% Just an example of what is possible here

fill fullcircle scaled 2cm shifted (1/4PaperWidth*left + center Page) withcolor resolvedcolor("turquoise");

%%% \structurenumber is the current structure (i.e. chapter)
%%% number. The same applies to \structuretitle

picture Pic;
Pic := image(draw outlinetext.d("\Romannumerals\structurenumber") (withcolor green);); 
draw Pic shifted (-center Pic) scaled 10 slanted 1/2 shifted center Page;
draw thetextext("\blue\ttbf\structuretitle",origin) scaled 2 rotated -45 shifted center Page;
StopPage;
\stopuseMPgraphic
\definelayer[ChapterBG]
%%% Our background setups
\startsetups lol
\setlayer[ChapterBG]{\useMPgraphic{ChapterBG}}%
\setupbackgrounds[page][background=ChapterBG]%
\page% Force a page break
\stopsetups
%%%
\setuphead[chapter]
    [placehead=no, %So no head in running text
     header=none,  %In order to avoid page numbers
     footer=none,  %Just in case
     insidesection=\setups{lol}] %Our setups are applied here
\starttext
\startchapter[title={A chapter}]
\input ward
\stopchapter
\startchapter[title={Another chapter}]
\input tufte
\stopchapter
\startchapter[title={Another chapter}]
\input knuth
\stopchapter
\stoptext

enter image description here

2

Found my way to another (hackish) solution, starting from some little clues from what Jairo A. del Río posted since I didn't wanted to deal with layers for the time being, and found out about \defineheadalternative which I think is rather a much more elegant approach.

However, while I was on that, I found what I think is rather a bug on MetaFun about paper sizes, which aren't exact and (partially) gave those weird gaps shown on the MWE from the initial question.

First, the full MWE:

\showframe
\setuppapersize[A8][A5,landscape]
\setuppaper[nx=4,ny=2]
\setuplayout[page][location=middle,marking=on]
\setuparranging[XY]

\setupbodyfont[8pt]

\startuseMPgraphic{chaptertitle}
StartPage;
path p;
p := unitsquare xyscaled(52mm, 74mm);
fill p withcolor red;
draw thetextext("\blue\headnumbercontent",origin) scaled 2 rotated 45 shifted center Page;
draw thetextext("\green\headtextcontent",origin) scaled 1 rotated 45 shifted center Page;
% your own stuffz with chapter titles and numbers
setbounds currentpicture to p;
StopPage;
\stopuseMPgraphic

\startsetups set:chaptertitle
\startMPpage[height=max]
\includeMPgraphic{chaptertitle}
\stopMPpage
\stopsetups

\defineheadalternative[ChapterTitle][renderingsetup=set:chaptertitle]

\setuphead[chapter][margin=-\backspace,alternative=ChapterTitle]

\starttext
\startchapter[title={A chapter}]
\input ward
\stopchapter
\startchapter[title={Another chapter}]
\input tufte
\stopchapter
\startchapter[title={Another chapter}]
\input knuth
\stopchapter
\stoptext

Which produces this:

Output from MWE

While tinkering with it a lot, I found out the leftmost gaps in OP MWE where the same size as the backspace dimension, so setting

\setuphead[chapter][margin=-\backspace]

got rid of them. No big deal there but I think that shouldn't be needed. To get rid of the top gap, I found out \startMPpage inherits \setupframed attrs, so after tinkering a lot more with that I found out setting height=max set it almost exactly at the top border.

After many hours and headaches tinkering with this, I found it seems MetaPost/MetaFun's PaperWidth or \the\paperwidth (and PaperHeight or \the\paperheight) won't output exactly the same size as ConTeXt's \paperwidth (and \paperheight). So if I set the drawing's dimensions from

p := unitsquare xyscaled(PaperWidth, PaperHeight);

to

p := unitsquare xyscaled(52mm, 74mm); % those being the dimensions of A8 paper size

that little gap at the top was gone.

Epilogue

I still think those \setuphead[chapter][margin=-\backspace] and height=max things should not be needed in this case and the result I was getting was more product of a rather unintuitive behavior (or bug) from ConTeXt in this specific scenario.

Update

As I fiddled with this a bit more since I posted the question originally, figured out how to couple a new layout with a makeup setup and use it as the page that holds the graphic - so the [height=max] and margin=-\backspace hacks are not needed anymore:

\showframe
\setuppapersize[A8][A5,landscape]
\setuppaper[nx=4,ny=2]
\setuplayout[page][location=middle,marking=on]
\definelayout[chapter][margindistance=\zeropoint,edgedistance=\zeropoint,backspace=\zeropoint,top=\zeropoint,topspace=\zeropoint,header=\zeropoint,headerdistance=\zeropoint]% ...basically making the textarea fill the whole page
\definemakeup[chapter][pagestate=start]
\setuparranging[XY]

\setupbodyfont[8pt]

\startuseMPgraphic{chaptertitle}
StartPage;
path p;
p := unitsquare xyscaled(52mm, 74mm);
fill p withcolor red;
draw thetextext("\blue\headnumbercontent",origin) scaled 2 rotated 45 shifted center Page;
draw thetextext("\green\headtextcontent",origin) scaled 1 rotated 45 shifted center Page;
% your own stuffz with chapter titles and numbers
setbounds currentpicture to p;
StopPage;
\stopuseMPgraphic

\startsetups set:chaptertitle
\startMPpage
\includeMPgraphic{chaptertitle}
\stopMPpage
\stopsetups

\defineheadalternative[ChapterTitle][renderingsetup=set:chaptertitle]

\setuphead[chapter][alternative=ChapterTitle,before={\startmakeup[chapter]},after={\stopmakeup}]

\starttext
\startchapter[title={A chapter}]
\input ward
\stopchapter
\startchapter[title={Another chapter}]
\input tufte
\stopchapter
\startchapter[title={Another chapter}]
\input knuth
\stopchapter
\stoptext

Also, found out that setting grid=yes messes with the vertical position of the graphic for some reason, so for that new layout it's better to set it with grid=no.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .