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.

5

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}
  • Sorry for the basic question. Why @{}r@{}>{{{{}}l@{} is better than rl? – AndréC Jul 24 '19 at 16:45
  • @AndréC Look at the outputs in the particular case. – egreg Jul 24 '19 at 17:30
  • Yes, the difference is obvious. What does @{} mean? – AndréC Jul 24 '19 at 17:51
  • 1
    @AndréC It means “no intercolumn space”. At the start and end “no padding”. – egreg Jul 24 '19 at 17:52
  • Thanks, but what’s the issue that breaks my code? It has apparently something to do with expansion, but what happens? – gigabytes Jul 24 '19 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.