# Displaying [ ], |, etc. in a TikZ matrix node

I would like to typeset a specrtal sequence using TikZ, as suggested in this post, but the entries of my spectral sequence contain brackets and vertical lines, e.g. [x|...|x]. If I type them naively, the entire node appears blank. How can I circumvent this problem ?

The code I'm using is the following

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,
nodes in empty cells,nodes={minimum width=5ex,
minimum height=5ex,outer sep=-5pt},
column sep=1ex,row sep=1ex]{
0     &   0  &  0  &  \vdots  & \\
0     &  [x] &  0  &  k   & \\
0     &   0  &  0  &  \vdots  & \\
0     &   0  &  0  & |1| & \\
0     &   0  & []  &  0  & \\
-2    &  -1  &  0  &     & \\};
\end{tikzpicture}
\end{document}

It returns an error when trying to compile the line including "|1|", but doesn't seem to be bothered with the "[x]", but none show on the output.

-
Could you include an example of the code that you are using which doesn't work. Try to make it as complete as you can - a compileable document with \documentclass and the packages that you're using as these might all have bearing on the case. – Loop Space Aug 24 '12 at 14:08
Just place braces around each problematic character and it should go through, – percusse Aug 24 '12 at 14:33
Thank you percusse, this worked (and I feel like an idiot, but that's not a problem). Do you want to make an answer out of that, or should I make it ? – Samuel T Aug 24 '12 at 14:37
@SamuelT No worries, you should see me when I'm coding anything else than TikZ :) Andrew gave yet another amazing answer so we should simply upvote that. – percusse Aug 24 '12 at 14:46

When using the key matrix of math nodes (or matrix of nodes), TikZ intends on interpreting everything between the ampersands as the contents of a node. However, we (the users) often want the ability to modify the style of those nodes on a case-by-case basis. In normal usage, this is done via the parameters to the \node command. Also, we like to modify how the matrix lays out its nodes a bit. So what TikZ does is to allow us to specify these things using special syntax that must occur first in the cell. Stuff in vertical lines is added between the \node command and its contents. So you can draw a specific node by writing |[draw]| and you can name it explicitly by writing |(name)|. In addition, the ampersand is actually a command which can take an optional argument and this translates into an extra spacing between the rows (if given on the first row).

So your square brackets are being eaten up as row-skips (and then ignored as they aren't on the first row) and your vertical lines are getting into the machinery of the node command. To protect each you need to ensure that the first character of the cell is not special. Either enclose the whole contents in braces or just put a {} in front.

Here's both versions:

\documentclass[a4paper,12pt]{article}
%\url{http://tex.stackexchange.com/q/68600/86}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,
nodes in empty cells,nodes={minimum width=5ex,
minimum height=5ex,outer sep=-5pt},
column sep=1ex,row sep=1ex]{
0     &   0  &  0  &  \vdots  & \\
0     &  {[x]} &  0  &  k   & \\
0     &   0  &  0  &  \vdots  & \\
0     &   0  &  0  & {}|1| & \\
0     &   0  & {}[]  &  0  & \\
-2    &  -1  &  0  &     & \\};
\end{tikzpicture}
\end{document}

Incidentally, you could use execute at empty cell={\node {0};} to avoid having to type all those zeros:

\documentclass[a4paper,12pt]{article}
%\url{http://tex.stackexchange.com/q/68600/86}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,
execute at empty cell={\node {0};},nodes={minimum width=5ex,
minimum height=5ex,outer sep=-5pt},
column sep=1ex,row sep=1ex]{
&  &  &  \vdots   \\
&  {[x]} &  &  k  \\
&   &  &  \vdots  \\
&   &  & {}|1|  \\
&   & {}[]  &   \\
-2    &  -1  & & {} \\};
\end{tikzpicture}
\end{document}

-
+1 for giving additional hints to the OP, albeit they're not directly related to the problem! – yo' Aug 24 '12 at 14:39
Thank you for this great, complete answer ! – Samuel T Aug 24 '12 at 14:58