我正在 tikz 中绘制一棵树(类似于二叉搜索树)。我使用 for 循环来创建节点和边。创建它们之后,我想为某些节点和边添加描述。对于节点,我没有问题,因为我可以引用它们。对于边,我该如何引用特定的边,以便我可以在其旁边放置标签,而不必重新绘制该边。
\documentclass{minimal}
\usepackage{tikz}
\tikzset{>=stealth}
\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,fill=black,minimum size=15}]
\pgfmathsetmacro{\scale}{1.5} % Scale of distance between nodes
\pgfmathtruncatemacro{\maxDepth}{4} % max depth
\pgfmathtruncatemacro{\maxDepthLessOne}{\maxDepth-1} % max depth - 1
\pgfmathtruncatemacro{\maxDepthLessTwo}{\maxDepth-2} % max depth - 2
\pgfmathtruncatemacro{\halfway}{\maxDepthLessOne/2} % floor((max depth - 1)/2)
\pgfmathtruncatemacro{\otherhalfway}{\maxDepthLessOne-\halfway} % ceil((max depth - 1)/2)
% create nodes
\foreach \curDepth in {0,...,\maxDepthLessOne} % current depth
\foreach \pos in {0,...,\curDepth} % horizontal
{
\pgfmathsetmacro{\verPos}{1-\curDepth}
\pgfmathsetmacro{\horPos}{\pos-\curDepth/2}
\node [darkstyle] (node\curDepth\pos) at (\scale*\horPos,\scale*\verPos) {};
}
% create edges
\foreach \curDepth in {0,...,\maxDepthLessTwo} % current depth
\foreach \pos in {0,...,\curDepth} % horizontal
{
\pgfmathtruncatemacro{\nextDepth}{\curDepth+1}
\pgfmathtruncatemacro{\nextPos}{\pos+1}
\draw [->,blue,ultra thick] (node\curDepth\pos)--(node\nextDepth\pos);
\draw [->,red,ultra thick] (node\curDepth\pos)--(node\nextDepth\nextPos);
}
\node [left=8] at (node20) {testt}; %testt
\end{tikzpicture}
\end{document}
答案1
另一种选择是在创建期间使用坐标:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{>=stealth}
\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,fill=black,minimum size=15}]
\pgfmathsetmacro{\scale}{1.5} % Scale of distance between nodes
\pgfmathtruncatemacro{\maxDepth}{4} % max depth
\pgfmathtruncatemacro{\maxDepthLessOne}{\maxDepth-1} % max depth - 1
\pgfmathtruncatemacro{\maxDepthLessTwo}{\maxDepth-2} % max depth - 2
\pgfmathtruncatemacro{\halfway}{\maxDepthLessOne/2} % floor((max depth - 1)/2)
\pgfmathtruncatemacro{\otherhalfway}{\maxDepthLessOne-\halfway} % ceil((max depth - 1)/2)
% create nodes
\foreach \curDepth in {0,...,\maxDepthLessOne} % current depth
\foreach \pos in {0,...,\curDepth} % horizontal
{
\pgfmathsetmacro{\verPos}{1-\curDepth}
\pgfmathsetmacro{\horPos}{\pos-\curDepth/2}
\node [darkstyle] (node\curDepth\pos) at (\scale*\horPos,\scale*\verPos) {};
}
% create edges
\foreach \curDepth in {0,...,\maxDepthLessTwo} % current depth
\foreach \pos in {0,...,\curDepth} % horizontal
{
\pgfmathtruncatemacro{\nextDepth}{\curDepth+1}
\pgfmathtruncatemacro{\nextPos}{\pos+1}
\draw [->,blue,ultra thick] (node\curDepth\pos)--(node\nextDepth\pos)coordinate[midway] (ArrowA\curDepth_\pos);
\draw [->,red,ultra thick] (node\curDepth\pos)--(node\nextDepth\nextPos) coordinate[midway] (ArrowB\curDepth_\pos);
}
\node[left] at (node20) {testt}; %testt
\node [left={of ArrowB1_0}] {\begin{minipage}{1.2cm}Text Added!\end{minipage}};
\end{tikzpicture}
\end{document}
答案2
一种可能性是使用 来calc
确定节点之间的中间点。您也可以\path
一起使用,例如midway
。
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{>=stealth}
\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,fill=black,minimum size=15}]
\pgfmathsetmacro{\scale}{1.5} % Scale of distance between nodes
\pgfmathtruncatemacro{\maxDepth}{4} % max depth
\pgfmathtruncatemacro{\maxDepthLessOne}{\maxDepth-1} % max depth - 1
\pgfmathtruncatemacro{\maxDepthLessTwo}{\maxDepth-2} % max depth - 2
\pgfmathtruncatemacro{\halfway}{\maxDepthLessOne/2} % floor((max depth - 1)/2)
\pgfmathtruncatemacro{\otherhalfway}{\maxDepthLessOne-\halfway} % ceil((max depth - 1)/2)
% create nodes
\foreach \curDepth in {0,...,\maxDepthLessOne} % current depth
\foreach \pos in {0,...,\curDepth} % horizontal
{
\pgfmathsetmacro{\verPos}{1-\curDepth}
\pgfmathsetmacro{\horPos}{\pos-\curDepth/2}
\node [darkstyle] (node\curDepth\pos) at (\scale*\horPos,\scale*\verPos) {};
}
% create edges
\foreach \curDepth in {0,...,\maxDepthLessTwo} % current depth
\foreach \pos in {0,...,\curDepth} % horizontal
{
\pgfmathtruncatemacro{\nextDepth}{\curDepth+1}
\pgfmathtruncatemacro{\nextPos}{\pos+1}
\draw [->,blue,ultra thick] (node\curDepth\pos)--(node\nextDepth\pos);
\draw [->,red,ultra thick] (node\curDepth\pos)--(node\nextDepth\nextPos);
}
\node [left=8] at (node20) {testt}; %testt
\node [left=5] at ($(node20)!0.5!(node10)$) {text}; %testt
\path (node11) -- (node22) node[midway,right=5] {text}; %testt
\end{tikzpicture}
\end{document}
答案3
我发现我们可以使用一些常用的 TikZ 命令自然地做到这一点。使用使图形变窄。下面的代码中xscale=.75
没有。我们可以使用来缩短代码。edge
foreach
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[%xscale=.75,
n/.style={circle,fill,minimum size=5mm},
ar/.style={line width=2pt,-stealth}]
\def\a{2}
\path
(0,0) node[n] (1) {}
++(-120:\a) node[n] (2a) {}
+(0:\a) node[n] (2b) {}
++(-120:\a) node[n] (3a) {}
+(0:\a) node[n] (3b) {}
+(0:2*\a) node[n] (3c) {}
++(-120:\a) node[n] (4a) {}
+(0:\a) node[n] (4b) {}
+(0:2*\a) node[n] (4c) {}
+(0:3*\a) node[n] (4d) {};
\draw[ar,blue] (1)--(2a);
\draw[ar,blue] (2a)--(3a);
\draw[ar,blue] (2b)--(3b);
\draw[ar,blue] (3a)--(4a);
\draw[ar,blue] (3b)--(4b);
\draw[ar,blue] (3c)--(4c);
\draw[ar,red] (1)--(2b);
\draw[ar,red] (2a)--(3b);
\draw[ar,red] (2b)--(3c);
\draw[ar,red] (3a)--(4b);
\draw[ar,red] (3b)--(4c);
\draw[ar,red] (3c)--(4d);
\draw[ar,blue] (2a)--(3a)
node[left=2mm,black]{The left of 3rd row!}
node[pos=.3,align=right,red,left=2mm]{YOU WANT\\TEXT HERE};
\end{tikzpicture}
\end{document}