[Tex/LaTex] How to represent a house of cards

cardstikz-pgf

I would like to represent the construction of a house of cards, probably in tikz. Before creating my own, has anyone done anything similar?

A 3 storey house of cards represented by lines

Best Answer

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/Lam/.style={code={
\draw (-0.45,0.1) -- (0,0.9) -- (0.45,0.1) (-0.45,0) -- (0.45,0);}}]
 \path foreach \Y in {1,2,3}
  {foreach \X in {1,...,\Y} {(\X-\Y/2,1-\Y) pic{Lam}}};
\end{tikzpicture}
\end{document}

enter image description here

Here is a version (more faithful to the original post) without lines at the bottom and much more. You only need to say

\path pic{card house=3};

See the examples in

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/Lam/.style={code={
    \def\pkv##1{\pgfkeysvalueof{/tikz/Lam/##1}}
    \draw (-0.5*\pkv{shrink}*\pkv{width},0.1) -- 
    (0,\pkv{shrink}*\pkv{height}) -- (0.5*\pkv{shrink}*\pkv{width},0.1) 
     \ifnum#1<\pkv{cutoff} (-0.5*\pkv{shrink}*\pkv{width},0) -- (0.5*\pkv{shrink}*\pkv{width},0)
     \fi;}},
    pics/card house/.style={code={
     \tikzset{Lam/cutoff=#1}
     \path foreach \Y in {1,...,#1}
      {foreach \X in {1,...,\Y} 
       {(\X*\pgfkeysvalueof{/tikz/Lam/width}-\Y*\pgfkeysvalueof{/tikz/Lam/width}/2,
       -\Y*\pgfkeysvalueof{/tikz/Lam/height}) pic{Lam=\Y}}};
    }},  
    /tikz/Lam/.cd,cutoff/.initial=3,width/.initial=1,height/.initial=1,shrink/.initial=0.9,
    ]
 \path pic{card house=3} (4,0) pic[red]{card house=4} (9,0) pic[blue,thick]{card house=5};  
\end{tikzpicture}
\end{document}

enter image description here

What could this be good for? Oh, of course!

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikzlings}
\newif\iftikzling
\begin{document}
\begin{tikzpicture}[pics/Lam/.style={code={
    \def\pkv##1{\pgfkeysvalueof{/tikz/Lam/##1}}
    \draw (-0.5*\pkv{shrink}*\pkv{width},0.1) -- 
    (0,\pkv{shrink}*\pkv{height}) -- (0.5*\pkv{shrink}*\pkv{width},0.1) 
     \ifnum#1<\pkv{cutoff} (-0.5*\pkv{shrink}*\pkv{width},0) -- (0.5*\pkv{shrink}*\pkv{width},0)
     \fi;
     \iftikzling
     \tikzling[scale=0.25*\pkv{height}*\pkv{shrink}]
     \fi}},
    pics/card house/.style={code={
     \tikzset{Lam/cutoff=#1}
     \path foreach \Y in {1,...,#1}
      {foreach \X in {1,...,\Y} 
       {(\X*\pgfkeysvalueof{/tikz/Lam/width}-\Y*\pgfkeysvalueof{/tikz/Lam/width}/2,
       -\Y*\pgfkeysvalueof{/tikz/Lam/height}) pic{Lam=\Y}}};
    }},  
    /tikz/Lam/.cd,cutoff/.initial=3,width/.initial=1,height/.initial=1,shrink/.initial=0.9,
    tikzling/.is if=tikzling
    ]
 \path[/tikz/Lam/.cd,height=3,width=3,tikzling=true]  pic[blue,thick]{card house=5};  
\end{tikzpicture}
\end{document}

enter image description here