8

I want to place background images behind all of my part titles, with the image spanning across two pages, e.g.:

 ___________ ___________
|           |           |
| Part 1    |           |
| Birds of  |           |
| South     |           |
| America   |           |
|___________|___________|

So far, I have created this code:

\definelayer[patternleft][x=0mm, y=0mm, width=\paperwidth, height=\paperheight]
\definelayer[patternright][x=0mm, y=0mm, width=\paperwidth, height=\paperheight]

\setlayer[patternleft]                {%
    \framed[frame=off, align=right]{%
        \clip[nx=2, ny=1, x=1, y=1]{%
            \externalfigure[background.jpg][factor=500]}%
    }%
}%

\setlayer[patternright]                {%
    \framed[frame=off, align=left]{%
        \clip[nx=2, ny=1, x=2, y=1]{%
            \externalfigure[background.jpg][factor=500]}%
    }%
}%

I then put it in the document like this:

\setupbackgrounds[page][background=patternleft] % sets background for verso
\part{Birds of South America}

\page[right]
\setupbackgrounds[page][background=patternright] % sets background for rector
\page[right]
\setupbackgrounds[page][background={}] % sets a blank background

    This is some text.

This has the following problems:

  • The images are the same size as the twp pages, however, white space remains along the margins and fold.
  • This sometimes places the backgrounds on the wrong pages.

How can I improve this code so that the background graphic spans across verso and recto pages on the \part titles?

1
  • 1
    Split the image into two part (see \clip) and use the left part as the background for one page and the right part as the background for the other page.
    – Aditya
    Jun 12, 2013 at 15:55

1 Answer 1

9
+150

To expand on the approach @Aditya was hinting at in his comment, you can chop whatever picture you choose as the background into fitting pieces using the \clip macro. First, set a partitioning grid using \setupclipping, e.g.:

\setupclipping[nx=2,ny=4]

This defines clipping to use two segments horizontally and four segments vertically. You can then address these parts using their coordinates:

\clip[x=2,y=3]{...}

This extracts the piece that is located at the third column of the fourth row of whatever the macro is applied to. Your doublesided background is a simpler problem as it requires only a single horizontal split:

\setupclipping[nx=2,ny=1]
%% left half:
\clip[x=1,y=1]{\externalfigure[...][width=2\paperwidth]}
%% right half:
\clip[x=2,y=1]{\externalfigure[...][width=2\paperwidth]}

Now you just have to draw these parts as the background. This is no problem in Context as all the elements that constitute the page layout are derived from \framed, inheriting its options. Thus, in order to draw an image as a page background you can use the same strategy as with \framed: wrap the content in an overlay and assign it to the background. The element that spans one entire page is called paper, and you can configure it via \setupbackgrounds:

\defineoverlay [background:overlay] [\backgroundcmd]
\setupbackgrounds [paper] [background=overlay:background]

What’s left is to define \backgroundcmd so that it clips the left or right half depending on whether the current page is recto or verso:

\def\backgroundcmd{
  \ifodd\pagenumber
    \clip[x=2,y=1]{\externalfigure[...]}
  \else
    \clip[x=1,y=1]{\externalfigure[...]}
  \fi%
}

Combining everything, a solution could look like this:

doublesided page background

\setupexternalfigures [location={default}] %% figure search path
\setuppagenumbering [alternative=doublesided]

\unprotect

%% 1) a named figure for inclusion, scaled to twice the width of one
%%    page
\useexternalfigure [half_cow] [cow.pdf] [
  width=2\paperwidth,
  height=\paperheight,
]

%% 2) a command that draws the left or right portion of a figure,
%%    depending on whether the page is recto or verso.
%%
%%    also, we install a counter to check if the background should be
%%    placed at all, since it is only desired with “\part” headings.
%%    this counter will be decremented with every invocation and reset
%%    whenever a new part begins.

\newcount\cowcount

\def\pickhalfcow_cmd{%
  \setupclipping [nx=2,ny=1,]%
  \ifnum\cowcount>0
    \ifodd\pagenumber                           %% clip right half
      \clip[x=2,y=1]
    \else                                       %% clip left half
      \clip[x=1,y=1]
    \fi{\externalfigure[half_cow]}%
    \global \advance \cowcount \minusone
  \fi%
}

%% 3a) define an overlay that we can use as page background
\defineoverlay [pickhalfcow] [\pickhalfcow_cmd]

%% 3b) hook the overlay into the “paper” element of the layout
\setupbackgrounds [paper] [background=pickhalfcow]

\setuphead [part] [
  page=left, %% assumption: parts always begin verso
  insidesection=\cowbackgrounds,
]

%% 4) define auxiliary macro that resets the background image counter
\def\cowbackgrounds{\global\cowcount=2}

\protect

%% Ready. Here’s some code that generates a couple pages.
\starttext
  \startcolor[red]

    \definedfont[name:IwonaMediumRegular at 22pt]
    \setupinterlinespace[25pt]

    \startpart[title=South America etc.]
      \dorecurse{5}{
        \input knuth \page
    }
    \stoppart

    \startpart[title=Birds And Such]
      \dorecurse{5}{
        \input knuth \page
    }
    \stoppart

  \stopcolor
\stoptext

Update: Hooked the code into the part headings.

1
  • is there a way to wrap this in a function so you could import an image i.png from within a \starttext \fun[img/i.png] \stoptext and it clips/pans automatically across the next 2 pages?
    – Georges
    Apr 18, 2019 at 9:07

You must log in to answer this question.

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