Problem with defining shortcuts for TikZ matrices
TikZ cannot make the ampersand given in the argument to an active character. You could
use \pgfmatrixnextcell
instead of & and perhaps define a shortcut for it,
or use the ampersand replacement option:
\newcommand\mymatrix[1]{
\begin{tikzpicture}
\matrix[ampersand replacement=\&,matrix of math nodes] {#1};
\end{tikzpicture}%
}
\mymatrix{
a \& b \\
e \& f \\
}
From p179 of the manual (emphasis added, one minor but significant error corrected):
Even though TikZ seems to use
&
to separate cells, pgf actually uses a different command to separate cells, namely the command\pgfmatrixnextcell
and using a normal&
character will normally fail. What happens is that, TikZ makes & an active character and then defines this character to be equal to\pgfmatrixnextcell
. In most situations this will work nicely, but sometimes&
cannot be made active; for instance because the matrix is used in an argument of some macro or the matrix contains nodes that contain normal {tabular} environments. In this case you can use the following option to avoid having to type\pgfmatrixnextcell
each time:/tikz/ampersand replacement= macro name or empty (no default)
If a macro name is provided, this macro will be defined to be equal to
\pgfmatrixnextcell
inside matrices and&
will not be made active. For instance, you could sayampersand replacement=\&
and then use\&
to separate columns as in the following example:\tikz \matrix [ampersand replacement=\&] { \draw (0,0) circle (4mm); \& \node[rotate=10] {Hello}; \\ \draw (0.2,0) circle (2mm); \& \fill[red] (0,0) circle (3mm); \\ };
There is presumably some horrendously complicated way to turn on the active nature of the ampersand again inside the macro argument, but the above would seem the cleanest way.