Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\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? ...

share|improve this question
6  
have you tried adding a second node? That would work perfectly. \draw (\from) -- node[below] {a} node[above] {b} (\to); –  zeroth Feb 5 '13 at 11:48
    
Thanks, yes, got it worked out :) Just had to replace \draw (\from) -- (\to) node[draw=none,fill=none,font=\scriptsize,midway,below] {text below}; with \draw (\from) -- (\to) node[midway,above] {text above} node[midway,below] {text below}; –  Faheem Feb 5 '13 at 12:45
add comment

1 Answer 1

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:

\begin{tikzpicture}
  \draw (-2,0) -- node[below] {a} node[above] {b} ++(4,0);
\end{tikzpicture}

which yields:
a REALLY BIG fraction...???

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

\begin{tikzpicture}
  \draw (-2,0) -- node[below] {a} ++(2,0) -- node[above] {b} ++(2,0);
\end{tikzpicture}

which yields:
skewed???, now I am doubting your sincerity!

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 that midway can also be placed during a path:

\begin{tikzpicture}
  \draw (-2,0) -- ++(1,0) -- ++(1,0) node[midway,below] {a}
  node[midway,above] {b} -- ++(2,0);
  \foreach \x in {-2,-1,0,2} 
  \draw (\x,0) circle (3pt);
\end{tikzpicture}

which produces (the circles are added to show the control points of the path):

freaking stop those ugly fractions... :)

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 of node[red].

In your case an example could be:

\tikzset{every node/.style={font=\scriptsize}}
\begin{tikzpicture}[every node/.append style={midway}]
  \draw (-2,0) -- ++(4,0) node[below] {a} node[above] {b};
\end{tikzpicture}

Notice here how I have implemented the every node style in a generalized tikzset call. This makes every node global to ALL tikzpicture environment nodes. And in the tikzpicture I use .append style which does not destroy any previous global options. The above produces:

it doesn't help with smaller font size... Don't do those fractions, or draw some more so it makes sense. ThaaaaAAA!

Notice the \scriptsize of the node text.

share|improve this answer
    
How can I draw something like this in tikz Link? A server and queue –  Faheem Mar 25 '13 at 12:44
    
@Faheem that question has come up several times on this site, and is not directly transferable to your question. –  zeroth Mar 25 '13 at 14:05
    
What does ++ mean? –  moose Nov 26 '13 at 10:52
    
@moose, good question, I would suggest you ask that, it is a very good question! –  zeroth Nov 26 '13 at 11:42
1  
@moose, basically it means add the following coordinate to the previous one. –  zeroth Nov 26 '13 at 21:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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