[Tex/LaTex] Labels above the nodes

tikz-pgf

Consider the simple graph below,

\begin{tikzpicture}[node distance=2.5cm,main node/.style={circle,fill=white!20,draw}, scale=0.6, every node/.style={scale=0.6}]   

\node[main node] [label={[label distance=1cm]30:label}] (A) {$v_1$};
\node[main node] (B) [right of=A] {$v_2$}; 
\node[main node] (C) [right of=B] {$v_3$};
\node[main node] (D) [right of=C] {$v_4$};

\draw 
(A) -- (C)
(B) -- (C)
(B) -- (D);

\end{tikzpicture}

Is it possible to remove lines going through the nodes (making the inside of the nodes empty) and placing the labels above the nodes?

Best Answer

You're drawing a line from A to C, so that will go over B. If you draw from A to B to C to D you avoid that.

If by 'label' you mean the label you have already added, just change 30 to 90 (or remove 30: altogether, above is the default). If you mean v_i, then add those with a label as well. In the last case you may want to change the minimum size of the nodes.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[node distance=2.5cm,main node/.style={circle,fill=white!20,draw}, scale=0.6, every node/.style={scale=0.6}]   

\node[main node] [label={[label distance=1cm]90:label}] (A) {$v_1$};
\node[main node] (B) [right of=A] {$v_2$}; 
\node[main node] (C) [right of=B] {$v_3$};
\node[main node] (D) [right of=C] {$v_4$};

\draw (A) -- (B) -- (C) -- (D);

\end{tikzpicture}
\begin{tikzpicture}[node distance=2.5cm,main node/.style={minimum size = .7cm,circle,fill=white!20,draw}, scale=0.6, every node/.style={scale=0.6}]   

\node[main node] [label={[label distance=1cm]90:label},label=$v_1$] (A) {};
\node[main node] (B) [right of=A,label=$v_2$] {}; 
\node[main node] (C) [right of=B,label=$v_3$] {};
\node[main node] (D) [right of=C,label=$v_4$] {};

\draw (A) -- (B) -- (C) -- (D);

\end{tikzpicture}
\end{document}

enter image description here