[Tex/LaTex] How to force a TikZ background pattern to be underneath nodes? (using layers)

backgroundslinguisticstikz-pgf

I am drawing Simon Dik's Functional Grammar Model Overview using TikZ, but I have run into a stumbling block. It is a design flaw on my part, and I would rather not redesign everything. The box called Lexicon was created using the fit library to make a box around a few nodes. It must be therefore drawn after those nodes are draw. Unfortunately, I need a pattern to fill the box (see original diagram).

UPDATE: I have solved the issue of the background covering my nodes using layers (see comments below). I am leaving this question open for anybody who could offer improvements to the code below and potentially solve my pattern issue and perhaps other alignment issues (like why the diagram is not centered when using the article class).

Problems:

  • I keep getting a black fill rather than a dotted background
  • The layer covers contained nodes

NOTE: The code I provide below does not include the pattern, because I could not get it to work.

Original Diagram:
enter image description here

My Code (updated according using layers see comments):

\documentclass[10pt]{article}
%\documentclass[tikz,border={5pt 5pt}]{standalone}
%SET MARGINS (AND PAGE DIMENSIONS)
%\usepackage[includeheadfoot,head=30pt,foot=30pt,top=1cm,bottom=1cm,left=1cm,right=1cm]{geometry}



\usepackage{fontspec}
\setmainfont{cmunrm.otf}
%\setmainfont{Calibri}

\usepackage{tikz}



\usetikzlibrary{fit,calc,trees,patterns,positioning,backgrounds,arrows,chains,shapes.geometric,%
    decorations.pathreplacing,decorations.pathmorphing,shapes,%
    matrix,shapes.symbols}

\tikzset{%
    join/.style={},
    myarrow/.style={->, thick,shorten >=1pt},
    mybox/.style={draw,rounded corners, minimum height=.7cm,align=center,node distance=1em},
    leftright/.style={outer sep=0,inner sep=0,text width=2cm,draw,align=center,node distance=0em,minimum height=3em},
    mainnode/.style={very thick,fill=red!10},
    spannodes/.style={text depth=-5pt,draw,inner sep=0em,outer sep=0,minimum height=1.5em,align=center,node distance=0em}
}%


\begin{document}
\thispagestyle{empty}

\begin{figure}
\centering
\vspace*{-45pt}
\hspace*{-3cm}\begin{tikzpicture}[remember picture]

% THE MAIN CHAIN THAT GOES STRAIGHT DOWN
\node(placeholder){};
\node[minimum height=1cm,minimum width=5cm,below=of placeholder.south,join] (lexicon) {};
\node[mainnode,mybox,below=2cm of lexicon.south,join] (nuclearpredication) {nuclear predication};
\node[mybox,below=of nuclearpredication,join] (lev1) {π\textsubscript{1} σ\textsubscript{1}};
\node[mainnode,mybox,below=of lev1,join] (corepredication) {core predication};
\node[mybox,below=of corepredication] (lev2) {π\textsubscript{2} σ\textsubscript{2}};
\node[mybox,below=of lev2] (syntacticfunctions) {syntactic functions};
\node[mainnode,mybox,below=of syntacticfunctions] (extendedpredication) {extended predication};
\node[mybox,below=of extendedpredication] (lev3) {π\textsubscript{3} σ\textsubscript{3}};
\node[mainnode,mybox,below=of lev3] (proposition) {proposition};
\node[mybox,below=of proposition] (lev4) {π\textsubscript{4} σ\textsubscript{4}};
\node[mybox,below=of lev4] (pragmaticfunctions) {pragmatic functions};
\node[mainnode,mybox,below=of pragmaticfunctions] (clausestructure) {clause structure};
\node[mybox,below=of clausestructure] (expressionrules) {\textsc{Expression Rules}\\\\form\\order\\prosody};
\node[mainnode,fill=yellow!20,mybox,below=of expressionrules,] (linguisticexpression) {\textsc{Linguistic Expression}};


% THE SECONDARY CHAIN LEFT AND RIGHT
% LEFT OF
\node[xshift=-1.5cm,leftright,left=of lexicon.south] (basicpredicates) {basic predicates};
\node[leftright,left=of basicpredicates] (derivedpredicates) {derived predicates};
\node[xshift=-2em,leftright,left=of derivedpredicates] (predicateformation) {predicate\\formation};
\node[fit={(derivedpredicates.west) (basicpredicates.east)},spannodes,below=of basicpredicates.south] at ($(derivedpredicates.south)!0.5!(basicpredicates.south)$) (predicateframes) {\textsc{Predicate Frames}};

