6

I am using ConTeXt and I would like to give my section heads a background extending slightly into the left and right margins (0.5 cm or so). I want the text itself to be in the usual position. I originally tried to do this with a framed (with no luck), and switched to trying a textbackground as suggested by a thread in the context mailing list archives.

I am able to extend the background in all directions by using backgroundoffset, but I only want it to be extended in the horizontal directions.

MLE:

\setuppapersize[A4]
\setupindenting[never]
\setuplayout[grid=yes]

\definetextbackground[headback][background=color, backgroundcolor=black, frame=off, location=paragraph] % if I add backgroundoffset=0.5cm I can get it to extend in ALL directions
\setuphead[subject][deeptextcommand=\headback, color=white]

\starttext
\startsubject[title={With an overhanging bar of colour}]

Some stuff

\stopsubject
\stoptext

Cross posting disclaimer: I also sent this to the ConTeXt mailing list Wednesday morning, but I'm still waiting for an admin to approve the email and my list subscription.

2 Answers 2

5

Here is a solution using framed. I simply set command=... key of \setuphead.I don't think that deeptextcommand is the right key as the makeup etc get applied after the deeptextcommand.

\showframe % for visualizing the page layout
\definemeasure[HeadFrameWidth][\dimexpr\textwidth+0.5cm\relax]
\defineframed
  [HeadFramed]
  [
    width=\measured{HeadFrameWidth},
    align=normal,
    background=color,
    backgroundcolor=black,
    foregroundcolor=white,
    % Play around with this setting if you wish
    toffset=0.5em,
    boffset=0.5em,
  ]
\define[2]\HeadCommand
    {\HeadFramed{#1#2}}

\setuphead[subject][command=\HeadCommand]

\starttext
\startsubject[title={With an overhanging bar of colour}]
Some stuff
\stopsubject
\stoptext

which gives

enter image description here

If you want the frame to extend in both directions, there are various ways to do that. One is to set the frame width to \textWidth + 0.5cm * 2, the loffset of the frame to be 0.5cm and add a \hskip -0.5cm before inserting the frame. So, effectively it looks like that the frame is extending to the left. (In the example below I changed the colors so that you can see the page margins drawn using \showframe:

\showframe % for visualizing the page layout

\definemeasure[HeadFrameWidth][\dimexpr\textwidth+0.5cm*2\relax]

\definecolor[gray][s=0.2,t=0.1,a=1]

\defineframed
  [HeadFramed]
  [
    width=\measured{HeadFrameWidth},
    align=normal,
    background=color,
    backgroundcolor=gray,
    foregroundcolor=black,
    loffset=0.5cm,
    % Play around with this setting if you wish
    toffset=0.5em,
    boffset=0.5em,
  ]
\define[2]\HeadCommand
    {\hskip -0.5cm\HeadFramed{#1#2}}

\setuphead[subject][command=\HeadCommand]

\starttext
\startsubject[title={With an overhanging bar of colour}]
Some stuff
\stopsubject
\stoptext

enter image description here

However, this is beginning to get very hackish. At this stage, it might be simpler to just draw the background using metapost.

\showframe % for visualizing the page layout

\definecolor[gray][s=0.2,t=0.1,a=1]

\startuseMPgraphic{extended}
  fill OverlayBox leftenlarged 0.5cm rightenlarged 0.5cm withcolor OverlayColor;
  setbounds currentpicture to OverlayBox;
\stopuseMPgraphic

\defineoverlay[extended][\useMPgraphic{extended}]

\defineframed
  [HeadFramed]
  [
    frame=off,
    width=broad,
    align=normal,
    background=extended,
    backgroundcolor=gray,
    foregroundcolor=black,
  ]
\define[2]\HeadCommand
    {\HeadFramed{#1#2}}

\setuphead[subject][command=\HeadCommand]

\starttext
\startsubject[title={With an overhanging bar of colour}]
Some stuff
\stopsubject
\stoptext
3
  • 1
    »makeup etc get applied after the deeptextcommand« What do you mean by that? In deeptextcommand you get the unprocessed title as an argument, in textcommand you get it with additional strut, style, and color. The dimensions of the page (which is what I understand by makeup) is already known at the point of deeptextcommand. Aug 5, 2017 at 5:31
  • 1
    @HenriMenke: I meant formatting commands etc. I haven't checked this, but from what I remember, the order is \command{\numbercommand{\numberformatting{\deepnumbercommand{<number>}}}\textcommand{\textformating{\deeptextcommand{<head>}}}}. deep...command are meant for things like \Uppercase.
    – Aditya
    Aug 5, 2017 at 5:41
  • I actually had something similar to this before when I was trying using framed, but I need the coloured background to extend to the left as well as right.
    – greenglass
    Aug 5, 2017 at 8:11
4

You can do it with background but not with textbackground. Anyway, I would use Aditya's solution.

\showframe

\setuppapersize[A4]
\setupindenting[never]
\setuplayout[grid=yes]

\definebackground
  [headback]
  [background=color,
   backgroundcolor=black,
   frame=off,
   roffset=.5cm]

\setuphead
  [subject]
  [deeptextcommand=\headback,
   color=white]

\starttext
\startsubject[title={With an overhanging bar of colour}]

Some stuff

\stopsubject
\stoptext

enter image description here

1
  • I need the coloured background to extend to the left as well as right. I have changed the question to make this clearer. I realised after viewing the answers that interpreting the original question the way I meant it depended on a single plural-s.
    – greenglass
    Aug 7, 2017 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.