Layers

From Wiki
Revision as of 13:46, 25 April 2012 by Esteis (talk | contribs)
Jump to navigation Jump to search

< Layout | Columns | Overlays | Logos | Collectors >

Layers are ConTeXt's mechanism for absolute positioning of elements and other advanced techniques like switching elements on and off.

There's still no manual about them.

My first Layer

Define a layer that takes the whole page

\definelayer[mybg]	% name of the layer
    [x=0mm, y=0mm,	% from upper left corner of paper
     width=\paperwidth, height=\paperheight] % let the layer cover the full paper

Now you can put something in that layer:

\setlayer[mybg]	% name of the layer
    [hoffset=1cm, voffset=1cm]  % placement (from upper left corner of the layer)
     {\framed[frame=on, width=3cm, height=2cm]{LAYER}}  % the actual contents of the layer

Activate the layer as a background:

\setupbackgrounds[page][background=mybg]

This command makes the contents of the layer appear only once after the background is activated. If you want to repeat the contents of the layer on each page, use the option repeat=yes in the \definelayer command. Then the contents of the layer will be shown on every page. You can add to these contents by a new \setlayer[mybg] command. To clear the accumulated contents use \resetlayer[mybg]. To make the layer appear on each page, so that it can be populated with different content, set the optionstate=repeat in \setupbackgrounds.

Now you can test the whole thing:

\setuppapersize[A10,portrait][A9,landscape]  % A10 = 2.6x3.7cm -- compact example
\setuparranging[2SIDE] % show both pages
\showframe % So we can see the pages

\definelayer[mybg]  % name of the layer
    [x=0mm, y=0mm,  % from upper left corner of paper
     width=\paperwidth, height=\paperheight, % let the layer cover the full paper
     state=continue]   % on all pages but the first one

\setupbackgrounds[page][background=mybg]

\setlayer[mybg]  % name of the layer
    [hoffset=0.3cm, voffset=0.7cm]  % placement (from upper left corner of the layer)
     {\framed[frame=on, width=2cm, height=2cm]{LAYER}}  % the actual contents of the layer

\starttext

\dorecurse{2}{\page[empty]} % for testing, to get 2 pages, 2nd with layer, 1st without

\stoptext


Placement

There are several possibilities for defining the placement of layer content:

  • x, y : offset from upper left corner of paper [=the current block; can sometimes be the text area instead of the page/paper!]
  • hoffset, voffset : offset from upper left corner of layer
  • corner : reference point, something like {left, top}
  • location : alignment of the element relative to the corner, something like {right, bottom}
  • preset : a named location, see below

There are some "presets" for paper egde placement:

% These four are defined by ConTeXt!
\definelayerpreset	[lefttop]	[corner={left,top}, location={right,bottom}]
\definelayerpreset	[righttop]	[corner={right,top}, location={left,bottom}]
\definelayerpreset	[leftbottom]	[corner={left,bottom}, location={right,top}]
\definelayerpreset	[rightbottom]	[corner={right,bottom}, location={left,top}]

Similarly you can define your own presets.

State

The available options for the "state" of a layer are:

  • start: layer appears only on the current page
  • stop: layer doesn't show up
  • repeat: layer prints on all pages
  • next: layer appears on the following page
  • continue: layer appears on all pages except the first

Line breaking and fixed size

You may experience cases where the defined size of your layer is ignored and you can’t get linebreaking or multiple lines at all. This is due to \setlayer using \hbox internally. You can work around the problem using \setlayerframed

This does not work:

\showframe

\definelayer[AddressBg][
    x=0mm, y=0mm,
    width=65mm, height=30mm,
    state=start] % size is ignored!
\setupbackgrounds[paper][
    setups=ALayer,
    background=AddressBg,
    state=start]

\starttext

\strut
\startsetups ALayer
    \setlayer[AddressBg]          % setlayer does not work here
       [width=65mm, height=30mm, 
        frame=off,
        hoffset=0mm, voffset=0mm,
        align=right]%
    {
    PRAGMA Advanced Document Engineering\crlf
    Mr. Hans Hagen (the wizard who wrote it all, \CONTEXT\ and everything else, 
                    with the help of his little elves)\crlf
    Ridderstraat 27\crlf
    8061 GH Hasselt\crlf
    THE NETHERLANDS
    }
