8

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

enter image description here

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).

omegadot
  • 255
  • 1
    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. http://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
  • Which code exactly? There are eight answers to that question. – Torbjørn T. Dec 21 '17 at 23:57
  • You still don't seem to understand. Please carefully read the information on this page: minimal working example (MWE). – Henri Menke Dec 21 '17 at 23:58
  • @Henri - I understand MWEs are usually required and is the reason why I explicitly mentioned in my original posting that I simply didn't have one since I was unable to not make any real progress on the problem. – omegadot Dec 22 '17 at 00:01
  • @ Torbjørn - Sorry, I was working on the code provided in the accepted solution. – omegadot Dec 22 '17 at 00:01
  • Then you could have turned that into a complete document (documentclass, tikz package and document environment is missing) and added it to your question. – Torbjørn T. Dec 22 '17 at 00:24
  • @HenriMenke The MWE is in the linked question. What are you trying to get out of this forcing? Are you planning to answer the question? – percusse Dec 22 '17 at 09:20
  • @percusse As I already stated: http://idownvotedbecau.se/noattempt/ – Henri Menke Dec 22 '17 at 09:28
  • 1
    @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

5 Answers5

14
\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}

enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
14

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

output of code

\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}
Torbjørn T.
  • 206,688
4

Almost as compact in Metapost...

enter image description here

\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.

Thruston
  • 42,268
3

Here's a one path approach which allows one to use the whole construct for shadings or clipping.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\tikzset{
  triangles/.style 2 args={
    /utils/exec=\pgfmathtruncatemacro \n         {#2}%
                \pgfmathsetlengthmacro\size      {(#1)*\n}%
                \pgfmathtruncatemacro \iterations{\n/2},
    line join=round, insert path={
      (210:\size) coordinate(x) (-30:\size) coordinate(y) (90:\size)coordinate (z)
      foreach[evaluate={\f=\i/\n;}]\i in {0, ..., \iterations}{
          ($(x)!\f!(y)$) -- ($(x)!\f!(z)$) -- ($(y)!\f!(z)$)
        \unless\ifnum\pgfinteval{2*\i}=\n\relax
        --($(y)!\f!(x)$) -- ($(z)!\f!(x)$) -- ($(z)!\f!(y)$)
        \fi -- cycle}}}}
\begin{document}
\tikz\path[ball color=blue, triangles={5mm}{6}];
\tikz\matrix[
  row sep=5mm, column sep=5mm, execute at empty cell={
    \draw[fill=green!50!black, triangles=
      {5mm}{\pgfmatrixcurrentcolumn+5*\pgfmatrixcurrentrow-5}];}]{
   & & & & \\ & & & & \\ & & & & \\};
\end{document}

Output

enter image description here enter image description here

Qrrbrbirlbel
  • 119,821
1

With Asymptote: for a specific triangle

enter image description here

// http://asymptote.ualberta.ca/
size(6cm);
int n=8;
path p=(0,0)--(1,0)--dir(60)--cycle;
for (int i=0; i<n; ++i)
for (int j=0; j<n-i; ++j)
fill(shift(i*dir(60)+(j,0))*p,purple);

and for a series of triangle: we can make a function returning a picture with a parameter for its size and add to the current picture by add(alttriangle()); (default n=8), or add(alttriangle(5));, or add(alttriangle(n=5));

enter image description here

// http://asymptote.ualberta.ca/
size(12cm);
picture alttriangle(int n=8){
picture temp;
path p=(0,0)--(1,0)--dir(60)--cycle;
for (int i=0; i<n; ++i)
for (int j=0; j<n-i; ++j)
fill(temp,shift(i*dir(60)+(j,0))*p,purple);
return temp;
}

int n=7; for (int i=1; i<n; ++i) add(shift(i(i+1)/2,0)alttriangle(i));

//shipout(bbox(5mm,invisible));

Black Mild
  • 17,569