# chain conflict with pgfdeclarelayer

See this example:

\documentclass[tikz]{standalone}
\usetikzlibrary{chains,scopes}
\begin{document}
\begin{tikzpicture}[node distance=0 and 0]
\pgfdeclarelayer{background} % comment this line works
{[start chain=C1]
\foreach \i in {a,b,c,d,e,f}
\node[draw,minimum size=1.5em,outer sep=0,on chain=C1] (N\i) {\i};
}
\end{tikzpicture}
\end{document}

Error message:

! Package tikz Error: Unknown chain C1''.

The code can not pass compile but if I comment out the pgfdeclarelayer line, it works fine.

Does that means pgfdeclarelayer conflict with chain package? How to fix it?

It doesn't conflict with \pgfdeclarelayer - you would get a similar error with lots of other commands in this place as your syntax is wrong.

The problem is that the scopes library allows you to use braces as a shorthand for a scope only in quite specific places -- and after a command is not such a place. (It would work if you add a \path command).

You should use [start chain C1] as option to a scope:

\documentclass[tikz]{standalone}
\usetikzlibrary{chains,scopes}
\begin{document}
\begin{tikzpicture}[node distance=0 and 0]
\pgfdeclarelayer{background} % comment this line works
\begin{scope}[start chain=C1]
\foreach \i in {a,b,c,d,e,f}
\node[draw,minimum size=1.5em,outer sep=0,on chain=C1] (N\i) {\i};
\end{scope}
\end{tikzpicture}
\end{document}
• Good answer, but why it work if without the pgfdeclarelayer line? – lucky1928 Feb 6 at 17:28
• Because you are then using the scopes shorthand directly after tikzpicture and then it works. I added a short explanation. – Ulrike Fischer Feb 6 at 17:48
• In general, I think it is better to declare the layers before starting the tikzpicture, perhaps even in the preamble of the document. – user121799 Feb 6 at 21:52