Graphics, Figures & TablesUsing multiple layers in tikZ together with matrix library

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
drgz
Posts: 44
Joined: Sat Apr 18, 2009 8:40 pm

Using multiple layers in tikZ together with matrix library

Post by drgz »

Hello,

I'm trying to draw a block schematic for a LMS filter using TikZ. I'd like to use two layers, such that I can draw an arrow "beneath" one of the blocks for the adaptive part (on the background layer). However, I get an error saying

Code: Select all

! Missing number, treated as zero.
<to be read again> 
                   \pgf@layerbox@foreground layer 
l.35  \begin{pgfonlayer}{foreground layer}                                 
? 
when I compile the code given below.

If I compile the exact same code without the layers, everything works fine (although the figure is not how I want it to be). I've made plenty of figures like this before, the only difference now is that I use the matrix library to place the different blocks in the schematic, so there's obviously something with this that causes the error, but I can't figure out what, so hopefully someone can help me out. :)

The following code is what I try to compile (with error)

Code: Select all

\documentclass[12pt,a4paper]{article}

\usepackage[T1]{fontenc}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{float}
\usepackage[obeyall]{siunitx}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}
\usetikzlibrary{shapes}
\usetikzlibrary{matrix}

\begin{document}
\thispagestyle{empty}

\tikzstyle{block} = [draw, minimum width = 3em, minimum height = 2em, semithick, rectangle, fill = white]
\tikzstyle{sum} = [draw, circle, inner sep = 0mm, minimum size = 1mm, semithick]
\tikzstyle{branch} = [circle, inner sep = 0pt, minimum size = 1mm, fill = black, draw = black]

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background, main, foreground}

\begin{figure}
\centering
\begin{tikzpicture}[semithick,>=latex']
	\begin{pgfonlayer}{foreground layer}
        \matrix[ampersand replacement = \&, row sep = 0.4cm, column sep = 1.0cm]{
            \node[] (i0) {}; \& \\
            \& \& \node[block] (f0) {$\hat{\mathbf{h}}[n]$}; \& \& \node[block] (f1) {$\mathbf{h}[n]$}; \\
            \& \\
            \node[] (i1) {}; \& \node[branch] (b0) {}; \& \node[sum] (s0) {$+$}; \& \& \node[sum] (s1) {$+$}; \& \node[] (i2) {}; \\
        };

        \node at ($(i0.west)+(-0.5,0)$) (input) {$x[n]$};
        \node at ($(i1.west)+(-0.5,0)$) (error) {$e[n]$};
        \node at ($(i2.east)+(0.5,0)$) (noise) {$\eta[n]$};

        \draw[semithick,->] (input) -| (f0.north);
        \draw[semithick,->] (input) -| (f1.north);
        \draw[semithick,->] (noise) -- (s1.east);
        \draw[semithick,->] (s1.west) -- node[above] () {$\tilde{y}[n]$} (s0.east);
        \draw[semithick] (s0.west) -- (b0.east);
        \draw[semithick,->] (b0.west) -- (error);
        \draw[semithick,->] (f1.south) -- node[right] () {$y[n]$} (s1.north);
        \draw[semithick,->] (f0.south) -- node[right] () {$\hat{y}[n]$} (s0.north);
	\end{pgfonlayer}

	\begin{pgfonlayer}{background layer}
		\draw[semithick,->] (b0) -- ($(b0.north)+(0.8,0)$) -- ($(f0.north east)+(0.5,0)$);
	\end{pgfonlayer}

\end{tikzpicture}
\end{figure}

\end{document}
Last edited by drgz on Wed Jun 09, 2010 8:58 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Using multiple layers in tikZ together with matrix library

Post by localghost »

The code below works for me. The dropped packages are not related to the problem.

Code: Select all

\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,matrix,shapes}

