1

For the following MWE, I would like to know how I can reduce the code by having two nested for loops.

\documentclass[border=10mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of nodes,row sep=3mm,column sep=3mm,nodes in empty cells]
{
    $a_{11}$ & $a_{12}$ & $a_{13}$ & $a_{14}$ & $a_{15}$ &  $\dots$ \\
    $a_{21}$ & $a_{22}$ & $a_{23}$ & $a_{24}$ & $a_{25}$ &          \\
    $a_{31}$ & $a_{32}$ & $a_{33}$ & $a_{34}$ & $a_{35}$ &          \\
    $a_{41}$ & $a_{42}$ & $a_{43}$ & $a_{44}$ & $a_{45}$ &          \\
    $a_{51}$ & $a_{52}$ & $a_{53}$ & $a_{54}$ & $a_{55}$ &          \\
    $\vdots$ &          &          &          &          & $\ddots$ \\
};
\end{tikzpicture}
\end{document}

enter image description here

Diaa
  • 9,599

1 Answers1

2

This is a bit tricky, and there several ways of doing this. I am using this one, but I am not saying this is necessarily the best one.

\documentclass[tikz,border=10mm]{standalone}
\usetikzlibrary{matrix}
\usepackage{etoolbox}
\begin{document}
% based on https://tex.stackexchange.com/a/349378/121799
\newcommand*\mytablecontents{}
\foreach \i in {1,...,5}{
  \foreach \j in {1,...,5}{
    \xappto\mytablecontents{$a_{\i\j}$ \&}
  }
  \ifnum\i=1
  \gappto\mytablecontents{$\cdots$}
  \fi
  \gappto\mytablecontents{\\}
}
\begin{tikzpicture}
\matrix (A) [matrix of nodes,row sep=3mm,column sep=3mm,nodes in empty cells,
ampersand replacement=\&]
{
\mytablecontents
$\vdots$ \&          \&          \&          \&          \& $\ddots$ \\
};
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    I would like to add that this is very similar to this answer, which I had upvoted some time ago and forgot. –  Sep 25 '18 at 16:14
  • is it possible to reveal what code \mytablecontents contains? – Diaa Sep 25 '18 at 16:19
  • 1
    @Diaa It just contains what you create in your code by hand. More precisely, the first 5 rows of the matrix contents (and with & replaced by \&). –  Sep 25 '18 at 16:23
  • I understand. Generally, in case I would like to know the content of any command, how can I reveal it? – Diaa Sep 25 '18 at 16:34
  • 1
    @Diaa You can use \show\mytablecontents which will tell you \mytablecontents=macro: ->$a_{11}$ \&$a_{12}$ \&$a_{13}$ \&$a_{14}$ \&$a_{15}$ \&$\cdots $\\$a_{21}$ \& $a_{22}$ \&$a_{23}$ \&$a_{24}$ \&$a_{25}$ \&\\$a_{31}$ \&$a_{32}$ \&$a_{33}$ \& $a_{34}$ \&$a_{35}$ \&\\$a_{41}$ \&$a_{42}$ \&$a_{43}$ \&$a_{44}$ \&$a_{45}$ \& \\$a_{51}$ \&$a_{52}$ \&$a_{53}$ \&$a_{54}$ \&$a_{55}$ \&\\ and interrupt the compilation. Other ways to debug include \typeouts: \typeout{\mytablecontents}. –  Sep 25 '18 at 17:29
  • I am sorry for asking this basic question: when trying to replace \newcommand* with \newcommand, the output doesn't differ. So, may I know why you chose \newcommand*? – Diaa Sep 25 '18 at 18:49
  • 1
    @Diaa Oh, I just copied that from the answer I quote. There is no particular reason here, really, but the * doesn't hurt either. Things will be different if you redo these tricks, in which case you may use \renewcommand in the next matrix. –  Sep 25 '18 at 19:30