12

Consider the following MWE

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
 \draw (0,0)--(1,1) node[auto,midway] {$A$}
 -- (2,0) node[auto,midway] {$B$};
\end{tikzpicture}
\end{document}

I am using auto here to get automatic left/right positioning. However, I am not satisfied with the distance between the edge labels and the edge itself, which is a bit too large (my real example is a picture with much more nodes in it, so there is room for confusion if they are not very close).

Is there a way to alter this distance? I tried adding node distance=something almost everywhere, but it seems to be ignored.

1 Answer 1

21

There's no gap between the nodes and the lines (node distance applies to a different positioning mechanism). You can see this if you activate draw for the nodes

There are two things you can do to move the text closer to the line: Either you decrease the inner sep, for example using inner sep=1pt, or you set the outer sep to a negative value, for example using outer sep=-3pt:

6
  • As a minor tweak, you can consider adding the circle option to the nodes as well, so that the spacing won't depend on the direction of the edge.
    – T. Verron
    Commented Dec 10, 2012 at 16:38
  • @T.Verron: That's what I thought until very recently as well, but it turns out that the auto placement only uses the north, north west, west, etc. anchors to place the nodes. Try putting a circle node on a line from (0,0) to (1,3): The circle will intersect the line. I posted a code snippet at TikZ parallel Edges between Nodes that makes TikZ use degree-accuracy for placing the nodes. Also, ellipse nodes are better for ensuring consistent distance in case your text isn't square.
    – Jake
    Commented Dec 10, 2012 at 16:54
  • @Jake : Interesting, I didn't know that (and it happened to be invisible on this question's MWE). However, I find that if you have to use only one shape and inner sep for all directions, a circle still gives a better output than a rectangle (most notably on vertical edges). Personal taste of course.
    – T. Verron
    Commented Dec 10, 2012 at 17:23
  • @T.Verron: I was thinking of the case of a horizontal edge with a long label, say, \tikz \draw (0,0) -- node [auto] {A long label} (4,0);. If you use a circle there, the label will be moved really far from the edge, while using ellipse would keep it close to the edge.
    – Jake
    Commented Dec 10, 2012 at 17:45
  • @Jake : Hm, and in that case, the default rectangle would even be better. Good point.
    – T. Verron
    Commented Dec 10, 2012 at 19:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .