Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The example in PGF/Tikz manual on the pp. 77-82 throws an error when incorporated into my simple org chart: ! Package pgf Error: Sorry, the requested layer 'background' could not be found. Maybe you misspelled it?.See the pgf package documentation for explanation.Type H <return> for immediate help.... \begin{pgfonlayer}{background}

Realizing that example was not self-contained I did search for background but could not see it being explicitly declared, maybe I am missing something.

\documentclass[10pt]{article}
\usepackage[landscape,paper=letterpaper]{geometry}
\usepackage{tikz}
\usetikzlibrary{trees}
\usetikzlibrary{shadings}
\tikzstyle{every node}=[draw=black,thin]

\begin{document}

\begin{figure}[!htb]
\begin{tikzpicture}[]
    \node[anchor=south](Supervisor){Supervisor}[]

    % Teammate and Subordinates
    child{node {Teammate1}}
    child{node {Teammate2}}
    child{node {Teammate3}}
    child{node {Teammate4}}
    child{node {Teammate5}};

    \begin{pgfonlayer}{background}
        \clip[xshift=-1cm] (-.5\textwidth,-.5\textheight) rectangle ++(\textwidth,\textheight);
        \colorlet{upperleft}{green!50!black!25}
        \colorlet{upperright}{orange!25}
        \colorlet{lowerleft}{red!25}
        \colorlet{lowerright}{blue!25}
        % The large rectangles:
        \fill [upperleft] (Supervisor) rectangle ++(-20,20);
        \fill [upperright] (Supervisor) rectangle ++(20,20);
        \fill [lowerleft] (Supervisor) rectangle ++(-20,-20);
        \fill [lowerright] (Supervisor) rectangle ++(20,-20);
        % The shadings:
        \shade [left color=upperleft,right color=upperright]([xshift=-1cm]Supervisor) rectangle ++(2,20);
        \shade [left color=lowerleft,right color=lowerright]([xshift=-1cm]Supervisor) rectangle ++(2,-20);
        \shade [top color=upperleft,bottom color=lowerleft]([yshift=-1cm]Supervisor) rectangle ++(-20,2);
        \shade [top color=upperright,bottom color=lowerright]([yshift=-1cm]Supervisor) rectangle ++(20,2);
    \end{pgfonlayer}         
\end{tikzpicture}
\end{figure}

\end{document}
share|improve this question
    
I'd be surprised if that isn't explained in the user guide right where you found the example. –  Thorsten Donig Feb 7 at 15:30
    
Same thought. Could you point me at the explanation? I thought it should have been in 6.5 Adding the Background on p. 77 but cannot see it explained. –  ajeh Feb 7 at 15:32

2 Answers 2

up vote 5 down vote accepted

You have to first declare layers:

\pgfdeclarelayer{background}
\pgfsetlayers{background}

The code:

\documentclass[10pt]{article}
\usepackage[landscape,paper=letterpaper]{geometry}
\usepackage{tikz}
\usetikzlibrary{trees}
\usetikzlibrary{shadings}
\tikzset{every node/.append style = {draw=black,thin}}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}   %% some additional layers for demo

\begin{document}

\begin{figure}[!htb]
\begin{tikzpicture}[ level 1/.style={sibling distance=6em}]
    \node[anchor=south](Supervisor){Supervisor}[]

    % Teammate and Subordinates
    child{node {Teammate1}}
    child{node {Teammate2}}
    child{node {Teammate3}}
    child{node {Teammate4}}
    child{node {Teammate5}};

    \begin{pgfonlayer}{background}
        \clip[xshift=-1cm] (-.5\textwidth,-.5\textheight) rectangle ++(\textwidth,\textheight);
        \colorlet{upperleft}{green!50!black!25}
        \colorlet{upperright}{orange!25}
        \colorlet{lowerleft}{red!25}
        \colorlet{lowerright}{blue!25}
        % The large rectangles:
        \fill [upperleft] (Supervisor) rectangle ++(-20,20);
        \fill [upperright] (Supervisor) rectangle ++(20,20);
        \fill [lowerleft] (Supervisor) rectangle ++(-20,-20);
        \fill [lowerright] (Supervisor) rectangle ++(20,-20);
        % The shadings:
        \shade [left color=upperleft,right color=upperright]([xshift=-1cm]Supervisor) rectangle ++(2,20);
        \shade [left color=lowerleft,right color=lowerright]([xshift=-1cm]Supervisor) rectangle ++(2,-20);
        \shade [top color=upperleft,bottom color=lowerleft]([yshift=-1cm]Supervisor) rectangle ++(-20,2);
        \shade [top color=upperright,bottom color=lowerright]([yshift=-1cm]Supervisor) rectangle ++(20,2);
    \end{pgfonlayer}
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

