# Triangular numbers (again) in TikZ

I am trying to recreate the following diagram for triangular numbers (smaller triangles within one larger triangle) using TikZ.

The colour is not important but white and grey is probably preferred.

I have tried to modify the code used here which produces a triangular grid but without success (I do not know how to produce more than one grid in a single figure and could not shade the triangles in the alternating pattern shown) and is the reason why I have not included any MWE.

Ideally the code would be flexible enough to choose the number of larger triangles required (6 are shown in the above figure).

• Show what you have done already. – Henri Menke Dec 21 '17 at 23:40
• Like I said, using the code in the link I can produce a single triangular grid only with no shading. I need multiple triangular grids on the same diagram that grow in size and are shaded along each column. This is what I do not know how to do. – omegadot Dec 21 '17 at 23:45
• So you refuse to make a minimal example? Here, have my downvote. idownvotedbecau.se/noattempt – Henri Menke Dec 21 '17 at 23:47
• @Henri - that is fine. It still does not solve my problem. Like I have said, I have tried to modify code I thought would be useful to solve my problem but I simply do not know how to make it work. What I can produce is a single triangular grid with 1, 2, 3, ... columns but cannot fit more than one of these grids onto a single diagram and have no ideal how to shade them as I require. – omegadot Dec 21 '17 at 23:53
• @HenriMenke After downvoting please stop alienating users then. There is nothing wrong with this question and you show no attempt of engaging to answer it so leave it to people who are interested. – percusse Dec 22 '17 at 11:25

\documentclass{standalone}
\usepackage{tikz}
\newcommand\triangles[2]{\def\w{#1}\foreach\n in{1,...,#2}{\path
([xshift=\w]current bounding box.south east)coordinate(O);
\foreach\s[count=\c]in{\n,...,1}{\foreach\m in{1,...,\s}{
\fill[green!50!black](O)++(60:{(\c-1)*\w})++(0:{(\m-1)*\w})--
++(60:\w)--++(-60:\w)--cycle;}}}}
\begin{document}
\begin{tikzpicture}\triangles{3mm}{8}\end{tikzpicture}
\end{document}


A small modification of Ignasi's answer on the question you refer to:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{shapes}

\tikzset{
tri/.style={
draw=none,
fill=black!50,
regular polygon,
regular polygon sides=3,
minimum size=2cm,
inner sep=0pt,
outer sep=0pt
}
}

\newcommand{\grid}[1]{
\foreach \i [count=\row from 0, remember=\row as \lastrow (initially 0)] in {0,...,#1}{
\foreach \j [count=\column from 0, remember=\column as \lastcolumn (initially 0)] in {0,...,\i}{
\ifnum\row=0
\node[tri](0-0){};
\else
\ifnum\column=0
\node[tri, anchor=corner 2](\row-0) at (\lastrow-0.corner 3) {};
\else
\node[tri, anchor=corner 2](\row-\column) at (\lastrow-\lastcolumn.corner 1) {};
\fi
\fi}}
}

\begin{document}
\begin{tikzpicture}

\foreach \x in {0,...,5}
{
\begin{scope}[xshift=2*\x cm + \x*\x cm]
\grid{\x}
\end{scope}
}
\end{tikzpicture}
\end{document}

• Thank you! It's nice to know that an answer is good enough to be recycled. – Ignasi Dec 22 '17 at 8:27
• \foreach options will never stop surprising me... – Rmano Dec 22 '17 at 8:39

Almost as compact in Metapost...

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
picture trig;
trig = image(fill origin -- right -- right rotated 60 -- cycle withcolor 1/2 white);
for k=1 upto 7:
for j=1 upto k:
for i=1 upto j:
draw trig
shifted (1/2(k+1)*k + 1/2(k-j) + i, sqrt(3/4)*(k-j))
scaled 5mm;
endfor
endfor
endfor
endfig;
\end{mplibcode}
\end{document}


The trick here is that the scaled 5mm scales both the triangle picture and the amount it's shifted.