% RIGHT OF
\node[xshift=1.5cm,leftright,right=of lexicon.south] (basicterms) {basic\\terms};
\node[leftright,right=of basicterms] (derivedterms) {derived\\terms};
\node[xshift=2em,leftright,right=of derivedterms] (termformation) {term\\formation};
\node[fit={(basicterms.west) (derivedterms.east)},spannodes,below=of basicterms] at ($(basicterms.south)!0.5!(derivedterms.south)$) (terms) {\textsc{Terms}};


\node [draw, very thick,inner sep=0,fit={%
        (lexicon) (derivedpredicates) (predicateframes) (derivedterms) (terms) (placeholder)
    },label=\textsc{Fund}] (fund){};
\begin{pgfonlayer}{background}
\node [draw=blue!100,anchor=south,dashed,fill=green!20,inner sep=0pt,fit={
        (basicpredicates) (basicterms) (lexicon)
    },label=\textsc{Lexicon}] {};
\end{pgfonlayer}{background}
%\node[above=2cm of basicterms.east] (rightcorner)  {};
%\draw[label=Lexicon] (basicpredicates.south west) rectangle (rightcorner);

% Connect the Lines
\draw[red] (derivedpredicates.north) -| ++(0,1.2) -| (termformation.north);
\draw[blue] (predicateformation.north) -| ++(0,1.4) -| (derivedterms.north);
\draw[red] (predicateformation.east) to (derivedpredicates);
\draw[blue] (predicateformation.south) -| ++(0,-.3) -| (predicateframes.west);
\draw[blue] (termformation.west) to (derivedterms.east);


% Draw Arrows
\draw[myarrow] (fund.south) to (nuclearpredication.north);
\draw[myarrow] (nuclearpredication.south) to (lev1.north);
\draw[myarrow] (lev1.south) to (corepredication.north);
\draw[myarrow] (corepredication.south) to (lev2.north);
\draw[myarrow] (lev2.south) to (syntacticfunctions.north);
\draw[myarrow] (syntacticfunctions.south) to (extendedpredication.north);
\draw[myarrow] (extendedpredication.south) to (lev3.north);
\draw[myarrow] (lev3.south) to (proposition.north);
\draw[myarrow] (proposition.south) to (lev4.north);
\draw[myarrow] (lev4.south) to (pragmaticfunctions.north);
\draw[myarrow] (pragmaticfunctions.south) to (clausestructure.north);
\draw[myarrow] (clausestructure.south) to (expressionrules.north);
\draw[myarrow] (expressionrules.south) to (linguisticexpression.north);




\end{tikzpicture}\hspace*{-3cm}
\caption{Functional Grammar Model Overview}
\end{figure}


\end{document}

My New Output using article class (diagram inside a figure environment)
New Article Class FG Diagram Output

My Old Output:
FG Diagram Output

Best Answer

To answer the subsidiary question:

The picture doesn't center as it is too wide:

Overfull \hbox (122.19116pt too wide) in paragraph at lines 93--94

in such cases TeX always puts the additional material into the right margin.

It is also too long:

LaTeX Warning: Float too large for page by 42.29073pt on input line 95.

Assuming that you don't mind it overlapping the nominal top and side margins, you can raise it by 45pt and insert (essentially arbitrary) negative space either side so that its effective width is less than text width. (I also switched your font choice as I don't have Calibri)

enter image description here

\documentclass[10pt]{article}
\usepackage{fontspec}
\setmainfont{cmunrm.otf}
%\setmainfont{Calibri}
\usepackage{tikz}
\usetikzlibrary{fit,calc,trees,patterns,positioning,backgrounds,arrows,chains,shapes.geometric,%
    decorations.pathreplacing,decorations.pathmorphing,shapes,%
    matrix,shapes.symbols}

\tikzset{
    join/.style={},
    myarrow/.style={->, thick,shorten >=1pt},
    mybox/.style={draw,rounded corners, minimum height=.7cm,align=center,node distance=1em},
    leftright/.style={outer sep=0,inner sep=0,text width=2cm,draw,align=center,node distance=0em,minimum height=3em},
    mainnode/.style={very thick,fill=red!10},
    spannodes/.style={text depth=-5pt,draw,inner sep=0em,outer sep=0,minimum height=1.5em,align=center,node distance=0em}
}


\begin{document}
\thispagestyle{empty}

\begin{figure}
\centering
\vspace*{-45pt}
\hspace*{-3cm}\begin{tikzpicture}[remember picture]

