# TikZ compiles in body, but not as command [duplicate]

Possible Duplicate:
Problem with defining shortcuts for TikZ matrices.

When I use the code below to draw a tikzpicture in the body of my document, it works just fine. However, when I define a command and try to use it, I get a Missing \$ inserted error.

\documentclass[11pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{matrix, shapes, backgrounds}
\newcommand{\matrixa}{
\begin{tikzpicture}[>=latex]
\matrix (A) [matrix of math nodes,%
left delimiter  = (,%
right delimiter = )] at (0,0) {%
\cdot & \cdot  & \cdot  \\
\cdot & \cdot  & \cdot  \\
\cdot & \cdot  & \cdot  \\
\\ };%
\end{tikzpicture}}
\begin{document}
\matrixa
\end{document}

-

## marked as duplicate by Loop Space, Guido, clemens, Claudio Fiandrino, ThorstenNov 17 '12 at 9:17

Welcome to TeX.SX! –  egreg Nov 16 '12 at 23:41

In this case you have to use a replacement for the ampersand:

\documentclass[11pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{matrix, shapes, backgrounds}
\newcommand{\matrixa}{%
\begin{tikzpicture}[>=latex]
\matrix (A) [matrix of math nodes,
left delimiter  = (,
ampersand replacement=\&,
right delimiter = )] at (0,0)
{
\cdot \& \cdot \& \cdot \\
\cdot \& \cdot \& \cdot \\
\cdot \& \cdot \& \cdot \\
};
\end{tikzpicture}}
\begin{document}
\matrixa
\end{document}


It's not necessarily \&, but this is usually chosen.

-
Thanks for the helpful tip! Is there an alternative to newcommand that will just paste the indicated code wherever you call it? (so that my original version would work.) –  ZRmath Nov 18 '12 at 2:23