Here is some code that works perfectly well (this code is based on this answer):
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes, nodes in empty cells,
nodes={align=center},
text width=.5cm,text depth=0.2cm,text height=0.3cm]
(mymatrix)
{
a & b & c & d \\
a & b & c & d \\
a & b & c & d \\
a & b & c & d \\
};
% Now some tikz styling is applied
\end{tikzpicture}
\end{document}
However, I would like to move the TikZ commands out of the document and be able to invoke this structure by calling an environment. Something like this:
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\newenvironment{mymatrix}{%
% Some code here
}{%
% More code here
}
\begin{document}
\begin{mymatrix}
a & b & c & d \\
a & b & c & d \\
a & b & c & d \\
a & b & c & d \\
\end{mymatrix}
\end{document}
The syntax used within the document is important, and cannot be touched. (If you want to know why: the TeX source is passed to various other systems, and for the purposes of these systems what's written in mymatrix has to be treated like an array or tabular environment.)
As we know (e.g. from this post), we can't leave unbalanced braces in a definition, so we obviously cannot just put the \matrix command into the opening part of the environment definition.
I experimented with lrbox and similar such things as suggested in other posts, but it doesn't like the & delimiters (for reasons that I think I now understand).
Previously, I also experimented with the tabularray package to format this table-like object. It is easy to use and looks great, but it causes LaTeX compilations to run for several minutes even on fairly small documents, and often the compilation fails completely (and, on some machines, it simply doesn't work at all). Hence I'm now looking at a solution using TikZ.
So, is there some way to pass the contents of mymatrix into the argument for a tikz \matrix?
tabularraypackage (which is good but too processor hungry), but notnicematrix- I will look into this, thanks! – rbrignall Apr 29 '22 at 19:16