% THE MAIN CHAIN THAT GOES STRAIGHT DOWN
\node(placeholder){};
\node[minimum height=1cm,minimum width=5cm,below=of placeholder.south,join] (lexicon) {};
\node[mainnode,mybox,below=2cm of lexicon.south,join] (nuclearpredication) {nuclear predication};
\node[mybox,below=of nuclearpredication,join] (lev1) {π\textsubscript{1} σ\textsubscript{1}};
\node[mainnode,mybox,below=of lev1,join] (corepredication) {core predication};
\node[mybox,below=of corepredication] (lev2) {π\textsubscript{2} σ\textsubscript{2}};
\node[mybox,below=of lev2] (syntacticfunctions) {syntactic functions};
\node[mainnode,mybox,below=of syntacticfunctions] (extendedpredication) {extended predication};
\node[mybox,below=of extendedpredication] (lev3) {π\textsubscript{3} σ\textsubscript{3}};
\node[mainnode,mybox,below=of lev3] (proposition) {proposition};
\node[mybox,below=of proposition] (lev4) {π\textsubscript{4} σ\textsubscript{4}};
\node[mybox,below=of lev4] (pragmaticfunctions) {pragmatic functions};
\node[mainnode,mybox,below=of pragmaticfunctions] (clausestructure) {clause structure};
\node[mybox,below=of clausestructure] (expressionrules) {\textsc{Expression Rules}\\\\form\\order\\prosody};
\node[mainnode,mybox,below=of expressionrules] (linguisticexpression) {\textsc{Linguistic Expression}};


% THE SECONDARY CHAIN LEFT AND RIGHT
% LEFT OF
\node[xshift=-1.5cm,leftright,left=of lexicon.south] (basicpredicates) {basic predicates};
\node[leftright,left=of basicpredicates] (derivedpredicates) {derived predicates};
\node[xshift=-2em,leftright,left=of derivedpredicates] (predicateformation) {predicate\\formation};
\node[fit={(derivedpredicates.west) (basicpredicates.east)},spannodes,below=of basicpredicates.south] at ($(derivedpredicates.south)!0.5!(basicpredicates.south)$) (predicateframes) {\textsc{Predicate Frames}};

% RIGHT OF
\node[xshift=1.5cm,leftright,right=of lexicon.south] (basicterms) {basic\\terms};
\node[leftright,right=of basicterms] (derivedterms) {derived\\terms};
\node[xshift=2em,leftright,right=of derivedterms] (termformation) {term\\formation};
\node[fit={(basicterms.west) (derivedterms.east)},spannodes,below=of basicterms] at ($(basicterms.south)!0.5!(derivedterms.south)$) (terms) {\textsc{Terms}};


\node [draw, very thick,inner sep=0,fit={
        (lexicon) (derivedpredicates) (predicateframes) (derivedterms) (terms) (placeholder)
    },label=\textsc{Fund}] (fund){};
\begin{pgfonlayer}{background}
\node [draw=blue!100,anchor=south,dashed,fill=green!10,inner sep=0pt,fit={
        (basicpredicates) (basicterms) (lexicon)
    },label=\textsc{Lexicon}] {};
\end{pgfonlayer}{background}
%\node[above=2cm of basicterms.east] (rightcorner)  {};
%\draw[label=Lexicon] (basicpredicates.south west) rectangle (rightcorner);

% Connect the Lines
\draw[red] (derivedpredicates.north) -| ++(0,1.2) -| (derivedterms.north);
\draw[blue] (predicateformation.north) -| ++(0,1.4) -| (termformation.north);
\draw[blue] (predicateformation.east) to (derivedpredicates);
\draw[blue] (predicateformation.south) -| ++(0,-.3) -| (predicateframes.west);
\draw[blue] (termformation.west) to (derivedterms.east);


% Draw Arrows
\draw[myarrow] (fund.south) to (nuclearpredication.north);
\draw[myarrow] (nuclearpredication.south) to (lev1.north);
\draw[myarrow] (lev1.south) to (corepredication.north);
\draw[myarrow] (corepredication.south) to (lev2.north);
\draw[myarrow] (lev2.south) to (syntacticfunctions.north);
\draw[myarrow] (syntacticfunctions.south) to (extendedpredication.north);
\draw[myarrow] (extendedpredication.south) to (lev3.north);
\draw[myarrow] (lev3.south) to (proposition.north);
\draw[myarrow] (proposition.south) to (lev4.north);
\draw[myarrow] (lev4.south) to (pragmaticfunctions.north);
\draw[myarrow] (pragmaticfunctions.south) to (clausestructure.north);
\draw[myarrow] (clausestructure.south) to (expressionrules.north);
\draw[myarrow] (expressionrules.south) to (linguisticexpression.north);

\end{tikzpicture}\hspace*{-3cm}
\caption{Functional Grammar Model Overview}
\end{figure}


\end{document}
Related Question