In version 3 of pgfmanual, this is explained in section 109.1,2 and 3 (109 Layered Graphics), on page 1084. For version 2.1, see page 662, section 82.

share|improve this answer
    
The manual I downloaded from CTAN goes only to p.726. Where can I obtain the manual you referred to? –  ajeh Feb 7 at 15:54
    
@ajeh At present, I don't have version 2 manual. Version 3 isn't officially released to tex distributions. You have to install it yourself: sourceforge.net/projects/pgf/files/pgf/version%203.0.0 –  Harish Kumar Feb 7 at 15:56
1  
@ajeh OK I have one for v2.1. See page 662 Section 82. –  Harish Kumar Feb 7 at 15:59
    
Link to manual v.2.1 is in the 1st line of the question, if anyone needs it. –  ajeh Feb 7 at 16:08

You can also use backgrounds library. It declares main and background layers and you don't need to use \pgfdeclarelayer commands. And instead of pgfonlayer environment, just add option [on background layer] to an scope environment.

Backgrounds tikzlibrary is explained in section 25, pages 263-265 in pgfmanual v 2.10.

Harish's code adapted to these commands looks like

\documentclass[10pt]{article}
\usepackage[landscape,paper=letterpaper]{geometry}
\usepackage{tikz}
\usetikzlibrary{trees}
\usetikzlibrary{shadings}
\usetikzlibrary{backgrounds}   %<------- Load backgrounds library
\tikzset{every node/.append style = {draw=black,thin}}

\begin{document}

\begin{figure}[!htb]
\begin{tikzpicture}[ level 1/.style={sibling distance=6em}]
    \node[anchor=south](Supervisor){Supervisor}[]

    % Teammate and Subordinates
    child{node {Teammate1}}
    child{node {Teammate2}}
    child{node {Teammate3}}
    child{node {Teammate4}}
    child{node {Teammate5}};

    \begin{scope}[on background layer] %<- Everything is drawn on background layer
        \clip[xshift=-1cm] (-.5\textwidth,-.5\textheight) rectangle ++(\textwidth,\textheight);
        \colorlet{upperleft}{green!50!black!25}
        \colorlet{upperright}{orange!25}
        \colorlet{lowerleft}{red!25}
        \colorlet{lowerright}{blue!25}
        % The large rectangles:
        \fill [upperleft] (Supervisor) rectangle ++(-20,20);
        \fill [upperright] (Supervisor) rectangle ++(20,20);
        \fill [lowerleft] (Supervisor) rectangle ++(-20,-20);
        \fill [lowerright] (Supervisor) rectangle ++(20,-20);
        % The shadings:
        \shade [left color=upperleft,right color=upperright]([xshift=-1cm]Supervisor) rectangle ++(2,20);
        \shade [left color=lowerleft,right color=lowerright]([xshift=-1cm]Supervisor) rectangle ++(2,-20);
        \shade [top color=upperleft,bottom color=lowerleft]([yshift=-1cm]Supervisor) rectangle ++(-20,2);
        \shade [top color=upperright,bottom color=lowerright]([yshift=-1cm]Supervisor) rectangle ++(20,2);
    \end{scope}
\end{tikzpicture}
\end{figure}

\end{document}

As you can see you get same results:

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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