2

The following code does not compile.

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix} 
\begin{document}

\def\xxxxx
{\begin{tikzpicture}[% from https://tex.stackexchange.com/a/330411/121799
every left delimiter/.style={xshift=.75em},
every right delimiter/.style={xshift=-.75em},
]
\matrix[
  matrix of math nodes,
  left delimiter=(,
  right delimiter=),
  nodes in empty cells
] (m) {
  1 & ~~~ &  & 1 \\
  0 & & & \\
   & & & \\
  1 &  & 0& 1\\ 
};
\draw (m-1-1) -- (m-1-4);
\draw (m-1-1) -- (m-4-4);
\draw (m-2-1) -- (m-4-1);
\draw (m-2-1.-20) -- (m-4-3);
\draw (m-4-1) -- (m-4-3);
\draw (m-1-4) -- (m-4-4);
\end{tikzpicture}}

\[
\xxxxx
\]

\end{document}

The error is:

./bac_a_sable.tex:31: Undefined control sequence. \pgf@matrix@last@nextcell@options l.31 \xxxxx ? Process aborted

I've tried to add ampersand replacement=\& but it does not help.

How can I fix this code?

Colas
  • 6,772
  • 4
  • 46
  • 96

1 Answers1

6

Like any catcode change the & change doesn't work in the body of another command. ampersand replacement is there to avoid the issue (you didn't show what you tried)

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix} 
\begin{document}

\def\xxxxx
{\begin{tikzpicture}[% from https://tex.stackexchange.com/a/330411/121799
every left delimiter/.style={xshift=.75em},
every right delimiter/.style={xshift=-.75em},
]
\matrix[
  ampersand replacement=\&,
  matrix of math nodes,
  left delimiter=(,
  right delimiter=),
  nodes in empty cells
] (m) {
  1 \& ~~~ \&  \& 1 \\
  0 \& \& \& \\
   \& \& \& \\
  1 \&  \& 0\& 1\\ 
};
\draw (m-1-1) -- (m-1-4);
\draw (m-1-1) -- (m-4-4);
\draw (m-2-1) -- (m-4-1);
\draw (m-2-1.-20) -- (m-4-3);
\draw (m-4-1) -- (m-4-3);
\draw (m-1-4) -- (m-4-4);
\end{tikzpicture}}

\[
\xxxxx
\]

\end{document}
David Carlisle
  • 757,742