\fbox{
\begin{tikzpicture}[scale=1,auto=center]
\node[circle,fill=gray!20] (n1) at (2,8) {Node A};
\node[circle,fill=gray!20] (n2) at (8,8) {Node B};
\foreach \from/\to in {n1/n2}
\draw (\from) -- (\to) node[draw=none,fill=none,font=\scriptsize,midway,below] {text below};
\end{tikzpicture}
}
The example show how to put text below the line in middle, but how to write the text below and above the line in middle (simultaneously) between two nodes? …
Best Answer
Ok, so this is basically a question about how and when can you place a
node
.In general you can apply any node as many times and wherever you want on a path.
\draw (-2,0) -- node {a} ++(2,0) -- node {b} ++(2,0);
Also using the optional
midway
can be circumvented by placing the node right after the--
which enables to construct very complex paths and nodes simultaneously (as shown in the above example).The easy solution as given in the comment is:
which yields:

This can also be used for more complex paths and placement of nodes on that path:
which yields:

Notice that the nodes are placed at the middle of the two points connected by
--
. Remark that the node has to be placed after the--
in order to be placed in the middle of the current segment (otherwise it would be placed at the point just preceding the node).However, it is perfectly fine to use
midway
and place the node at the end of the path. One should notice thatmidway
can also be placed during a path:which produces (the circles are added to show the control points of the path):
This is of course useful when creating non-straight lines.
As a remark I would like to point the interest to the
every node
option which can be used to generalize options on the nodes, i.e.every node/.style={red}
will make all nodes the equivalent ofnode[red]
.In your case an example could be:
Notice here how I have implemented the
every node
style in a generalizedtikzset
call. This makesevery node
global to ALLtikzpicture
environment nodes. And in thetikzpicture
I use.append style
which does not destroy any previous global options. The above produces:Notice the
\scriptsize
of the node text.