[Tex/LaTex] TikZ background not working

backgroundstikz-pgf

I am drawing a block diagram with TikZ. While everything is working like a charm, I cannot draw in the background. My code is

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,snakes,automata,backgrounds,petri,shadows}
\pgfdeclarelayer{background}
\newcommand{\eng}[1]{\latintext#1\greektext}

\tikzstyle{blockOrange}=[top color=white, bottom color=orange!60,     draw=orange!90!black!100, minimum size=1cm, rounded corners]
\tikzstyle{blockBrown}=[top color=white, bottom color=brown!60,   draw=brown!90!black!100, minimum size=1cm, rounded corners]
\tikzstyle{blockGray}=[top color=white, bottom color=gray!60, draw=gray!90!black!100,  minimum size=1cm, rounded corners]
\tikzstyle{blockBlack}=[top color=white, bottom color=black!60, draw=black!90!black!100, minimum size=1cm, rounded corners]

\begin{figure}%[H]%
\centering%
\begin{tikzpicture}[node distance=0.1cm,auto,>=latex']


%read-out
\node [blockOrange] (MM) {\eng{Detector}};
\node [blockBrown, right of=MM, node distance=3cm] (PRE) {\eng{Pre-Amp}};
\node [blockBrown, right of=PRE, node distance=3cm] (AMP) {\eng{Amplifier}};
\node [below of=AMP, node distance=2cm] (dummy1) {};
\node [blockBlack, right of=AMP, node distance=2.7cm] (MCA) {\eng{\textcolor{red}{MCA}}};
\node [blockGray, below of=MCA, node distance=2cm] (SCOPE) {\eng{Scope}};
\node [blockGray, above of=MCA, node distance=2.5cm] (PC) {\eng{\textcolor{red}{PC}}};

\path[->] (MM) edge node {strips} (PRE);
\path[->] (PRE) edge node {energy} (AMP);
\path[->] (AMP) edge node {bipolar} (MCA);
\path[->] (MCA) edge node {} (PC);
\draw (7.3,0) -- (7.3,-2);
\draw[->] (7.3,-2) -- (8.1,-2);
\draw node at (7.9,-1) {\textcolor{red}{balun}};

\begin{pgfonlayer}{background}

    %Read Out   
    \path (PRE.east |- PRE.north)+(1,0.3) node (e) {};
    \path (SCOPE.south -| SCOPE.west)+(-0.8,-0.3) node (f) {};

    \path[top color=white, bottom color=red!60, draw=red!90!black!100, dashed, rounded corners, thick]
        (e) rectangle (f);

\end{pgfonlayer}

\end{tikzpicture}
\caption{Block Diagram}
\label{fig:blockGain}%
\end{figure}

\end{document}

Any ideas on that? My output is


This is the \listitem output straight from the .log file

 *File List*
subfiles.cls    2002/06/08 Federico Garcia
verbatim.sty    2003/08/22 v1.5q LaTeX2e package for verbatim enhancements
    main.tex
filecontents.sty    2009/03/17 v1.2 Create an external file from within a LaTeX
 document
    book.cls    2007/10/19 v1.4h Standard LaTeX document class
    bk11.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
   babel.sty    2008/07/06 v3.8l The Babel package
 bblopts.cfg    2005/09/08 v0.1 add Arabic and Farsi to "declared" options of b
abel
 english.ldf    2005/03/30 v3.3o English support from the babel system
   greek.ldf    2005/03/30 v1.3l Greek support from the babel system
  lgrenc.def    2008/06/17 v2.3 Greek Encoding
inputenc.sty    2008/03/30 v1.1d Input encoding file
iso-8859-7.def    2000/05/28 v1.2 Greek input encoding file
kerkis.sty    2000/02/25 v1.5 Package `kerkis.sty'
 amsmath.sty    2000/07/18 v2.13 AMS math features
 amstext.sty    2000/06/29 v2.01
  amsgen.sty    1999/11/30 v2.0
  amsbsy.sty    1999/11/29 v1.2d
  amsopn.sty    1999/12/14 v2.01 operator names
 amssymb.sty    2009/06/22 v3.00
 amsfonts.sty    2009/06/22 v3.00 Basic AMSFonts support
 amsthm.sty    2004/08/06 v2.20
resizegather.sty    2010/03/01 v1.2 Automatically resizing of too large equatio
ns (HO)
kvoptions.sty    2010/02/22 v3.7 Keyval support for LaTeX options (HO)
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
kvsetkeys.sty    2010/03/01 v1.9 Key value parser (HO)
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/message (HO)
etexcmds.sty    2010/01/28 v1.3 Prefix for e-TeX command names (HO)
graphics.sty    2009/02/05 v1.0o Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2007/01/18 v1.5 graphics configuration of teTeX/TeXLive
  pdftex.def    2010/02/14 v0.04n Graphics/color for pdfTeX
   units.sty    1998/08/04 v0.9b Typesetting units
  ifthen.sty    2001/05/26 v1.1c Standard LaTeX ifthen package (DPC)
nicefrac.sty    1998/08/04 v0.9b Nice fractions
   array.sty    2008/09/09 v2.4c Tabular extension package (FMi)
  framed.sty    2007/10/04 v 0.95: framed or shaded text with page breaks
extarrows.sty    2008/05/15 v1.0b Some extensible arrows
  cancel.sty    2000/03/12 v2.1 Cancel math terms
graphicx.sty    1999/02/16 v1.0f Enhanced LaTeX Graphics (DPC,SPQR)
geometry.sty    2010/03/13 v5.3 Page Geometry
   ifpdf.sty    2010/01/28 v2.1 Provides the ifpdf switch (HO)
  ifvtex.sty    2010/03/01 v1.5 Switches for detecting VTeX and its modes (HO)
geometry.cfg
fncychap.sty    2007/07/30 v1.34 LaTeX package (Revised chapters)
fancybox.sty    2010/05/15 1.4
fancyhdr.sty    
colortbl.sty    2001/02/13 v0.1j Color table columns (DPC)
   color.sty    1999/02/16
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
footmisc.sty    2011/06/06 v5.5b a miscellany of footnote facilities
subfiles.sty    2002/06/08 Federico Garcia
   float.sty    2001/11/08 v1.3d Float enhancements (AL)
slashbox.sty    
 tocloft.sty    2010/10/13 v2.3e parameterised ToC, etc., typesetting
 hyperref.sty    2010/03/30 v6.80u Hypertext links for LaTeX
  ltxcmds.sty    2010/04/08 v1.5 LaTeX kernel commands for general use (HO)
 pdfescape.sty    2010/03/01 v1.9 Provides hex, PDF name and string conversions 
(HO)
pdftexcmds.sty    2010/04/01 v0.9 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
 ifxetex.sty    2009/01/23 v0.5 Provides ifxetex conditional
 hycolor.sty    2009/12/12 v1.6 Color options of hyperref/bookmark (HO)
xcolor-patch.sty    2009/12/12 xcolor patch
letltxmacro.sty    2008/06/24 v1.3 Let assignment for LaTeX macros (HO)
  pd1enc.def    2010/03/30 v6.80u Hyperref: PDFDocEncoding definition (HO)
 intcalc.sty    2007/09/27 v1.1 Expandable integer calculations (HO)
hyperref.cfg    2002/06/06 v1.2 hyperref configuration of TeXLive
     url.sty    2006/04/12  ver 3.3  Verb mode for urls, etc.
  bitset.sty    2007/09/28 v1.0 Data type bit set (HO)
bigintcalc.sty    2007/11/11 v1.1 Expandable big integer calculations (HO)
atbegshi.sty    2010/03/25 v1.12 At begin shipout hook (HO)
 hpdftex.def    2010/03/30 v6.80u Hyperref driver for pdfTeX
atveryend.sty    2010/03/24 v1.5 Hooks at very end of document (HO)
rerunfilecheck.sty    2010/03/16 v1.6 Rerun checks for auxiliary files (HO)
uniquecounter.sty    2009/12/18 v1.1 Provides unlimited unique counter (HO)
subfigure.sty    2002/07/30 v2.1.4 subfigure package
subfigure.cfg
 wrapfig.sty    2003/01/31  v 3.6
rotating.sty    2009/03/28 v2.16a rotated objects in LaTeX
  lscape.sty    2000/10/22 v3.01 Landscape Pages (DPC)
fancyvrb.sty    2008/02/07
listings.sty    2007/02/22 1.4 (Carsten Heinz)
 lstmisc.sty    2007/02/22 1.4 (Carsten Heinz)
listings.cfg    2007/02/22 1.4 listings configuration
textcomp.sty    2005/09/27 v1.99g Standard LaTeX package
  ts1enc.def    2001/06/05 v3.0e (jk/car/fm) Standard LaTeX file
lstlang1.sty    2004/09/05 1.3 listings language file
pifont.sty    2005/04/12 PSNFSS-v9.2a Pi font support (SPQR) 
    upzd.fd    2001/06/04 font definitions for U/pzd.
    upsy.fd    2001/06/04 font definitions for U/psy.
  feynmp.sty    1996/12/02 v1.08 LaTeX/MetaPost Feynman Diagram Package (ohl)
    tikz.sty    2010/10/13 v2.10 (rcs-revision 1.76)
     pgf.sty    2008/01/15 v2.10 (rcs-revision 1.12)
  pgfrcs.sty    2010/10/25 v2.10 (rcs-revision 1.24)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v2.10 (rcs-revision 1.7)
  pgfsys.sty    2010/06/30 v2.10 (rcs-revision 1.37)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2008/07/18  (rcs-revision 1.7)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v2.10 (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v2.10 (rcs-revision 1.1)
  pgffor.sty    2010/03/23 v2.10 (rcs-revision 1.18)
 pgfkeys.sty    
 pgfkeys.code.tex
  pgffor.code.tex
    tikz.code.tex
circuitikz.sty    2009/11/18 The CircuiTikz circuit drawing package version 0.2.4    
multicol.sty    2008/12/05 v1.6h multicolumn formatting (FMi)
enumitem.sty    2011/07/14 v3.3 Customized lists
 makeidx.sty    2000/03/29 v1.0m Standard LaTeX package
booktabs.sty    2005/04/14 v1.61803 publication quality tables
  lgrcmr.fd    2008/06/17 v2.3 Greek Computer Modern
  ts1cmr.fd    1999/05/25 v2.5h Standard LaTeX font definitions
  ot1mak.fd
  lgrmak.fd
  supp-pdf.tex
 nameref.sty    2010/01/25 v2.36 Cross-referencing by name of section
refcount.sty    2008/08/11 v3.1 Data extraction from references (HO)
gettitlestring.sty    2009/12/18 v1.3 Cleanup title references (HO)
chapter4.out
chapter4.out
lgrmaksf.fd
    umsa.fd    2009/06/22 v3.00 AMS symbols A
    umsb.fd    2009/06/22 v3.00 AMS symbols B
 ***********

)

I have forgoten to add \usetikzlibrary{backgrounds} in main's preamble… When I did something really weird happened… Rectangles are not filled with color, background rextangles are not filled with color as well and you can see some shading float around page.

I don't have a clue on what's going on…

Best Answer

You have to tell TikZ in what order to stack the layers via \pgfsetlayers{<layers>}, where <layers> is a comma separated list of layers from back to front. All commands which are not inside a pgfonlayer environment will be assigned to the layer main which is always defined. To have such commands on the right layer, main should always appear in the <layers> specification. TikZ will not throw an error when not using \pgfsetlayers{<layers>}, however it will act rather strange and leave out commands for most layers. When leaving out the \pgfsetlayers{<layers>} it didn't matter in what order background or foreground were declared or used, the backround was always shown and the foreground never, which is really strange. So I guess the advice is always to remember writing \pgfsetlayers{<layers>} as otherwise strange results may occur.

Code

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{arrows,backgrounds,shadows}

% define layers
    \pgfdeclarelayer{foreground}
    \pgfdeclarelayer{background}
% tell TikZ how to stack them (back to front)
    \pgfsetlayers{background,main,foreground}

\begin{document}

\begin{tikzpicture}[rounded corners=3mm,mysquare/.style={left color=#1!10,right color=#1!90,draw=#1!50!black}]
    \begin{pgfonlayer}{foreground}
        \fill[mysquare=green] (-1,-3) node[above right] {\Huge F} rectangle (1,3);
    \end{pgfonlayer}

    \fill[mysquare=red] (-2,-2) node[above right] {\Huge M} rectangle (2,2);

    \begin{pgfonlayer}{background}
        \fill[mysquare=blue] (-3,-1) node[above right] {\Huge B} rectangle (3,1);
    \end{pgfonlayer}
\end{tikzpicture}

\end{document}

Output

enter image description here

Related Question