# How to draw a generalized Catalan number diagram using TikZ

This question is related to my previous question How to draw a Catalan number diagram on TikZ

I would like to be able to draw generalized Catalan number diagrams using Tikz. More concretely, I would like a 3 dimensional version of the previous 2 dimensional one. Three dimensional Catalan numbers are defined as 3-dimensional lattice paths using steps (1,0,0),(0,1,0) and (0,0,1) from the origin to the point (n,n,n), the points in the path satisfy 0 ≤ x_1 ≤ x_2 ≤ x_3.

-
Did you try just adding one more coordinate (for the z value) to all the points in the earlier solutions which only have xand y? – Peter Grill Sep 20 '12 at 21:40
Could you show a picture of the expected result? – Paul Gaborit Sep 20 '12 at 22:01
@PeterGrill There are more paths in a three-dimensional lattice than that... That way - if it works just like that, what I doubt - would only show the same paths I had but projected on the xy-plane. – Combin Sep 21 '12 at 12:10
@PaulGaborit I would like it to look like the "empty room" in the link below (a 3d-grid) but with a colored path moving from (0,0,0) to (n,n,n). (techpubs.sgi.com/library/dynaweb_docs/0650/SGI_EndUser/books/…) – Combin Sep 21 '12 at 12:16

As, for my taste, Paul Gaborit's answer is a little to unornamental, here's another one:

## Code

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\begin{document}

\newcommand{\xangle}{-10}
\newcommand{\yangle}{230}
\newcommand{\zangle}{270}

\newcommand{\xlength}{1}
\newcommand{\ylength}{1}
\newcommand{\zlength}{1}

\pgfmathsetmacro{\xx}{\xlength*cos(\xangle)}
\pgfmathsetmacro{\xy}{\xlength*sin(\xangle)}
\pgfmathsetmacro{\yx}{\ylength*cos(\yangle)}
\pgfmathsetmacro{\yy}{\ylength*sin(\yangle)}
\pgfmathsetmacro{\zx}{\zlength*cos(\zangle)}
\pgfmathsetmacro{\zy}{\zlength*sin(\zangle)}

\begin{tikzpicture}
[   x={(\xx cm,\xy cm)},
y={(\yx cm,\yy cm)},
z={(\zx cm,\zy cm)},
]
\foreach \a in {0,...,3}
{   \draw[help lines]
(\a,0,0) -- (\a,0,3)
(0,0,\a) -- (3,0,\a)
node at (\a,0,-0.2) {\tiny\a}
node at (\a,3,3.2) {\tiny\a}

(0,\a,0) -- (0,\a,3)
(0,0,\a) -- (0,3,\a)
node at (0,\a,-0.2) {\tiny\a}
node at (3,\a,3.2) {\tiny\a}

(0,\a,3) -- (3,\a,3)
(\a,0,3) -- (\a,3,3)
node at (3.2,0,\a) {\tiny\a}
node at (0,3.2,\a) {\tiny\a};
}

\node at (1.5,0,-0.5) {x};
\node at (1.5,3,3.5) {x};

\node at (0,1.5,-1) {y};
\node at (3,1.5,4) {y};

\node at (3.6,0,1.5) {z};
\node at (0,3.6,1.5) {z};

\draw[red,thin] (0,0,0) -- (0,0,3);
\draw[red,thin] (0,0,0) -- (0,3,3);
\draw[red,thin] (0,0,0) -- (3,3,3);
\draw[red,thin] (0,0,3) -- (0,3,3);
\draw[red,thin] (0,0,3) -- (3,3,3);
\draw[red,thin] (0,3,3) -- (3,3,3);

\draw[very thick,blue]
(0,0,0)
-- ++(0,0,1)
-- ++(0,1,0)
-- ++(0,0,1)
-- ++(1,0,0)
-- ++(0,1,0)
-- ++(0,0,1)
-- ++(1,0,0)
-- ++(0,1,0)
-- ++(1,0,0);

\draw[dashed,blue]
(1,1,2) -- (1,1,3);

\end{tikzpicture}

\end{document}


## Result

-
+1 Nice result! – Paul Gaborit Sep 21 '12 at 21:07

Here is an example of 3d path:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \a in {0,...,3}{
\draw[help lines]
(\a,0,0) -- (\a,0,-3)
(0,0,-\a) -- (3,0,-\a)
%
(0,\a,0) -- (0,\a,-3)
(0,0,-\a) -- (0,3,-\a)
%
(0,\a,-3) -- (3,\a,-3)
(\a,0,-3) -- (\a,3,-3)
;
}

\draw[dashed,blue]
(1,0,-1) -- (1,1,-1)
(3,0,-1) -- (3,1,-1)
(3,0,-2) -- (3,2,-2)
;

\draw[line width=1pt,blue]
(0,0,0)
-- ++(1,0,0)
-- ++(0,1,0)
-- ++(0,0,-1)
-- ++(1,0,0)
-- ++(1,0,0)
-- ++(0,1,0)
-- ++(0,0,-1)
-- ++(0,1,0)
-- ++(0,0,-1)
;

\end{tikzpicture}
\end{document}

-
Thank you @PaulGaborit! – Combin Sep 21 '12 at 13:47
Where do the dashed lines come from? – Tom Bombadil Sep 21 '12 at 16:38
@TomBombadil Good point! This was an oversight when copy/paste... – Paul Gaborit Sep 21 '12 at 21:03