10

I have a picture in which each of the nodes looks something like this:

\node (species1) [shape=rectangle,draw] {
  \begin{tabular}{c c c}
  \multicolumn{3}{c}{{Species 1}} \\
  \colorbox{red}{G1a} & \colorbox{blue}{G2a} &
  \colorbox{green}{G3} \\
  \colorbox{blue}{G1b} & \colorbox{red}{G2b} & 
  \end{tabular}
};

The nodes look fine, but when I put them all into a matrix node to align them properly, the tabulars inside the nodes are all screwed up and I get errors like this one:

Extra alignment tab has been changed to \cr. ...{G1} & \colorbox{blue}{G2a} &
\colorbox (followed by: {green!50}{G3})

Is there are way to make this work?

  • Have you tried to surround the node contents with {}? (ie. { \begin{tabular}... and ...\end{tabular} }) – morbusg Jan 18 '11 at 20:26
  • @morbusg: Just tried it and it does not change anything. – Björn Pollex Jan 18 '11 at 20:30
  • make a complete example. Inside a tikzpicture environment it works. – user2478 Jan 18 '11 at 20:47
  • @Herbert: not in a matrix within a tikzpicture. – Caramdir Jan 18 '11 at 22:18
14

The problem is that TikZ needs to make & equivalent to \pgfmatrixnextcell, which interferes with the normal functioning of & as a table cell separation marker. Fortunately, TikZ includes the ampersand replacement option that lets you choose any other macro for matrix cell separation instead of &. The following example uses \& as replacement:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
    \matrix[ampersand replacement=\&] {
        \node (species1) [shape=rectangle,draw] {
            \begin{tabular}{c c c}
                \multicolumn{3}{c}{{Species 1}} \\
                \colorbox{red}{G1a} & \colorbox{blue}{G2a} & \colorbox{green}{G3} \\
                \colorbox{blue}{G1b} & \colorbox{red}{G2b} & 
            \end{tabular}
        };
        \& 
        \node {b}; \\
        \node {c}; \& \node {d}; \\
};
\end{tikzpicture}

\end{document}
4

You can put each node inside a save box, and then it will work:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\newsavebox{\speciesone}
\sbox{\speciesone}{
\begin{tabular}{c c c}
  \multicolumn{3}{c}{{Species 1}} \tabularnewline
  \colorbox{red}{G1a} & \colorbox{blue}{G2a} &
  \colorbox{green}{G3} \tabularnewline
  \colorbox{blue}{G1b} & \colorbox{red}{G2b} & 
  \end{tabular}
  }

\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes] {
\node (species1) [shape=rectangle,draw] {\usebox{\speciesone}};\\
};
\end{tikzpicture}


\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.