14

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}
2
  • I'd be surprised if that isn't explained in the user guide right where you found the example. Commented Feb 7, 2014 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
    Commented Feb 7, 2014 at 15:32

2 Answers 2

17

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.

4
  • The manual I downloaded from CTAN goes only to p.726. Where can I obtain the manual you referred to?
    – ajeh
    Commented Feb 7, 2014 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
    – user11232
    Commented Feb 7, 2014 at 15:56
  • 1
    @ajeh OK I have one for v2.1. See page 662 Section 82.
    – user11232
    Commented Feb 7, 2014 at 15:59
  • Link to manual v.2.1 is in the 1st line of the question, if anyone needs it.
    – ajeh
    Commented Feb 7, 2014 at 16:08
19

You can also use backgrounds library (section 43, pages 509-512 in pgfmanual v 3.0.0). It declares main and background layers and you don't need to use \pgfdeclarelayer commands. Instead of pgfonlayer environment, just add option [on background layer] to an scope environment.

\begin{scope}[on background layer]
...
\end{scope}

This syntax can be shortened with scopes library (section 12.3.2 Shorthand fo Scope Environments) which allows to write

{[on background layer]
...
}

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,scopes}   %<------- Load libraries
\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}};

    {[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 background scope
\end{tikzpicture}
\end{figure}

\end{document}

As you can see you get same results:

enter image description here

2
  • I am sorry for asking this without MWE since it will be quite difficult at the moment. If I have a minimal document with only a TikZ pic, and it works fine. However, after trying to include all the tikz code inside my main document I get an error Package pgf Error: Sorry, the requested layer 'background' is not part of the' is part of this list. \scoped[on background layer]. What might be wrong despite loading all the tikz libraries in my main document?
    – Diaa
    Commented Aug 8, 2018 at 11:15
  • 2
    @Diaa You need to load tikz but also used tikzlibraries. In this case you have to include \usetikzlibrary{scopes, backgrounds} in your preamble.
    – Ignasi
    Commented Aug 9, 2018 at 6:30

You must log in to answer this question.

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