# Draw a recursive staircase function

I am trying to draw the following figures shown below

However I am really unsure how to go about it. Sorry for not including a MWE as I usually do. But I do not know where I am going to start.

Like this?

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/staircase/.style={code={
\draw[mydash] (-0.5*#1,-0.5*#1) -| (0.5*#1,0.5*#1);
\foreach \XX in {1,...,#1}
{\draw[mydash] (-0.5*#1+\XX-1,-0.5*#1+\XX-1) |- ++ (1,1);
\ifnum\XX<#1
\draw[mydash,red] (-0.5*#1+\XX,-0.5*#1) -- ++ (0,\XX);
\draw[mydash,blue] (0.5*#1,0.5*#1-\XX) -- ++ (-\XX,0);
\fi}}},mydash/.style={thick,shorten <=1mm,dash pattern=on 8mm off 2mm}]

\path (0,-1.5) pic{staircase=1} (2,-1) pic{staircase=2}
(5,-0.5) pic{staircase=3} (9,0) pic{staircase=4};
\end{tikzpicture}
\end{document}


This version has the origin at its "center" so is easier to rotate. However, in order to reproduce your figure you may be interested in a version that has the origin in the middle of the base line.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/staircase/.style={code={
\draw[mydash] (-0.5*#1,0) -| (0.5*#1,#1);
\foreach \XX in {1,...,#1}
{\draw[mydash] (-0.5*#1+\XX-1,\XX-1) |- ++ (1,1);
\ifnum\XX<#1
\draw[mydash,red] (-0.5*#1+\XX,0) -- ++ (0,\XX);
\draw[mydash,blue] (0.5*#1,#1-\XX) -- ++ (-\XX,0);
\fi}}},mydash/.style={thick,shorten <=1mm,dash pattern=on 8mm off 2mm}]

\path \foreach \X in {1,...,5}
{({\X*(\X+1)/2-1},0) pic{staircase=\X} node[below=0.5ex] {$F_\X$}};
\end{tikzpicture}
\end{document}


As for your uneven spacing problem: try

\documentclass{article}

\usepackage{geometry}

\usepackage{subcaption}
\usepackage{tikz}

\tikzset{%
pics/staircase/.style={code={
\draw[mydash] (-0.5*#1,-0.5*#1) -| (0.5*#1,0.5*#1);
\foreach \XX in {1,...,#1}
{\draw[mydash] (-0.5*#1+\XX-1,-0.5*#1+\XX-1) |- ++ (1,1);
\ifnum\XX<#1
\draw[mydash,red] (-0.5*#1+\XX,-0.5*#1) -- ++ (0,\XX);
\draw[mydash,blue] (0.5*#1,0.5*#1-\XX) -- ++ (-\XX,0);
\fi}}},mydash/.style={thick,shorten <=1mm,dash pattern=on 8mm off 2mm}
}
\newcommand{\staircase}[1]{%
\begin{tikzpicture}
\path (0,0) pic{staircase=#1};
\end{tikzpicture}
}
\begin{document}

\begin{figure}[!h]
\centering
\foreach \X in {1,...,4}
{\begin{subfigure}[b]{\X.5cm}
\centering
\staircase{\X}
\caption*{$F_\X$}
\end{subfigure}}
\caption{}
\end{figure}
\end{document}


• I used your first version like so pastebin.com/b1zsCf7D . However this leads to some uneven spacing, do you know a fix for this as well? Btw, how do you answer these questions so fast! ;-) – N3buchadnezzar Jun 12 at 17:31
• @N3buchadnezzar Which spacing is uneven? Does it help if you replace the first draw command of the pic by \draw[mydash] (-0.5*#1,-0.5*#1) -- (0.5*#1,-0.5*#1) -- (0.5*#1,0.5*#1);? Or do you mean the gaps between the subfigures? This comes from the widths you give them by saying \begin{subfigure}[b]{0.09\textwidth} and so on. (Slow marmots get caught by the eagels. Will be offline now for a bit.) – user121799 Jun 12 at 17:39