업데이트된 솔루션(Forest 버전 2+)

업데이트된 솔루션(Forest 버전 2+)

MWE는 다음과 같습니다.

\documentclass[landscape]{article}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}

\tikzset{
  treenode/.style = {align=center, inner sep=2pt, rounded corners = 2pt, minimum width = 2cm, text centered, font=\sffamily},
  block/.style = {treenode, rectangle, white, font=\sffamily\bfseries, draw=black, fill=black},
  phantom/.style = {}
}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 3in/#1, level distance = 1.5cm}] 
  \node [block] {P1}
    child {node [block] {P2}
      child {node [block] {P3}
        child {node [block] {P4}}
        child {node [block] {P5}}
      }
    }
    child {node [block] {P6}
      child {node [phantom] {}
        child {node [block] {P7}}
      }
    }
    child {node [block] {P8}
      child {node [block] {P9}
        child {node [block] {P10}}
      }
    }
  ;
\end{tikzpicture}

\end{document}

서로 다른 노드 사이의 커넥터는 기울어진 선입니다. 이 위치에 가로-세로선을 표시하려면 어떻게 해야 합니까?

답변1

또 다른 옵션은 를 사용하는 것입니다 forest. 이 경우 phantom노드를 생략하고 를 사용하여 등 을 tier정렬 할 수 있습니다. 또한 현재 권장되는 라이브러리 에서 제공하는 화살표의 현재 구문을 사용하도록 코드를 업데이트했습니다 . 트리를 지정하는 데 훨씬 적은 타이핑 작업이 필요하기 때문에 마음에 듭니다 !P7P4P5arrows.metaforest

업데이트된 솔루션(Forest 버전 2+)

\documentclass[tikz, border=5pt]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}
\begin{document}

\tikzset{
  treenode/.style = {align=center, inner sep=2pt, rounded corners = 2pt, minimum width = 2cm, text centered, font=\sffamily},
  block/.style = {treenode, rectangle, white, font=\sffamily\bfseries, draw=black, fill=black},
}

% Forest version 2.1
\begin{forest}
  for tree={
    block,
    edge+={thick, -{Stealth[]}}
  },
  forked edges
  [P1
    [P2
      [P3
        [P4, tier=terminal]
        [P5]
      ]
    ]
    [P6, calign with current
      [P7, tier=terminal]
    ]
    [P8
      [P9
        [P10]
      ]
    ]
  ]
  ;
\end{forest}
\end{document}

기존 솔루션(Forest 버전 1)

