3

here is a pictures of a catalan path:

enter image description here

It is a lattice path in Z^2 starting a some point (a,b) and using the steps (1,1) and (1,-1). It can be decoded (knowing the starting point) as a ballot sequence 111-1-1.... where 1 corresponds to up(1,1) and -1 to down(1,-1). Is there a way using tex to just type the ballot sequence and get the corresponding catalan path?

percusse
  • 157,807
Mare
  • 291
  • 1
  • 7

2 Answers2

6

A simple foreach can do it:

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

\begin{document}
\begin{tikzpicture}
\draw[->] (0,-1)--++(90:4);
\draw[->] (-1,0)--++(0:12);
\coordinate (aux) at (0,0);
\foreach \i in {1,-1,1,1,1,-1,1,-1,-1,1,-1}
    \draw[->] (aux)--++(1,\i) coordinate (aux);
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
3

Just about does it (although without the axes):

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{arrow path/.style={decoration={show path construction,
  lineto code={
    \path [->, every lineto/.try] 
      (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
  }}, decorate},
  every lineto/.style={draw, thick, ->},
  lineto/.style={every lineto/.append style={#1}},
  ballot sequence/.style={arrow path, .. ballot sequence=#1@@;},
  .. ballot sequence/.code args={#1#2#3;}{%
    \if#1@\else
      \if#1-
      \tikzset{symbol #1#2/.try, .. ballot sequence=#3;}%
    \else
      \tikzset{symbol #1/.try, .. ballot sequence=#2#3;}%
    \fi\fi%
  },
  symbol -1/.style={insert path={ -- ++(1,-1) }},
  symbol 1/.style={insert path={ -- ++(1,1) }}
}
\begin{document} 
\begin{tikzpicture}[>=stealth]
\path [lineto=red]   (5,0) [ballot sequence={1-1}];
\path [lineto=green] (3,3) [ballot sequence={111-1-1-1}];
\path [lineto=blue]  (0,6) [ballot sequence={1-1111-11-1-11-1-1}];
\end{tikzpicture}
\end{document}

enter image description here

Mark Wibrow
  • 70,437