[Tex/LaTex] ConTeXt: Set margin background colour for all chapter pages

backgroundschapterscolorcontextmargins

Problem

The \setupbackgrounds macro has the ability to change the background colour of the margins, from the top of the page to the bottom:

  \setupbackgrounds[footer,header,text][rightmargin][
    background=color,
    backgroundcolor=blue,
  ]

However, once set, there does not seem to be a way to stop the backgrounds. This is preventing me from setting the background colour for only the chapter pages.

I'm fairly certain I could use a layer for this, but since \setupbackgrounds already has context for the right margin's header, footer, and text areas, I thought it would be easiest to commandeer this macro:

  \setuphead[chapter][
    header=empty,
    footer=empty,
    before={%
      \setupbackgrounds[footer,header,text][rightmargin][
        background=color,
        backgroundcolor=blue,
        state=repeat,
      ]
    },
    after={%
      \setupbackgrounds[footer,header,text][rightmargin][
        background=color,
        backgroundcolor=blue,
        state=stop,
      ]
    }
  ]

The state=stop doesn't stop the margin from being blue after the chapter page. Rather, the blue continues in the margin for the remainder of the document. I tried to stop the blue within the section:

  \setuphead[section][
    before={%
      \setupbackgrounds[][][state=stop]
    }
  ]

Question

Is it possible to use a \setupbackground in this way (if so, how)?

If not, how would you use a layer to fill in the right-hand margin from the top of the page to the bottom?

Related

Best Answer

One option is to use Metapost backgrounds and use the trick described in Conditional page backgrounds in ConTeXt.

\startMPinclusions
  numeric ChapterPageDone[];
\stopMPinclusions

\startuseMPgraphic{chapterbackground}
  StartPage;
  n := \somenamedheadnumber{chapter}{current};

  x0 := PaperWidth - BackSpace;
  x1 := x0 + RightMarginWidth;

  if n > 0 : % ignore pages before the first chapter
    if unknown ChapterPageDone[n] : % This is the first page a new chapter
          fill (x0,0) -- (x1,0) -- (x1, PaperHeight) -- (x0, PaperHeight) -- cycle
               withcolor \MPcolor{blue};
        ChapterPageDone[n] := 1 ;
    fi;
  fi;
  StopPage;
\stopuseMPgraphic

\defineoverlay[chapterbackground][\useMPgraphic{chapterbackground}]
\setupbackgrounds[page][background=chapterbackground]

\setuppapersize[A6]

\starttext

Non chapter page

\chapter{First}
Chapter page
\page

Non chapter page

\chapter{Second}
Chapter page

\stoptext

which gives

enter image description here

EDIT Based on the comment below, the above method will not work for unnumbered section heads. In that case, one option is to check if there has been a change in the title of chapter. Such an approach will work as long as you do not have two chapters with identical titles.

\startMPinclusions
  string prevChapterTitle, currentChapterTitle;
  prevChapterTitle := "";
  currentChapterTitle := "";
\stopMPinclusions

\startuseMPgraphic{chapterbackground}
  StartPage;
  currentChapterTitle := "\namedstructurevariable{chapter}{title}";

  x0 := PaperWidth - BackSpace;
  x1 := x0 + RightMarginWidth;

  if currentChapterTitle <> prevChapterTitle :
        fill (x0,0) -- (x1,0) -- (x1, PaperHeight) -- (x0, PaperHeight) -- cycle
             withcolor \MPcolor{blue};
     prevChapterTitle := currentChapterTitle ;
  fi;
  StopPage;
\stopuseMPgraphic

\defineoverlay[chapterbackground][\useMPgraphic{chapterbackground}]
\setupbackgrounds[page][background=chapterbackground]

To exclude the table of contents page, make the following minor change:

if prevChapterText <> "":
  fill (x0,0) -- (x1,0) -- (x1, PaperHeight) -- (x0, PaperHeight) -- cycle
    withcolor \MPcolor{blue};
fi;