8

I am a newbie and I asked in How to create rectangles like in this example? about how to make some rectangles. I am trying to read the code and understand it as I need rectangles rather than squares. But I cannot find in the net what this #1 to #4 are? As the notation is short I can't even search it up. So I would be happy if somebody helps me with it.

\newcommand\catalannumber[3]{
  % start point, size, Dyck word (size x 2 booleans)
  \fill[cyan!25]  (#1) rectangle +(#2,#2);
  \fill[fill=lime]
  (#1)
  \foreach \dir in {#3}{
    \ifnum\dir=0
    -- ++(1,0)
    \else
    -- ++(0,1)
    \fi
  } |- (#1);
  \draw[help lines] (#1) grid +(#2,#2);
  \draw[dashed] (#1) -- +(#2,#2);
  \coordinate (prev) at (#1);
  \foreach \dir in {#3}{
    \ifnum\dir=0
    \coordinate (dep) at (1,0);
    \else
    \coordinate (dep) at (0,1);
    \fi
    \draw[line width=2pt,-stealth] (prev) -- ++(dep) coordinate (prev);
  };
}
Naji
  • 1,505

1 Answers1

12

The "#" has little to do with TikZ. It's used in the expression of formal parameters in the definition of a command (here: \catalannumber). In lay terms, #1 refers to the first argument passed to \catalannumber, #2 refers to the second argument, etc.

I'm not familiar with Catalan numbers, so I'm not even sure that what follows makes any mathematical sense, but if you want to get a rectangular lattice instead of a square one, you can adapt the code from How to draw a Catalan number diagram on TikZ as follows. The second argument to \catalannumberrectangle is "m,n", where "m" is the number of columns and "n" is the number of rows of the lattice.

enter image description here

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\newcommand\catalannumberrectangle[3]{
  % start point, size, Dyck word (size x 2 booleans)
  \fill[cyan!25]  (#1) rectangle +(#2);
  \fill[fill=lime]
  (#1)
  \foreach \dir in {#3}{
    \ifnum\dir=0
    -- ++(1,0)
    \else
    -- ++(0,1)
    \fi
  } |- (#1);
  \draw[help lines] (#1) grid +(#2);
  \draw[dashed] (#1) -- +(#2);
  \coordinate (prev) at (#1);
  \foreach \dir in {#3}{
    \ifnum\dir=0
    \coordinate (dep) at (1,0);
    \else
    \coordinate (dep) at (0,1);
    \fi
    \draw[line width=2pt,-stealth] (prev) -- ++(dep) coordinate (prev);
  };
}

\begin{document}
\begin{tikzpicture}
  \catalannumberrectangle{0,-9}{8,4}{0,1,0,0,1,1,0,1,0,0,0,0};
\end{tikzpicture}
\end{document}
jub0bs
  • 58,916
  • Thanks a lot, can you please also tell me why I get this: http://i46.tinypic.com/21eyubn.jpg

    Rather than the one like yours?

    – Naji Apr 05 '13 at 22:10
  • this is what I run:\catalannumberrectangle{0,-4}{4,5}{0,1,0,0,1,1,0,1,1}; – Naji Apr 05 '13 at 22:11
  • 1
    Do you mean that you get a different result with the code from my answer? If so, you might want to check that you have the latest version of PGF/TikZ. Or do you mean that you want to get an output like in the picture you link to? – jub0bs Apr 05 '13 at 22:13
  • no I want my result to look like yours. Mine is only showing a lime triangle as you can see! I will try to update my TikZ then... – Naji Apr 05 '13 at 22:20
  • problem solved after updating. Thanks again. – Naji Apr 06 '13 at 00:48