# Trying to get Tikz chains example working

I found a lovely example of chains diagramming here and have been trying to get it to work. I opened a fresh document on WriteLaTeX, here, and put in the minimal preamble for the chains drawing, namely

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows,chains}

\begin{document}

\begin{frame}{Chains}
% http://texblog.net/latex-archive/maths/pgf-tikz-commutative-diagram/
\tikzset{join/.code=\tikzset{after node path={%
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)%
edge[every join]#1(\tikzchaincurrent)\fi}}}

\tikzset{>=stealth',every on chain/.append style={join},every join/.style={->}}
\end{frame}
\end{document}


I get an error that there is an illegal parameter number in the definition of \test, and that I meant to type ## and not #, didn't I?

If I take out the tikzset lines, then the document compiles. I have the same problems in a non-beamer document.

I'm just using WriteLaTeX and don't have any tooling that will allow me to debug into the guts of tikz to search for an answer; I am only a noob user of tikz, taking working examples and tweaking them one thing at a time, backing off when they break; and lots of other tikz examples I have mined from the web work great. But this one I couldn't even get bootstrapped.

EDIT: Putting [fragile] after the \begin{frame} notation got me past the error. Now the following drawing doesn't look good (arrows don't draw; all the arrow labels pile up on one another at the beginning:

\begin{tikzpicture}[start chain] {
\node[on chain] {$0$};
\node[on chain] {$A$} ;
\node[on chain, join={node[above]
{$\scriptstyle\varphi$}}] {$B$};
\node[on chain, join={node[above]
{$\scriptstyle\psi$}}] {$C$};
\node[on chain] {$0$}; }
\end{tikzpicture}


Merging the two tikzset instructions and removing the stealth' sub-instruction had no effect on the appearance of the result.

• There was an extra \tikzset in the code ;) I recommend tikz-cd package if this is going to be a commutative diagram. – percusse Jul 27 '13 at 14:02
• Quite hard for me to mentally parse the code from the source web site: it does say \tikzset{join/.code=\tikzset{after node path={%, but, if I change it to \tikzset{join/.code={after node path={%, that doesn't work. I fully admit I do not understand the syntax. – Reb.Cabin Jul 27 '13 at 14:45

Here is a document that will compile:

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{matrix,arrows,chains}

\begin{document}

% http://texblog.net/latex-archive/maths/pgf-tikz-commutative-diagram/

\makeatletter
\tikzset{join/.code=\tikzset{after node path={%
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)%
edge[every join]#1(\tikzchaincurrent)\fi}}}
\makeatother

\begin{frame}{Chains}
\tikzset{>=stealth',every on chain/.append style={join},
every join/.style={->}}

\begin{tikzpicture}[start chain] {
\node[on chain] {$X$};
\node[on chain, join={node[above] {$\scriptstyle f$}}] {$Y$};
}
\end{tikzpicture}

\end{frame}

\end{document}


There were at least two problems with your code.

1. # in a beamer frame confuses the beamer parsing. Instead you can move this \tikzset command outside of the frame.

2. The same code snippet contains an @ sign and so needs to be enclosed in a \makeatletter / \makeatother pair.

The restrictions on frame contents are described in the beamer manual as follows

The ⟨environment contents⟩ can be normal LATEX text, but may not contain \verb commands or verbatim environments or any environment that changes the character codes, unless the fragile option is given.

As # is not a normal text character, it is thus excluded from standard frame contents. @Daniel points out that you can use the fragile option on the frame to work around this. @Daniel further points out that this usable despite the description in the manual:

If a frame contains fragile text, different internal mechanisms are used to typeset the frame to ensure that inside the frame the character codes can be reset. The price of switching to another internal mechanism is that either you cannot use overlays or an external file needs to be written and read back (which is not always desirable).

The code written to an external file, causing slightly slower compilation, but not loosing any overlay features. See @Daniel's answer to another question for more information.

• This one also renders correctly, so is a full answer! – Reb.Cabin Jul 27 '13 at 14:39
• If, however, you want to define the style locally (inside the frame), passing the fragile option to the frame environment should also help. – Daniel Jul 27 '13 at 14:42
• @Daniel I have added a little more to my answer discussing this. – Andrew Swann Jul 27 '13 at 15:00
• "This looses you some beamer functionality" is not true for any practical purpose; the cited paragraph from the manual is misleading: Historically, beamer provided the containsverbatim option that had the mentioned disadvantage of not supporting overlays. However, with the newer fragile, the only disadvantage is a slight compilation-speed penalty caused by the detour over the extra file. For the average user, this probably is hardly noticeable. – Daniel Jul 27 '13 at 19:55
• @Daniel Nice - I have incorporated this into my answer. – Andrew Swann Jul 28 '13 at 8:37