4

Using ConTeXt for typesetting and metafun to draw, I would like to create a custom colored overlay for my table of contents. I want to fill a colored rectangle for certain TOC entries that stretches from their section number (or rather my fancy number alternative) to the right edge of the page. However whatever I try, my framed metafun overlay is always centered on the framed element, I can't draw asymmetrically off to one side.

Here is a minimal working example.

\setuphead[chapter][number=yes]
\setuphead[section][number=no]

%Draw in TOC
\startuseMPgraphic{TOChighlight}
path p;
p := origin  -- origin shifted (0,\overlayheight) -- origin shifted (PaperWidth,\overlayheight)  -- origin shifted (PaperWidth,0) --cycle;
fill p  withcolor .3red;
\stopuseMPgraphic

\defineoverlay[TOChighlight][\uniqueMPgraphic{TOChighlight}]

\define[1]\ChapterTOCNumber{ 
  \inframed[background=TOChighlight, frame=off]{
    \hbox to 3em{\sc \doifnot{\rawstructurelistuservariable{short}}{}{\[\structurelistuservariable{short}\]\crlf} } 
  }
}

\setuplist[chapter][width=1em,  numbercommand=\ChapterTOCNumber,  width=5em, alternative=c]
\setuplist[section] [margin=6em]


\starttext

\placecontent[criterium=all]

\startchapter[title=A Vision of The Future][short=What]
  \input tufte
  \section{What will happen?}
  \section{What will cause it?}
\stopchapter

\starttext
\startchapter[title=A Causal Account][short=Why]
  \input tufte
  \section{Why did it happen?}
  \section{Why wasn't it stopped?}
\stopchapter

\stoptext

Everything I try produces results like this:

Filled box is centered on the framed word "What"

When what I am trying to achieve is this:

enter image description here

For extra bonus points: I'd also like to continue drawing the color down the right hand edge only until the next chapter (i.e., not subsection) line. Can metapost know where the next chapter line is, even though when the draw command is invoked the next chapter's entry doesn't exist yet?

1 Answer 1

2

I am not sure I get exactly what you are after, changing the boundingbox of the MetaFun picture is perhaps what you want.

I have also simplified your path p as well as set an offset on the frame.

For the bonus question, I have no idea.

\setuphead[chapter][number=yes]
\setuphead[section][number=no]

%Draw in TOC
\startuseMPgraphic{TOChighlight}
path p;
p:=unitsquare xyscaled (PaperWidth,OverlayHeight);
fill p  withcolor .3red withtransparency (1,.5);
setbounds currentpicture to unitsquare xyscaled (OverlayWidth,OverlayHeight);
\stopuseMPgraphic

\defineoverlay[TOChighlight][\uniqueMPgraphic{TOChighlight}]

\define[1]\ChapterTOCNumber{ 
  \inframed[background=TOChighlight,offset=0pt,frame=off]{
    \hbox to 3em{\sc \doifnot{\rawstructurelistuservariable{short}}{}{\[\structurelistuservariable{short}\]\crlf} } 
  }
}

\setuplist[chapter][width=1em,  numbercommand=\ChapterTOCNumber,  width=5em, alternative=c]
\setuplist[section] [margin=6em]

\starttext

\placecontent[criterium=all]

\startchapter[title=A Vision of The Future][short=What]
  \input tufte
  \section{What will happen?}
  \section{What will cause it?}
\stopchapter

\starttext
\startchapter[title=A Causal Account][short=Why]
  \input tufte
  \section{Why did it happen?}
  \section{Why wasn't it stopped?}
\stopchapter

\stoptext

Result of compiling the code

1
  • Thank you, this seems right. For the bonus question I was thinking I could use intersections of page boundaries and coordinates defined with \hpos, but am failing to use \structurelistuservariable in combination with \hpos (see this question ). If you have any insight into that, I'd be very grateful.
    – Maciek
    Commented Feb 5, 2022 at 19:40

You must log in to answer this question.

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