How to add label AFTER drawing a path

One possibility is to use calc to determine the middle between nodes. You could also use \path together with, say, 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}

enter image description here


Another option is to use coordinate during creation:

\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}

enter image description here

Tags:

Tikz Pgf