5

I'm trying to package a tikz key math array that lets me wrap the contents of a node inside an array environment in math mode, but I'm getting errors that I'm unable to debug.

Here's the code:

\documentclass{article}

\usepackage{tikz}
\usepackage{amsmath}
\usepackage{array}

\tikzset{
  math array/.style={
    execute at begin node=\(\begin{array}{#1},
    execute at end node=\end{array}\)
  },
}

\begin{document}

\begin{tikzpicture}
  \node {
    \(\begin{array}{c}
      x\\
      y
    \end{array}\)
  };

  % this does not work
  % \node[math array=c] {
  %   x\\
  %   y
  % };
\end{tikzpicture}

\end{document}

The math array key is trying to reproduce what is written explicitly in the first node, but when used (in the commented part) I get strange errors about missing braces.

! Missing } inserted.
<inserted text>
                }
l.27     y

What am I doing wrong? Is there an explanation of why is this happening?

EDIT: I've changed the example to have only a single column, to show that the problem does not lie in the & symbol.

1 Answers1

4

I got it to work with node text from https://tex.stackexchange.com/a/209188/4427

\documentclass{article}

\usepackage{tikz}
\usepackage{amsmath}
\usepackage{array}

\tikzset{
  math array/.style={
    execute at begin node=$\def\arraypreamble{#1},
    execute at end node=$,
  },
  node text/.style={node contents=\makearray{#1}},
}
\newcommand{\makearray}[1]{%
  \expandafter\array\expandafter{\arraypreamble}
  #1
  \endarray
}

\begin{document}

\begin{tikzpicture}
  \node[
    math array=@{}r@{}>{{}}l@{}, % better than rl
    node text={
      x &= y\\
      y &= z
    }
  ];
\end{tikzpicture}

\end{document}
egreg
  • 1,121,712