# Loops in tikz for accessing labels

I would like to create something like this drawing using Tikz:

I found a nice tikz example that creates something similar:

\documentclass[border=10pt]{standalone}%
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\foreach \n in {0,...,4} {
\foreach \k in {0,...,\n} {
\node at (\k-\n/2,-\n) {${\n \choose \k}$};
}
}
\end{tikzpicture}

\end{document}


I thought it would be trivial to change the label at a node by accessing the label on a list, something like lst[\n], where lst contains monomials, but it turned out to be more complicated than I thought. Besides, I couldn't find a way to add the exponent programatically. Is this possible with tikz using loops, or do I have to create an ad-hoc solution?

\documentclass[border=10pt]{standalone}%
\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\newcommand{\exponent}[2]{\ifthenelse{#2=0}{}{\ifthenelse{#2=1}{$#1$}{$#1^{#2}$}}}

\begin{tikzpicture}
\foreach \x in {0,...,7} {
\foreach \n in {0,...,\x} {

\pgfmathparse{int(subtract(\x,\n))}
\edef\xexp{\pgfmathresult}
\edef\nexp{\n}

\node[inner sep = 8pt] (X\xexp-N\nexp) at (2*\n-\x,-\x) {\ifthenelse{\xexp=0 \AND \nexp=0}{$1$}{\exponent{\xi}{\xexp}\exponent{\eta}{\nexp}}};
\node[inner sep = 16pt] (dX\xexp-N\nexp) at (2*\n-\x,-\x) {}; % d: dummy
}
}

\draw[blue, thick, dashed] (X0-N0.north) -- (X1-N0.west) -- (X1-N1.south) -- (X0-N1.east) node[inner sep = 0] (S1) {} -- cycle;

\draw[red, thick, dashed] (dX0-N0.north) -- (dX4-N0.west) -- (dX4-N1.south) -- (dX3-N1.south) --
(dX1-N3.south) -- (dX1-N4.south) -- (dX0-N4.east) node[inner sep = 0] (S4) {} -- cycle;

\node[right = 0.5cm, above = 0.5cm] (S1t) at (S1.west) {$S_1(\Omega_{\rm st}^{(q)})$};
\draw[blue] (S1t) edge[thick, out=270, in=90, ->] (S1);

\node[right = 0.5cm, above = 0.5cm] (S4t) at (S4.west) {$S_4(\Omega_{\rm st}^{(q)})$};
\draw[red] (S4t) edge[thick, out=270, in=90, ->] (S4);

\end{tikzpicture}

\end{document}


• Impressive! That’s exactly what I was looking for. Thanks! – aaragon Jan 23 '18 at 19:20
• @aaragon Happy to help – caverac Jan 23 '18 at 20:29