我想创建一个戴克路径在 Latex 中,有两个附加功能。首先,我想给除最后一个之外的所有东台阶编号。其次,对于每个山谷(即东台阶后面跟着北台阶),我想绘制“激光”,这些激光将是与对角线平行的线,一旦到达 Dyck 路径就会停止。这与“激光构造”类似,但不完全相同这纸。参见图6。
我已经有一些代码来获取 Dyck 路径。
\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}
得到下面的图片。
而我想获得类似的东西
是否可以修改现有代码来获得我想要的结果?如果不行,还有其他方法吗?
答案1
只是为了好玩,可以自动添加数字和激光线。激光线会根据您指定的处方自动绘制。策略是查看列表中的元素,并检查它们是否满足某些条件。整数测试当前点\vtest
是否为“谷”,在这种情况下是10
,其他整数的构造方式类似。
\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}
答案2
在 JouleV 的帮助下,我设法解决了这个问题,但我将其留给可能感兴趣的人作为答案。
\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}
这将产生以下图片
也许这不是最漂亮的解决方案,而且相当机械,但它对我的目的有用。然而,如果有一个更通用的解决方案,那么我不需要手动绘制线条和数字,那仍然很有趣。
答案3
首先,恭喜你自己搞清楚了一切。我赞同你的回答。
此答案在节点(数字)的位置上对您的答案进行了轻微改进。我使用选项above
来在数字和其下方的行之间留出更好的空间。
\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}