37

I have the following graph, where I want to label the edges. Is there a way I can integrate this in to the draw command?

{
\begin{figure}
\begin{center}
\begin{tikzpicture}[every node/.style={circle, draw, scale=.8, fill=gray!50}, scale=1.0, rotate = 180, xscale = -1]

\node (1) at ( 2.55, 3.0) {1};
\node (2) at ( 3.87, 0.92) {2};
\node (3) at ( 6.85, 0.92) {3};
\node (4) at ( 8.17, 3.0) {4};
\node (5) at ( 6.85, 5.0) {5};
\node (6) at ( 3.87, 5.0) {6};
\node (7) at ( 10.2, 3.0) {7};
\node (8) at ( 12.2, 3.0) {8};

\draw (2) -- (1);
\draw (3) -- (2);
\draw (4) -- (3);
\draw (7) -- (4);
\draw (8) -- (7);
\draw (6) -- (1);
\draw (5) -- (6);
\draw (4) -- (5);

\end{tikzpicture}
\caption{Weight 2}
\label{fig:wt2}
\end{center}
\end{figure}
}
Nikhil
  • 2,375
  • 5
  • 19
  • 14
  • There are several examples on the site; for instance, see Package for drawing RDF graphs – Claudio Fiandrino Apr 10 '14 at 09:50
  • This may be more to the point: http://tex.stackexchange.com/questions/96846/how-to-place-label-in-middle-of-line-above-and-below-with-tikz – Torbjørn T. Apr 10 '14 at 10:03
  • 1
    This isn't at all a duplicate. It may be that the answer appears somewhere in the answer to the overly broad linked question, but I came here with this specific question of how to change edge label positions, and for that purpose the linked "duplicate" is about as useful as any other overly complex example, which is to say, highly suboptimal. I've flagged for mod attention - no idea if that will result in it being reopened. – N. Virgo Mar 28 '18 at 13:16
  • On the other hand, it seems it is a duplicate of this question: https://tex.stackexchange.com/questions/159461/tikz-edge-label-position. Linking to that question instead would be vastly more useful to future visitors coming in from Google searches. – N. Virgo Mar 28 '18 at 13:21

2 Answers2

54

you can add a new node to your draw command to create a label:

\draw (Point1) -- (Point2) node [<position>, fill=white] {Label Text};

The position Parameter can be one of:

  • midway
  • near end
  • near start

Also you can set the label above or below the connector created by the draw command. Herefore you can use

  • above
  • above=10pt
  • below
  • below=10pt
  • ...

If you want to create many labels in your drawing i recommend to create a label style you assign to each label node.

EDIT - Some more information:

If you want to play around with labels and their positions and placing them also on curved lines, use the following code:

\documentclass[border=6mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x [count=\i] in {midway, at start, near start, very near start, at end, near end, very near end} {
  \draw (0,-\i) .. controls ++(1,1) and ++(-1,1) .. ++(4,0) node [\x, sloped, fill=white] () {\x};
}
\end{tikzpicture}
\end{document}

The information can also be found in the pgfmanual on page 195

moospit
  • 4,456
7

After eight years ...

  • For labeling of edges I would use quotes library. Using it code for including labels is shorter as at use of nodes and by this less prone on errors.
  • In drawing of pictures seems (at least to me) is handy to use relative coordinates for vertex positioning:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, 
                quotes}

\begin{document} \begin{tikzpicture}[ node distance = 15mm and 15mm, V/.style = {circle, draw, fill=gray!30}, every edge quotes/.style = {auto, font=\footnotesize, sloped} ] \begin{scope}[nodes=V] \node (1) {1}; \node (2) [above right=of 1] {2}; \node (3) [right=of 2] {3}; \node (4) [below right=of 3] {4}; \node (5) [below left=of 4] {5}; \node (6) [left= of 5] {6}; \node (7) [right=of 4] {7}; \node (8) [right=of 7] {8}; \end{scope} \draw (1) edge["A"] (2) (2) edge["B"] (3) (3) edge["C"] (4) (4) edge["D"] (5) (5) edge["E"] (6) (6) edge["F"] (1) %
(4) edge["G"] (7) (7) edge["H"] (8); \end{tikzpicture} \end{document}

enter image description here

  • Instead use letters for edge labels you can use any other text or math.
  • Edge labels (quotes) you cn determine globally to be on left side of edge direction by auto or on the right sida by option `auto=right.
  • Globally settings of quotes position can be changed locally by adding to edge option a swap or shorted '.
  • If you not like that label text is aligned with edge, than you remove option sloped from edge quotes style.
Zarko
  • 296,517
  • 1
    If you get errors like Package pgfkeys: I do not know the key '/tikz/"A"' and I am going to ignore it. Perhaps you misspelled it., try \usetikzlibrary{babel}. Babel seems to redefine quotes like ", so without \usetikzlibrary{babel}, tikz is unable to recognize them. In some cases you may not be aware that you're using babel, it may be loaded by the documentclass you're using, like it was in my case. – jkhsjdhjs Dec 16 '22 at 13:41
  • 1
    @jkhsjdhjs, from OP question is not seems that (s)he use some other language than English, where is no problem with babel. Some other languages, as are Spanissn my native language etc, babel redefine some symbols as is ". In such cases help to load TikZ library babel and at loading babel package sometimes is neede to add some option, for example \usepackage[brazil,shorthands=off]{babel}. – Zarko Jan 16 '23 at 22:19
  • 1
    I agree. I wasn't criticizing your answer, just wanted to add this info since there probably are a lot of users who use babel together with tikz. I wrote this comment so these users are aware of this and know how to deal with it. – jkhsjdhjs Jan 18 '23 at 00:37
  • Is it possible to have labeling in 30 degree arrows as straight? like A looks straight like like B.. – alper Aug 09 '23 at 14:11
  • 1
    @alper, of course. Just remove sloped option from every edge quotes style. – Zarko Aug 09 '23 at 14:35