5

I started learning TikZ today, and I got to the following image:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs,positioning,arrows,shapes.misc,decorations.pathmorphing}

\tikzset{
    >=stealth,
    every picture/.style={thick},
    graphs/every graph/.style={empty nodes},
}

\tikzstyle{vertex}=[
    draw,
    circle,
    fill=black,
    inner sep=1pt,
    minimum width=5pt,
]

\begin{document}
\begin{tikzpicture}
    \begin{scope}
        \graph[ grow right = 2 ] {
            u [vertex, label=$u$]
            --[edge label=$e$]
            v [vertex, label=$v$]
        };
    \end{scope}
    \draw[->,thin] (3,0) -- (5,0) node [midway,above] {subdividing};
    \begin{scope} [xshift=6cm]
        \graph[ grow right = 1 ] {
            u  [vertex, label=$u$]
            -- %[edge label=$e_{uv}$]
            e  [vertex, label=$v_e$]
            -- %[edge label=$e_{vu}$]
            v  [vertex, label=$v$]
        };
    \end{scope}
\end{tikzpicture}
\end{document}

refinement

Is there a way to automatically vertically align the v_e label with the u and v in the right graph?

Also, I like the graph syntax, but I haven't found an easy way to have it making node labels next to the circle (like in the picture) instead of nodes with the name inside the circle without explicitly using the label option.

  • 2
    You can use $v\smash{_e}$ in place of $v_e$. – Kpym Oct 9 '15 at 21:58
  • @Kpym, thanks! So it turns out to be a math solution, instead of a TikZ solution, but I think \smash (neaver heard of it before) can be used in more general alignment problems. – Ragnar Oct 9 '15 at 22:11
  • You can also use every node/.append style={text depth=0.5ex}, – user11232 Oct 9 '15 at 23:43
5

I am not sure that \smash should be necessary here. It is not needed for the edge labels if anchor is set to mid. However, it does seem to be required for the node label. But perhaps this is due to my unfamiliarity with the graphs library.

You can use the quotes library to make it easier to label edges, but I think you still need label to label nodes.

\documentclass[border=20pt,tikz]{standalone}
\usetikzlibrary{graphs,quotes,positioning}

\tikzset{
  >=stealth,
  every picture/.style={thick},
  graphs/every graph/.style={empty nodes},
  every edge quotes/.append style={above=7.5pt, anchor=mid, inner sep=0pt},
  every label/.append style={above=2.5pt, anchor=mid, inner sep=0pt},
  vertex/.style={
    draw,
    circle,
    fill=black,
    inner sep=0pt,% doesn't actually change the size and makes it easier to calculate things - or, more accurately, it makes it more obvious how to calculate them
    outer sep=0pt,
    minimum size=5pt,
  },
}

\begin{document}
\begin{tikzpicture}
  \graph[ grow right = 2 ] {
    u [vertex, label=$u$]
    -- ["$e$"]
    v [vertex, label=$v$]
  };
  \draw (3,0) edge ["subdividing",->,thin] (5,0);
  \begin{scope} [xshift=6cm]
    \graph[ grow right = 1 ] {
      u  [vertex, label=$u$]
      -- ["$e_{uv}$"]
      e  [vertex, label=$v\smash{_e}$]
      -- ["$e_{vu}$"]
      v  [vertex, label=$v$]
    };
  \end{scope}
\end{tikzpicture}
\end{document}

Note that it does not seem necessary to use \smash for the subscript in $e_{uv}$.

<code>quotes</code> library, <code>anchor=mid</code> and <code>\smash</code> as last resort

  • The quotes library looks useful indeed. I'll look into it tomorrow. Aside, is using grow right = 2 and =1 the right way to make the u to v distance equal in both graphs? – Ragnar Oct 9 '15 at 22:55
  • @Ragnar Sorry, but I don't know. That's a different question and concerns something I know nothing about. I had to look up the manual to follow your syntax ;). – cfr Oct 9 '15 at 22:59
  • Well, no one can know the TikZ manual by heart I guess, looking at those 1000+ pages. – Ragnar Oct 9 '15 at 23:02
  • 1
    @Ragnar Well, actually, I have to look stuff up all the time. But, in this case, I had to discover that there was a bit of the manual on this to look stuff up in, so I was starting from an earlier point in the learning process ;). – cfr Oct 9 '15 at 23:04

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.