2

I have one node (or rather, a coordinate) and a couple of nodes which are aligned in one "row" (via right=of, left=of). They are aligned below each other (via below right=of),

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[
    auto,
    node distance = 1em and 2em,
    thick,
    every node/.style = {
      text width = 5em,
      top color = blue!60,
      bottom color = blue!60,
      rectangle,
      font = \sffamily,
      white
    }
  ]
  \coordinate (center);
  \node [above = of center,text width=10em] (T0) {test0};
  \node (T1) [below left = of center] {test1};
  \node (T2) [left = of T1] {test2};
  \node (T3) [right = of T1] {test3};
  \node (T4) [right = of T3] {test4};
\draw [black,thick]
(center) -- (T0)
(center) -| (T1)
(center) -| (T2)
(center) -| (T3)
(center) -| (T4);
\end{tikzpicture}
\end{document}

This gives

align

As you can see, the center (test0) is not properly aligned with the group of nodes below. All boxes are equally wide.

How to better align the nodes?

  • 2
    \node (T1) [below left = 1em and 1em of center] {test1}; \node (T2) [left = of T1] {test2}; \node (T3) [below right = 1em and 1em of center] {test3}; \node (T4) [right = of T3] {test4}; I would recommend the package forest for such graphs. – LaRiFaRi Aug 25 '15 at 15:39
3

With library trees of TikZ package this is very simple to obtain:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}

\usepackage[active,tightpage]{preview}% just for showing image
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%

    \begin{document}
\begin{tikzpicture}[
thick,
every node/.style = {
      text width = 5em,
      top color = blue!60,
      bottom color = blue!60,
      rectangle,
      font = \sffamily,
      text=white
                    },
sibling distance = 7em,
edge from parent fork down                  
  ]
\node[text width=10em] (T0) {test0}
    child {node (T1) {test1}}
    child {node (T2) {test2}}
    child {node (T3) {test3}}
    child {node (T4) {test4}};
\end{tikzpicture}
    \end{document}

Result:

enter image description here

| improve this answer | |
4

Below I present two options using TikZ and one additional version using forest.

First TikZ option

Since to place T1 you used below left=of center, then you can locate T3 symmetrically using below right=of center. Also, to have even separation between the nodes you have to use below left=1em and 1em of center and below right=1em and 1em of center

Second TikZ option

Here I build the diagram from the bottom to the top. I place the nodes on the bottom layer evenly spaced and then use the calc library to find the middle point between the central nodes; I place an auxiliary coordinate and then use this to place the upper node and to draw the lines.

The code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}
\begin{tikzpicture}[
    auto,
    node distance = 1em and 2em,
    thick,
    every node/.style = {
      text width = 5em,
      top color = blue!60,
      bottom color = blue!60,
      rectangle,
      font = \sffamily,
      white
    }
  ]
  \coordinate (center);
  \node [above = of center,text width=10em] (T0) {test0};
  \node (T1) [below left = 1em and 1em of center] {test1};
  \node (T2) [left = of T1] {test2};
  \node (T3) [below right = 1em and 1em  of center] {test3};
  \node (T4) [right = of T3] {test4};
\draw [black,thick]
(center) -- (T0)
(center) -| (T1)
(center) -| (T2)
(center) -| (T3)
(center) -| (T4);
\end{tikzpicture}\par\bigskip

\begin{tikzpicture}[
    auto,
    node distance = 1em and 2em,
    thick,
    every node/.style = {
      text width = 5em,
      top color = blue!60,
      bottom color = blue!60,
      rectangle,
      font = \sffamily,
      white
    }
  ]
  \node (T2) {test2};
  \node (T1) [right= of T2] {test1};
  \node (T3) [right= of T1] {test3};
  \node (T4) [right = of T3] {test4};
  \coordinate (center) at ([yshift=10pt] $ (T1.north)!0.5!(T3.north) $ );
  \node (T0) [above=10pt of center,text width=10em] {test0};
\draw [black,thick]
(center) -- (T0)
(center) -| (T1)
(center) -| (T2)
(center) -| (T3)
(center) -| (T4);
\end{tikzpicture}

\end{document}

The result:

enter image description here

forest option

Since this seems like a tree, then you could use forest and leave the alignment to the package:

\documentclass{article}
\usepackage{forest}

\begin{document}
\begin{forest}
for tree={
text width = 5em,
top color = blue!60,
bottom color = blue!60,
rectangle,
font = \sffamily,
white,
parent anchor=south,
child anchor=north,
  edge path={
    \noexpand\path[\forestoption{edge}]
      (!u.parent anchor) -- +(0,-10pt) -|   
      (.child anchor)\forestoption{edge label};
  },
  l sep=20pt,
}
[test0,text width=10em
  [test2]
  [test1]
  [test3]
  [test4]
]
\end{forest}

\end{document}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.