0

I'm working on a complicated set of figures using the pgfplots package. As part of the figures, I'd like to be able to create a matrix of nodes in which rows are added using a call to \pgfplotsinvokeforeach or a similar looping command. A minimal working example achieving something very close to what I want is given below.

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17, filter discard warning=false}

\usetikzlibrary{matrix}

\newcommand{\MakeMatrix}[1]{%
  \matrix (myMatrix) [ matrix of nodes ]
  {
     A \\
    \pgfplotsinvokeforeach{#1}{
      ##1
    } \\
  };
}

\begin{document}

\begin{tikzpicture}
  \MakeMatrix{B,C,D,E,F}
\end{tikzpicture}

\end{document}

The output looks like this: compiled LaTeX output

The document fails to compile, however, if I move the row separator \\ inside of the \pgfplotsinvokeforeach loop (thus achieving my ultimate goal of one row per loop iteration). I'd like to know if this is achievable with \pgfplotsinvokeforeach (ideal) or a similar approach to looping.

1 Answer 1

1

I adapted a solution from Build tabular content via \foreach. Minimal working example below.

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17, filter discard warning=false}

\usepackage{tikz}
\usetikzlibrary{matrix}

\newcommand*{\MyTempTableTokens}{}%
\makeatletter
  \newtoks\@tabtoks
  %%% assignments to \@tabtoks must be global, because they are done in \foreach
  \newcommand\AddTableTokens[1]{\global\@tabtoks\expandafter{\the\@tabtoks#1}}
  \newcommand\eAddTableTokens[1]{%
    \edef\MyTempTableTokens{#1}%
    \expandafter\AddTableTokens\expandafter{\MyTempTableTokens}%
    }
  %%% variable should always be operated on always locally or always globally
  \newcommand*\ResetTableTokens{\global\@tabtoks{}}
  \newcommand*\PrintTableTokens{\the\@tabtoks}
\makeatother

\newcommand{\MakeMatrix}[1]{%

  \ResetTableTokens
  \pgfplotsinvokeforeach{#1}{
     \eAddTableTokens{ ##1 \\ }
  }

  \matrix (myMatrix) [ matrix of nodes ] {
    A \\
    \PrintTableTokens
  };
}

\begin{document}

\begin{tikzpicture}
  \MakeMatrix{B,C,D,E,F}
\end{tikzpicture}

\end{document}

The output looks like this: compiled LaTeX output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .