When I pass code containing a TikZ matrix to a TeX macro it prints the following error message: ! Package pgfbasematrix Error: Single ampersand used with wrong catcode.
\documentclass{article}
\usepackage{tikz}
\def\identity#1{#1}
\begin{document}
\begin{tikzpicture}
\identity{
\matrix {
\node {1}; & \node{2};\\
\node {3}; & \node{4};\\
};
}
\end{tikzpicture}
\end{document}
Why does this happen? Is there any way to make this work?
Rationale:
I use such macros to create exercise sheets for my students. I like the answers to be embedded in the source code but naturally do not want them to show up on the exercise sheet. Thus I mark up my code with macros like \problem{}
and \answer{}
which are then suitably defined in the document header depending on whether I want to compile the exercise sheet or the reference solution.
The probably best way (I guess) would be to replace my markup macros with environments that use \iftrue
or \iffalse
to include or skip parts of the document, but currently I'm not ready to convert all my files. That's why I'm looking for a short term solution to that problem (that, and I'm curious).