3

I'm using tikz to plot Graphs, each edge shall have an edge-label. This edge label should be next to the center of the edge.

I put an edge node at every edge. There should be a small orthogonal distance from the label to the edge's center. So starting from the edge's center, go a specified distance along the orthogonal direction of the edge and put the label there.

Till now I got that which does not work fine.

\documentclass[12pt,a4paper,oneside,listof=totoc,bibliography=totoc,BCOR=4mm,DIV=12,]{scrartcl}

  \usepackage{tikz}
  \usepackage{pgfplots}

\begin{document}

  \begin{tikzpicture} [scale=0.75]  
    \begin{scope}[every node/.style={circle,draw,fill=white}]
      \node (5) at (0.5,4.5) {1};
      \node (6) at (3.5,4.5) {3};
      \node (7) at (1.5,6) {3};
      \node (8) at (4.5,6) {1};
    \end{scope}      
    \begin{scope}[>=latex,
                    every node/.style={midway},
                   every edge/.style={draw=black,thick}]
      \path [-] (5) edge[right] node {\footnotesize $1$} (6);
      \path [-] (6) edge[right] node {\footnotesize $1$} (7);
      \path [-] (6) edge[right] node {\footnotesize $1$} (8);
  \end{scope}
 \end{tikzpicture}

\end{document}
1

How about using the quotes library instead of edge nodes? Then you can specify the edge node within the edge itself by edge["1"]. The position of the edge node is then controlled by the key auto which can take left or right.

The meaning of left and right to the auto key is that the node will be placed on the left of the path respecting ist direction (e.g. with auto=left if the path is going from the left (0,0) to right (1,0) the node will be placed on the path midway and above).

MWE

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{quotes}

\begin{document}
  \begin{tikzpicture} [scale=0.75]  
    \begin{scope}[every node/.style={circle,draw,fill=white}]
      \node (5) at (0.5,4.5) {1};
      \node (6) at (3.5,4.5) {3};
      \node (7) at (1.5,6) {3};
      \node (8) at (4.5,6) {1};
    \end{scope}      
    \begin{scope}[
        >=latex,
        every node/.style={font=\footnotesize},
        every edge/.style={auto=right, draw=black,thick}]
      \path [-] (6) edge["1"] (5);
      \path [-] (6) edge["1"] (7);
      \path [-] (6) edge["1"] (8);
  \end{scope}
 \end{tikzpicture}
\end{document}

enter image description here

  • definitions >=latex and [-] are surplus :) – Zarko Jan 20 '17 at 8:53
  • I know... I struggled with leaving it or taking it out, but I thought maybe the OP minimized the example and in the actual drawing it's needed. So I left it there to facilitate copy-pasting, though I really do not like it... :P – Guilherme Zanotelli Jan 20 '17 at 8:55
1

An alternative to nice answer of Guilherme Zanotelli (with less consideration of OP MWE):

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{quotes}

\begin{document}
    \begin{tikzpicture}[
every edge quotes/.style = {auto, inner sep=1pt, font=\footnotesize},
                    ]
    \begin{scope}[every node/.style = {circle,draw}]
      \node (5) at (0.5,4.5)    {1};
      \node (6) at (3.5,4.5)    {3};
      \node (7) at (1.5,6)      {3};
      \node (8) at (4.5,6)      {1};
    \end{scope}
\draw[thick]    (5) to ["$1$"] (6)
                (7) to ["$1$"] (6)
                (6) to ["$1$"] (8);
    \end{tikzpicture}
\end{document}

enter image description here

  • +1 mostly for openly disregarding the OP's MWE! ;D – Guilherme Zanotelli Jan 20 '17 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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