\begin{document}
  \thispagestyle{empty}

  \tikzstyle{block}=[%
    draw,
    minimum width=3em,
    minimum height=2em,
    semithick,
    rectangle,
    fill=white
  ]
  \tikzstyle{sum}=[%
    draw,
    circle,
    inner sep=0mm,
    minimum size=1mm,
    semithick
  ]
  \tikzstyle{branch}=[%
    circle,
    inner sep=0pt,
    minimum size=1mm,
    fill=black,
    draw=black
  ]

  \pgfdeclarelayer{background}
  \pgfdeclarelayer{foreground}
  \pgfsetlayers{background,main,foreground}

  \begin{figure}
    \centering
    \begin{tikzpicture}[semithick,>=latex']
      \begin{pgfonlayer}{foreground}
        \matrix[%
          ampersand replacement=\&,
          row sep=0.4cm,
          column sep = 1.0cm
        ]{%
          \node[] (i0) {}; \& \\
          \& \& \node[block] (f0) {$\hat{\mathbf{h}}[n]$}; \& \& \node[block] (f1) {$\mathbf{h}[n]$}; \\
          \& \\
          \node[] (i1) {}; \& \node[branch] (b0) {}; \& \node[sum] (s0) {$+$}; \& \& \node[sum] (s1) {$+$}; \& \node[] (i2) {}; \\
        };

        \node at ($(i0.west)+(-0.5,0)$) (input) {$x[n]$};
        \node at ($(i1.west)+(-0.5,0)$) (error) {$e[n]$};
        \node at ($(i2.east)+(0.5,0)$) (noise) {$\eta[n]$};

        \draw[semithick,->] (input) -| (f0.north);
        \draw[semithick,->] (input) -| (f1.north);
        \draw[semithick,->] (noise) -- (s1.east);
        \draw[semithick,->] (s1.west) -- node[above] () {$\tilde{y}[n]$} (s0.east);
        \draw[semithick] (s0.west) -- (b0.east);
        \draw[semithick,->] (b0.west) -- (error);
        \draw[semithick,->] (f1.south) -- node[right] () {$y[n]$} (s1.north);
        \draw[semithick,->] (f0.south) -- node[right] () {$\hat{y}[n]$} (s0.north);
      \end{pgfonlayer}

      \begin{pgfonlayer}{background}
        \draw[semithick,->] (b0) -- ($(b0.north)+(0.8,0)$) -- ($(f0.north east)+(0.5,0)$);
      \end{pgfonlayer}
    \end{tikzpicture}
  \end{figure}
\end{document}
Note that you have to omit the word »layer« in the argument for the pgfonlayer environments.


Thorsten
drgz
Posts: 44
Joined: Sat Apr 18, 2009 8:40 pm

Re: Using multiple layers in tikZ together with matrix libra

Post by drgz »

I didn't know about omitting the "layer" part for that environment - will remember it for next time (although strange it has worked in other figures I've made earlier).

Anyhow, thanks for the help! Really appreciated!
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Using multiple layers in tikZ together with matrix library

Post by localghost »

drgz wrote:I didn't know about omitting the "layer" part for that environment - will remember it for next time (although strange it has worked in other figures I've made earlier).[...]
You are partly right. Probably in former documents you declared the layers with their "full" name.

Code: Select all

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}
But in your example above you declared something else.

Code: Select all

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
Hence you can only use the layers you declared with the names assigned.

That's at least what the pgf/tikZ manual tells me. See Section 65 (Layered Graphics, p. 509f) for the details.
drgz
Posts: 44
Joined: Sat Apr 18, 2009 8:40 pm

Re: Using multiple layers in tikZ together with matrix libra

Post by drgz »

You're completely right. Can't understand why I didn't see that this differed in the two files. Again thanks for the help and the explanation. ;)
wicko3
Posts: 4
Joined: Wed Jan 28, 2009 7:03 pm

Using multiple layers in tikZ together with matrix library

Post by wicko3 »

For the benefit of other people having similar problems with layering in PGF/TikZ, I'd like to point out that the reason for the original poster's error was the erroneous spaces in the \pgfsetlayers command.

Replace:

Code: Select all

\pgfsetlayers{background, main, foreground}
with:

Code: Select all

\pgfsetlayers{background,main,foreground}
Post Reply