Tikz builds the tree in such a way that the length level distance
is the distance between the point growth parent anchor
and the anchor of the child.
By default, the growth parent anchor
is center
. And the anchor of the children are the default nodes anchors, which are also center
. So you get 1cm between the centers of the nodes.
In order to have 1cm between the edges of the nodes as you request, you have to set growth parent anchor
to south
, and set also the default anchor
for all the nodes of the tree to north
. The following code implements this idea (and adds some blue lines to test if it works).
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[level distance=1cm, growth parent anchor={south}, nodes={anchor=north}]
\path node (a) {a}
child {
node (b) {b}
child {
node [align=center] (cd) {c \\ d}
child {
node (e) {e}
}
}
};
% Testing
\foreach \n in {a,b,cd} {
\draw[blue,<->] (\n.south) ++(2mm,0) -- ++(0, -1cm) node[midway, right]{1cm};
}
\end{tikzpicture}
\end{document}
It works! See the result:
Here, we a have a festival of \foreach
:
\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{%
every neuron/.style={
circle,
draw,
minimum size=1cm
},
neuron missing/.style={
draw=none,
scale=4,
text height=0.333cm,
execute at begin node=\color{black}$\vdots$
},
}
\begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]
\foreach \m/\l [count=\y] in {1,2,3,missing,4}
\node [every neuron/.try, neuron \m/.try] (input-\m) at (0,2.5-\y) {};
\foreach \m [count=\y] in {1,missing,2}
\node [every neuron/.try, neuron \m/.try ] (hidden-\m) at (2,2-\y*1.25) {};
\foreach \m [count=\y] in {1,missing,2}
\node [every neuron/.try, neuron \m/.try ] (output-\m) at (4,1.5-\y) {};
\foreach \l [count=\i] in {1,2,3,n}
\draw [<-] (input-\i) -- ++(-1,0)
node [above, midway] {$I_\l$};
\foreach \l [count=\i] in {1,n}
\node [above] at (hidden-\i.north) {$H_\l$};
\foreach \l [count=\i] in {1,n}
\draw [->] (output-\i) -- ++(1,0)
node [above, midway] {$O_\l$};
\foreach \i in {1,...,4}
\foreach \j in {1,...,2}
\draw [->] (input-\i) -- (hidden-\j);
\foreach \i in {1,...,2}
\foreach \j in {1,...,2}
\draw [->] (hidden-\i) -- (output-\j);
\foreach \l [count=\x from 0] in {Input, Hidden, Ouput}
\node [align=center, above] at (\x*2,2) {\l \\ layer};
\end{tikzpicture}
\end{document}
Although it seems unwise to have n
denote the number of nodes in each layer when they could be different and the arrangement of the diagram suggests they are not.
Best Answer
Second proposition (using two nested loops as suggested by marsupilam)
(Note: My previous proposition did not use the correct definition of degrees. Thanks to cfr and marsupilam for having noticed.)
Here is a
pdflatex
solution. The first node (the center) isc-0-1
. The first level nodes arec-1-1
,c-1-2
andc-1-3
... The fourth level nodes arec-4-1
,c-4-2
, ... andc-4-96
.