3

I would like to create a Dyck path in Latex with two additional features. First, I would like to number all the East step except(!) for the last one. Secondly, for each valley (that is, an East step that is followed by a North step), I would like to draw "lasers" which would be lines that are parallel to the diagonal and that stops once it reaches the Dyck path. This is similar, but not exactly the same as the "laser construction" in this paper. See e.g. Figure 6.

I already have some code to obtain a Dyck path.

\documentclass{article}
\usepackage{tikz}

\newcommand{\NEpath}[4]{
    \fill[white!25]  (#1) rectangle +(#2,#3);
    \fill[fill=white]
    (#1)
    \foreach \dir in {#4}{
        \ifnum\dir=0
        -- ++(1,0)
        \else
        -- ++(0,1)
        \fi
    } |- (#1);
    \draw[help lines] (#1) grid +(#2,#3);
    \draw[dashed] (#1) -- +(#3,#3);
    \coordinate (prev) at (#1);
    \foreach \dir in {#4}{
        \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}
    \NEpath{0,0}{6}{6}{1,1,0,1,1,0,0,0,1,0,1,0};
    \end{tikzpicture}
\end{document}

which produces the following picture.

enter image description here

Whereas I would like to obtain something like

enter image description here

Is it be possible to modify my existing code to obtain what I desire? If not, is there an alternative approach?

  • How about adding those paths manually? – user156344 Apr 19 at 14:41
  • Btw, as it is an arXiv document, you may find the source code of the file, thus you can find the code for the picture – user156344 Apr 19 at 14:42
  • @JouleV I am unfortunately not very good with tikz - what do you mean by adding the paths manually? Also, the image in the pdf is not done in tikz but rather attached as a pdf. – Joakim Uhlin Apr 19 at 14:59
  • 1
    What I mean is that you can add the lines "by hand" inside the tikzpicture – user156344 Apr 19 at 15:02
4

Just for fun, something that adds the numbers and laser lines automatically. The laser lines are drawn automatically according to your clarified prescription. The strategy is to look at the elements of the list that are ahead and check whether or not they fulfill certain criteria. The integer \vtest tests if the current point is a "valley", in which case it is 10, and the other integers are constructed similarly.

\documentclass[tikz,border=3.14mm]{standalone}
\newcounter{DyckHsteps}
\begin{document}
\tikzset{count list/.code 2 args={\foreach \XX [count=\YY] in {#1}
{\xdef#2{\YY}}},Dyck arrow/.style={ultra thick,-stealth},
laser/.style={draw=blue},
Dyck path/.style={count list={#1}{\DyckSteps},
/utils/exec=\setcounter{DyckHsteps}{0},insert path={%
foreach \XX [count=\YY,remember=\YY as \LastY (initially 0)]in {#1}
{\ifnum\XX=0
edge[Dyck arrow] ++(1,0) ++(1,0) coordinate(Dyck-\YY)
\ifnum\YY<\DyckSteps
(Dyck-\LastY) -- (Dyck-\YY) node[midway,above]{\stepcounter{DyckHsteps}\number\value{DyckHsteps}}
\fi
\else
edge[Dyck arrow] ++(0,1) ++(0,1) coordinate(Dyck-\YY)
\fi
\pgfextra{\pgfmathtruncatemacro{\vtest}{0}\pgfmathtruncatemacro{\ftest}{0}\pgfmathtruncatemacro{\htest}{0}\pgfmathtruncatemacro{\itest}{1}
\pgfmathtruncatemacro{\RestSteps}{\DyckSteps-\YY}
\ifnum\YY>1
 \ifnum\RestSteps>1
 \pgfmathtruncatemacro{\ftest}{{#1}[\YY+1]+{#1}[\YY]*10} % should be 10
 \pgfmathtruncatemacro{\vtest}{{#1}[\YY-1]+10*{#1}[\YY]} % valley test
 \fi
 \ifnum\RestSteps>3
 \pgfmathtruncatemacro{\htest}{pow(-1,{#1}[\YY+3])+pow(-1,{#1}[\YY+2])
 +pow(-1,{#1}[\YY+1])+pow(-1,{#1}[\YY])+ifthenelse({#1}[\YY-1]==1,11,0))}
 \fi
 \ifnum\RestSteps>5
 \pgfmathtruncatemacro{\itest}{pow(-1,{#1}[\YY+5])+
 pow(-1,{#1}[\YY+4])+pow(-1,{#1}[\YY+3])+pow(-1,{#1}[\YY+2])
 +pow(-1,{#1}[\YY+1])+pow(-1,{#1}[\YY])+ifthenelse({#1}[\YY-1]==1,11,0)
 +ifthenelse({#1}[\YY-2]==1,11,0)}
 \fi
\fi%\typeout{\YY:\RestSteps:\ftest,\htest,\itest,\vtest}
}
\ifnum\vtest=10 
 %(Dyck-\YY) node[blue,fill,circle,inner sep=2pt]{}(Dyck-\YY)
 \ifnum\itest=0
  (Dyck-\YY) edge[laser] ++(3,3) (Dyck-\YY)
 \fi
 \ifnum\htest=1100
  (Dyck-\YY) edge[laser] ++(-2,-2) (Dyck-\YY)
 \fi
 \ifnum\ftest=10
  (Dyck-\YY) edge[laser] ++(1,1) (Dyck-\YY)
 \fi
\fi 
}}}}
\begin{tikzpicture}
 \draw (0,0) grid (6,6);
 \draw (0,0) [Dyck path={1,1,0,1,0,0,1,1,0,1,0,0}];
\end{tikzpicture}
\end{document}

enter image description here

  • Nice! But (at least in my case) the lasers should only go from the valleys and up-right. I imagine one could modify the code to get this tho. – Joakim Uhlin Apr 19 at 17:06
  • @JoakimUhlin They go right-up, don't they? – marmot Apr 19 at 17:08
  • True but they also go down-left in your case. You can compare this to my picture. For example, there should not be a laser between (0,0) and (3,3) but there should be one between (3,3) and (6,6). – Joakim Uhlin Apr 19 at 17:12
  • @JoakimUhlin Hmm, sorry, I do not understand these prescriptions. Whether it is up-right or down-left is only a matter of perspective, isn't it? From the point (0,0) you can go up-right to (3,3), or you can go from (3,3) down-left to (0,0), the result looks identical to me. – marmot Apr 19 at 17:16
  • 1
    @JoakimUhlin I updated the answer accordingly. – marmot Apr 19 at 18:16
5

With the help from JouleV, I managed to do solve this but I am leaving this as an answer for people who might be interested.

\documentclass{article}
\usepackage{tikz}

\newcommand{\NEpath}[4]{
    \fill[white!25]  (#1) rectangle +(#2,#3);
    \fill[fill=white]
    (#1)
    \foreach \dir in {#4}{
        \ifnum\dir=0
        -- ++(1,0)
        \else
        -- ++(0,1)
        \fi
    } |- (#1);
    \draw[help lines] (#1) grid +(#2,#3);
    \draw[dashed] (#1) -- +(#3,#3);
    \coordinate (prev) at (#1);
    \foreach \dir in {#4}{
        \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}
    \NEpath{0,0}{6}{6}{1,1,0,1,0,0,1,1,0,1,0,0};
    \draw (1,2) -- +(1,1);
    \draw (3,3.1) -- +(2.9,2.9);
    \draw (4,5) -- +(1,1);
    \node at (0.5, 2.5)   {1};
    \node at (1.5, 3.5)   {2};
    \node at (2.5, 3.5)   {3};
    \node at (3.5, 5.5)   {4};
    \node at (4.5, 6.5)   {5};
    \end{tikzpicture}
\end{document}

This produces the following the picture

enter image description here

Perhaps not the most pretty solution and quite mechanical but it works for my purposes. However, It would still be interesting to have a more general solution so that I would not need to draw lines and numbers manually.

  • 1
    I think this is going to be the most general solution unless someone comes up with a tikz package for drawing your specific kind of graph/path. – nomen Apr 19 at 22:22
4

First, congratulations for figuring out everything by yourself. I upvoted your answer.

This answer is a slight improvement of your answer in the position of the nodes (the numbers). I use option above to have a better space between the number and the line below it.

\documentclass{article}
\usepackage{tikz}

\newcommand{\NEpath}[4]{
    \fill[white!25]  (#1) rectangle +(#2,#3);
    \fill[fill=white]
    (#1)
    \foreach \dir in {#4}{
        \ifnum\dir=0
        -- ++(1,0)
        \else
        -- ++(0,1)
        \fi
    } |- (#1);
    \draw[help lines] (#1) grid +(#2,#3);
    \draw[dashed] (#1) -- +(#3,#3);
    \coordinate (prev) at (#1);
    \foreach \dir in {#4}{
        \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}
    \NEpath{0,0}{6}{6}{1,1,0,1,0,0,1,1,0,1,0,0};
    \draw (1,2) -- +(1,1);
    \draw (3,3.1) -- +(2.9,2.9);
    \draw (4,5) -- +(1,1);
    \node[above=2pt] at (0.5, 2)   {1};
    \node[above=2pt] at (1.5, 3)   {2};
    \node[above=2pt] at (2.5, 3)   {3};
    \node[above=2pt] at (3.5, 5)   {4};
    \node[above=2pt] at (4.5, 6)   {5};
    \end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.