# How can I extend textbackground horizontally in ConTeXt?

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

\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.

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
\defineframed
[
align=normal,
background=color,
backgroundcolor=black,
foregroundcolor=white,
% Play around with this setting if you wish
toffset=0.5em,
boffset=0.5em,
]

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

which gives

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

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

\defineframed
[
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,
]

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

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
[
frame=off,
align=normal,
background=extended,
backgroundcolor=gray,
foregroundcolor=black,
]

\starttext
\startsubject[title={With an overhanging bar of colour}]
Some stuff
\stopsubject
\stoptext
``````
• »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
• @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`. 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. Aug 5, 2017 at 8:11

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
[background=color,
backgroundcolor=black,
frame=off,
roffset=.5cm]

[subject]