\stopsetups

\definelayer[AddressBg][
    x=0mm, y=0mm,
    width=65mm, height=30mm,
    state=start] % size is ignored!
\setupbackgrounds[paper][
    setups=ALayer,
    background=AddressBg,
    state=start]

\startsetups ALayer
    \setlayerframed[AddressBg]     % setlayerframed does not work here
       [width=65mm, height=30mm,
        frame=off,
        hoffset=0mm, voffset=0mm,
        align=right]%
    {
    PRAGMA Advanced Document Engineering\crlf
    Mr. Hans Hagen (the wizard who wrote it all, \CONTEXT\ and everything else, 
                    with the help of his little elves)\crlf
    Ridderstraat 27\crlf
    8061 GH Hasselt\crlf
    THE NETHERLANDS
    }
\stopsetups

\stoptext


Place labels on pictures

Layers can be used to put labels on pictures, alternative ways can be found in the Details manual (see also Collectors).

\setupexternalfigures[location={local,default}]
% (necessary for the Wiki's ConTeXt live processor)

\definelayer  [figure][width=\overlaywidth,height=\overlayheight]
\defineoverlay[figure][{\directsetup{figure}\tightlayer[figure]}]

\setupcolors[state=start]

\starttext

\startsetups figure

\setlayerframed
  [figure]
  [preset=rightbottom,
   x=.25\layerwidth,
   y=.25\layerheight]
  {HERE}

\setlayerframed
  [figure]
  [preset=leftbottom,
   x=.15\layerwidth,
   y=.35\layerheight]
  {THERE}

\stopsetups

\externalfigure[cow][background={foreground,figure},width=4cm,height=8cm]

\startsetups figure

\setlayerframed
  [figure]
  [preset=righttop,
   x=.25\layerwidth,
   y=.25\layerheight]
  {MORE}

\setlayerframed
  [figure]
  [preset=middle,
   foregroundcolor=green]
  {EVEN MORE}

\stopsetups

\externalfigure[cow][background={foreground,figure},width=14cm,height=2cm]

\defineexternalfigure[whatever][background={foreground,figure}]

\startsetups figure

\setlayerframed
  [figure]
  [preset=righttop,
   x=.25\layerwidth,
   y=.25\layerheight]
  {\red MORE}

\setlayerframed
  [figure]
  [preset=middle,
   foregroundcolor=green]
  {EVEN MORE}

\stopsetups

\externalfigure[cow][whatever][width=14cm,height=4cm]

\stoptext

Use dynamic content

The content of layer is fixed after your set it and will appear in the same way on each page.

\definelayer
  [pagenumber]
  [width=\paperwidth,
   height=\paperheight,
   preset=middle,
   state=repeat]

\setlayer[pagenumber]{Page \pagenumber}

\setupbackgrounds[page][background=pagenumber]

\starttext
\dorecurse{4}{\page[empty]} % Every page will say "Page 1"
\stoptext

This is not always what you desire like in the example above, where on each page the current page number should be shown and not the number from the first page.

To recalculate the content on each new page you have to enclose the \setlayer settings within \startsetups ... \stopsetups and write \setupbackgrounds[page][setups=setupname,background=layername] in your document.

\definelayer
  [pagenumber]
  [width=\paperwidth,
   height=\paperheight,
   preset=middle]

\startsetups layer

\setlayer[pagenumber]{Page \pagenumber}

\stopsetups

\setupbackgrounds[page][setups=layer,background=pagenumber]

\starttext
\dorecurse{4}{\page[empty]} % You get "Page 1, Page 2" etc.
\stoptext


Another solution was given by Wolfgang on the mailing list on February 21, 2012. He writes: Here is a slightly different version which is better when you need more dynamic layers.


\definelayer  [pagenumber][width=\overlaywidth,height=\overlayheight,preset=middle]
\defineoverlay[pagenumber][\texsetup{layer}]

\startsetups layer

\setlayer[pagenumber]{Page \pagenumber}

\tightlayer[pagenumber]

\stopsetups

\setupbackgrounds[page][setups=layer,background=pagenumber]

\starttext
\dorecurse{4}{\page[empty]}
\stoptext


Links


TODO: We need a lot of documentation and samples for this complicated subject. (See: To-Do List)