# tikzlibrary chains misbehaving

The example below is straight out of the TikZ manual (section 46.3 page 537) and produces 'Unknown chain "chain" error'. I have noticed in a previous example that my PGF\TikZ does not like

{[start chain]
...
}


clauses.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[node distance=1mm,
every node/.style=draw,every join/.style=->]
\draw [help lines] (0,0) grid (3,2);
\node[red] (existing) at (0,2) {existing};
{[start chain]
\node [draw,on chain,join] {Hello};
\node [draw,on chain,join] {World};
\chainin (existing) [join];
\node [draw,on chain,join] {this};
\node [draw,on chain,join] {is};
}
\end{tikzpicture}
\end{document}


A single brace is not a replacement for a proper TikZ scope which does quite a lot of nontrivial things.

Hence you need to spell out the \begin{scope} then everything works properly. Thus, I wouldn't call this a bug but a typo in the documentation.

\documentclass[tikz]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[node distance=1mm,
every node/.style=draw,every join/.style=->]
\draw [help lines] (0,0) grid (3,2);
\node[red] (existing) at (0,2) {existing};
\begin{scope}[start chain,]
\node [draw,on chain,join] {Hello};
\node [draw,on chain,join] {World};
\chainin (existing) [join];
\node [draw,on chain,join] {this};
\node [draw,on chain,join] {is};
\end{scope}
\end{tikzpicture}
\end{document}

• while both cfr 's and @percusse 's input are fixing the problem, and I have consequently marked both as answers, I accept that of the two the latter is the more logically consistent one. It works for the more complex scenario posed in the last example in section 46.2, page 542 of the PGF Manual and is the one I adopt. – Reinhard Neuwirth May 30 '16 at 9:24
• @ReinhardNeuwirth This answer was first and is by an expert and it is entirely appropriate to choose it. However, mine is no less logically consistent. Moreover, it works fine for the last example in 46.2 on 542 of the manual. (See edited answer below.) Just so that other users who might prefer to use the examples as-is from the manual don't think there's any issue with using the scopes library: as far as I can tell, there's no problem either way because either way you get a scope which is what's needed here. – cfr May 30 '16 at 12:48
• @cfr Actually yours is more verbose if Reinhard agrees we should combine and I can delete this. – percusse May 30 '16 at 13:00
• @ReinhardNeuwirth Would you agree? – percusse May 30 '16 at 13:01
• More verbose is not good ;). I hadn't seen your answer when I wrote mine, but you definitely answered first and the point is the same. Let's see what @ReinhardNeuwirth wants to do. – cfr May 30 '16 at 13:04

It is not a bug. To treat { ... } as a TikZ scope - as opposed to just an ordinary TeX group - you need to load the scopes library.

\documentclass[tikz,multi,border=10pt]{standalone}
\usetikzlibrary{chains,scopes}
\begin{document}
\begin{tikzpicture}[node distance=1mm,
every node/.style=draw,every join/.style=->]
\draw [help lines] (0,0) grid (3,2);
\node[red] (existing) at (0,2) {existing};
{[start chain]
\node [draw,on chain,join] {Hello};
\node [draw,on chain,join] {World};
\chainin (existing) [join];
\node [draw,on chain,join] {this};
\node [draw,on chain,join] {is};
}
\end{tikzpicture}
% last example from section on chains, page 542
\begin{tikzpicture}[node distance=5mm,
every node/.style=draw]
{ [start chain=1]
\node [on chain] {A};
\node [on chain] {B};
\node [on chain] {C};
}
{ [start chain=2 going below]
\node [on chain=2] at (0.5,-.5) {0};
\node [on chain=2] {1};
\node [on chain=2] {2};
}
{ [continue chain=1]
\node [on chain] {D};
}
\end{tikzpicture}
\end{document}


One persistent issue with the PGF/TikZ manual is its tendency to assume that the correct libraries are loaded, even when example code uses libraries which were mentioned several pages earlier or which were mentioned only briefly, in passing or implicitly. This can make it difficult to use from a user's perspective. (And presumably the manual is supposed to serve us as users, so this is something of a short-coming.)

In this case, many, many of the examples for the chains library assume that at least scopes is loaded and it would be very helpful if this could be mentioned right at the beginning of the section, along with the syntax for loading the chains library itself.

• I stand corrected. This answer should be marked correct, in my opinion – Runar May 29 '16 at 18:52
• This is because scopes provides just a shortcut not a functionality. The problem is missing a scope not missing a library. – percusse May 29 '16 at 19:18
• ... ahhh! As much as I value the PGF/TikZ manual the fact that examples are frequently only documented as tikzpicture environments has often bothered me. – Reinhard Neuwirth May 29 '16 at 21:41
• @percusse It is is missing a scope because it is missing the library. The explanations are not mutually exclusive. They are simply specified at different levels or for different purposes. The problem in the documentation is that the examples in the section on chains assume that the scopes library is loaded. At least, this seems to me the most charitable reading. As a result, attempting to use the code without the library results in errors because, by default, curly brackets do not a scope make. [But I do realise than if I said that 1+1=2 or that a square had 4 sides, I would be incorrect.] – cfr May 29 '16 at 23:31
• Look at the comment in the top example on the manual page 542. You can't start from scratch in each example for a 1000+ page manual. There is a context. – percusse May 30 '16 at 4:29

CFR and percusse both posted a much better solution to the code and provided a much better explanation for as to why the typo arose in the manual. One of them should be marked correct.

Original post:

You are correct in that it seems to be an error in the code. Simply moving the start chain into the optional part of \begin{tikzpicture} seems to correct this. This can be seen in the previous examples.

Also, in my copy of the manual, which I believe is the most recent one, the mentioned example is on page 545, not that it matters too much as you included the section number, which is a lot more useful.

A bug report has been filed just now, #392 Documentation, section 46.3, example produces error

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[start chain,node distance=1mm,
every node/.style=draw,every join/.style=->]
\draw [help lines] (0,0) grid (3,2);
\node[red] (existing) at (0,2) {existing};
{
\node [draw,on chain,join] {Hello};
\node [draw,on chain,join] {World};
\chainin (existing) [join];
\node [draw,on chain,join] {this};
\node [draw,on chain,join] {is};
}
\end{tikzpicture}
\end{document}

• I confirm your fix works and am happy to report your fix also works for the last example in section 46.2 in the manual. – Reinhard Neuwirth May 29 '16 at 12:27
• ... my manual was Version 3.0.1. The latest is V.3.0.1a dated August 29, 2015. Vast improvement as it shows the pictures for example codes. – Reinhard Neuwirth May 29 '16 at 13:18