Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm working with TikZ and am basically drawing a tree, but my needs require that the nodes be precisely positioned on the coordinate space and precisely sized. For this purpose, I've found that using the \draw command allows me the finest control. I want, however, each node to be labeled along the edge originating from its parent (or, in the case of the root, just a label to the right).

From some other helpful answers here, I've gotten to the following MWE, which is pretty close to what I want:

\documentclass[letterpaper]{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
    \usetikzlibrary{decorations.markings}

\begin{document}

\begin{tikzpicture}[x=10mm,y=10mm,
    name/.style={%
    postaction={ decorate,transform shape,
    decoration={ markings, mark=at position 1.0 with \node #1;}}}]

% Set styles for tree nodes
\tikzstyle{gen0} = [circle, minimum width=16pt, fill, inner sep=0pt]
\tikzstyle{gen1} = [circle, minimum width=12pt, fill, inner sep=0pt]

% ==============================

\draw[black!50] (0,0) grid (8,8);

\draw[name={[right]{nodeA}}]        (3,8)           node[gen0]{};

\draw[name={[above left]{nodeB}}]   (3,8) -- (0,4)  node[gen1]{};
\draw[name={[above left]{nodeC}}]   (3,8) -- (7,2)  node[gen1]{};

% ==============================

\end{tikzpicture}

\end{document}

The actual output looks like this:

Compiled MWE

My goal is to get something like this mock-up I put together in Photoshop:

Desired Output Mock-up

In particular, I want the labels to appear a fixed distance from the edge of each node, not from the center, so they don't get covered up. I would also like the ability to choose on which side of the path the label appears, so none of them appear upsidedown.

In addition, if there is a better way to accomplish this precisely-positioned tree structure more generally with \node and \path commands instead of individual \draw commands, that would be immensely helpful. My actual use-case will have a few dozen nodes among several generations with absolute coordinates to several decimal places, so I'd rather not have to keep re-listing the coordinates of the parent to draw each edge.

share|improve this question
add comment

1 Answer 1

up vote 7 down vote accepted

I would suggest you separate the placement of the circle nodes from the lines and labels. This has the advantage that TikZ then knows that the lines start at the edge of the nodes, which makes the label placement much easier:

\documentclass[letterpaper]{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
    \usetikzlibrary{decorations.markings}

\begin{document}

\begin{tikzpicture}[x=10mm,y=10mm]

% Set styles for tree nodes
\tikzstyle{gen0} = [circle, minimum width=16pt, fill, inner sep=0pt]
\tikzstyle{gen1} = [circle, minimum width=12pt, fill, inner sep=0pt]

% ==============================

\draw[black!50] (0,0) grid (8,8);

\node [gen0, label=right:nodeA] (A) at (3,8) {};
\node [gen1] (B) at (0,4) {};
\node [gen1] (C) at (7,2) {};

\draw (B) -- (A) node [pos=0, sloped, anchor=south west, inner sep=1pt] {nodeB};
\draw (A) -- (C) node [pos=1, sloped, anchor=south east, inner sep=1pt] {nodeC};

% ==============================

\end{tikzpicture}

\end{document}
share|improve this answer
    
Great! An elegant solution! I take it the sloped and inner sep options in the individual \draw commands can be applied generally to every label? –  Tim Parenti Jan 21 '13 at 6:40
    
@TimParenti: Yes, indeed they can (and probably should). –  Jake Jan 21 '13 at 6:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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