How can I extend textbackground horizontally in ConTeXt?
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
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
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
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