\documentclass[tikz, border=5pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\begin{document}

\tikzset{
  treenode/.style = {align=center, inner sep=2pt, rounded corners = 2pt, minimum width = 2cm, text centered, font=\sffamily},
  block/.style = {treenode, rectangle, white, font=\sffamily\bfseries, draw=black, fill=black},
}

\begin{forest}
  for tree={
    parent anchor=south,
    child anchor=north,
    block,
    edge path={
      \noexpand\path [-{Stealth[]}, \forestoption{edge}, thick]
        (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label};
    },
  }
  [P1
    [P2
      [P3
        [P4, tier=terminal]
        [P5]
      ]
    ]
    [P6, calign with current
      [P7, tier=terminal]
    ]
    [P8
      [P9
        [P10]
      ]
    ]
  ]
  ;
\end{forest}
\end{document}

결과

어떤 솔루션을 사용하든 기본적으로 출력은 동일합니다.

숲속의 나무

답변2

가로/세로라고 하면 이런 뜻인가요?

산출

코드는 다음과 같습니다.

\documentclass[tikz,border=10]{standalone}
\usetikzlibrary{shapes,arrows}
\begin{document}
\tikzset{
  treenode/.style = {align=center, inner sep=2pt, rounded corners = 2pt, minimum width = 2cm, text centered, font=\sffamily},
  block/.style = {treenode, rectangle, white, font=\sffamily\bfseries, draw=black, fill=black},
  phantom/.style = {},
  edge from parent/.style={draw,red,thick},
  edge from parent path={(\tikzparentnode.south)-- ++(0,-3mm) -| (\tikzchildnode.north)}
}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 3in/#1, level distance = 1.5cm}]
  \node [block] {P1}
    child {node [block] {P2}
      child {node [block] {P3}
        child {node [block] {P4}}
        child {node [block] {P5}}
      }
    }
    child {node [block] {P6}
      child {node [phantom] {}
        child {node [block] {P7}}
      }
    }
    child {node [block] {P8}
      child {node [block] {P9}
        child {node [block] {P10}}
      }
    }
  ;
\end{tikzpicture}
\end{document}

편집하다:[edge from parent fork down]방금 라이브러리 의 스타일을 사용하여 trees동일한 효과를 낼 수 있다는 것을 발견했습니다 .

답변3

PSTricks 솔루션:

\documentclass{article}

\usepackage{pstricks}
\usepackage{xfp}

\psset{
  linecolor = red,
  arrows = ->
}

\def\Label(#1,#2)#3{%
  \psframe[
    framearc = 0.3,
    linecolor = black,
    fillstyle = solid,
    fillcolor = black
  ](\fpeval{#1-0.5*\widthBox},\fpeval{#2-0.5*\heightBox})%
   (\fpeval{#1+0.5*\widthBox},\fpeval{#2+0.5*\heightBox})
  \rput(#1,#2){\textcolor{white}{#3}}}

\begin{document}

% parameters
\def\widthBox{1.4}
\def\heightBox{0.4}
\def\horiDist{4.5}
\def\vertDist{1.6}

% drawing
\begin{pspicture}(\fpeval{2*\horiDist+1.75*\widthBox},\fpeval{4*\heightBox+3*\vertDist})
  % left
  \Label(\fpeval{1.25*\widthBox},\fpeval{2.5*\heightBox+2*\vertDist}){P2}
  \psline(\fpeval{1.25*\widthBox},\fpeval{2*\heightBox+2*\vertDist})%
         (\fpeval{1.25*\widthBox},\fpeval{2*\heightBox+\vertDist})
  \Label(\fpeval{1.25*\widthBox},\fpeval{1.5*\heightBox+\vertDist}){P3}
  \psline(\fpeval{1.25*\widthBox},\fpeval{\heightBox+\vertDist})%
         (\fpeval{1.25*\widthBox},\fpeval{\heightBox+0.5*\vertDist})%
         (\fpeval{0.5*\widthBox},\fpeval{\heightBox+0.5*\vertDist})%
         (\fpeval{0.5*\widthBox},\heightBox)
  \psline(\fpeval{1.25*\widthBox},\fpeval{\heightBox+0.5*\vertDist})%
         (\fpeval{2*\widthBox},\fpeval{\heightBox+0.5*\vertDist})%
         (\fpeval{2*\widthBox},\heightBox)
  \Label(\fpeval{0.5*\widthBox},\fpeval{0.5*\heightBox}){P4}
  \Label(\fpeval{2*\widthBox},\fpeval{0.5*\heightBox}){P5}
  % middle
  \Label(\fpeval{\horiDist+1.25*\widthBox},\fpeval{3.5*\heightBox+3*\vertDist}){P1}
  \psline(\fpeval{\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+3*\vertDist})%
         (\fpeval{\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+2*\vertDist})
  \Label(\fpeval{\horiDist+1.25*\widthBox},\fpeval{2.5*\heightBox+2*\vertDist}){P6}
  \psline(\fpeval{\horiDist+1.25*\widthBox},\fpeval{2*\heightBox+2*\vertDist})%
         (\fpeval{\horiDist+1.25*\widthBox},\heightBox)
  \Label(\fpeval{\horiDist+1.25*\widthBox},\fpeval{0.5*\heightBox}){P7}
  \psline(\fpeval{\horiDist+1.25*\widthBox},\fpeval{1.5*\heightBox+\vertDist-0.1})%
         (\fpeval{\horiDist+1.25*\widthBox},\heightBox)
  % right
  \Label(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{2.5*\heightBox+2*\vertDist}){P8}
  \psline(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{2*\heightBox+2*\vertDist})%
         (\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{2*\heightBox+\vertDist})
  \Label(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{1.5*\heightBox+\vertDist}){P9}
  \psline(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{\heightBox+\vertDist})%
         (\fpeval{2*\horiDist+1.25*\widthBox},\heightBox)
  \Label(\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{0.5*\heightBox}){P10}
  % connection
  \psline(\fpeval{\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+2.5*\vertDist})%
         (\fpeval{1.25*\widthBox},\fpeval{3*\heightBox+2.5*\vertDist})%
         (\fpeval{1.25*\widthBox},\fpeval{3*\heightBox+2*\vertDist})
  \psline(\fpeval{\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+2.5*\vertDist})%
         (\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+2.5*\vertDist})%
         (\fpeval{2*\horiDist+1.25*\widthBox},\fpeval{3*\heightBox+2*\vertDist})
\end{pspicture}

\end{document}

산출

이 코드를 사용하면 상자의 너비( \widthBox)와 높이( ), 상자 사이의 가로( ) 및 세로( ) 거리를 선택하기만 하면 됩니다 . 그러면 도면이 그에 따라 자동으로 조정됩니다.\heightBox\horiDist\vertDist

관련 정보