경로를 그린 후 레이블을 추가하는 방법

경로를 그린 후 레이블을 추가하는 방법

저는 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}

관련 정보