1

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?

rbrignall
  • 1,564

1 Answers1

3

Do you know the nicematrix package and have you tried it to achieve your goal? Here is a short example:

\documentclass[multi=mymatrix]{standalone}
\usepackage{tikz}
\usepackage{nicematrix}

\newenvironment{mymatrix}{% \begin{NiceTabular}{cccc} \CodeBefore \cellcolor[HTML]{FFFF88}{1-1,1-2,1-3} \rectanglecolor{blue!15}{3-3}{4-4} }{% \CodeAfter \begin{tikzpicture} \draw (2-2) circle (2mm); \end{tikzpicture} \end{NiceTabular} }

\begin{document} \begin{mymatrix} a & b & c & d \ a & b & c & d \ a & b & c & d \ a & b & c & d \ \end{mymatrix} \begin{mymatrix} a & b & c & d \ a & b & c & d \ a & b & c & d \ a & b & c & d \ \end{mymatrix} \end{document}

If that's not what you want, show an example picture of your target or/and your TikZ code. You can also describe the desirable design of your table. Maybe you do not need TikZ for your design.

  • 1
    Oh, I must look into this! I tried the tabularray package (which is good but too processor hungry), but not nicematrix - I will look into this, thanks! – rbrignall Apr 29 '22 at 19:16
  • I'm going to mark this as the "answer", partly as this is the only answer and nicematrix seems useful, even though nicematrix doesn't seem to have enough control to specify different hlines and vlines for my use case. – rbrignall May 03 '22 at 13:59