[Tex/LaTex] TikZ node label alignment

labelstikz-pgfvertical alignment

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.

Best Answer

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