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 use this code to draw filesystem trees, and also I use node labels to add descriptions.

The question is how to align node labels? I can add descriptions as separate nodes and use relative positioning, but it is tedious.

bad:

node1    description
  node2    description
    node3    description

good:

node1        description
  node2      description
    node3    description

MWE:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
    grow via three points={one child at (0.5,-0.7) and
    two children at (0.5,-0.7) and (0.5,-1.4)},
    edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
    \node [label={right:description}] {texmf}
        child { node [label={right:description}] {doc}}     
        child { node [label={right:description}] {fonts}}
        child { node [label={right:description}] {source}}
        child { node [selected,label={right:description}] {tex}
        child { node [label={right:description}] {generic}}
        child { node [optional,label={right:description}] {latex}}
        child { node [label={right:description}] {plain}}
    }
    child [missing,label={right:description}c] {}               
    child [missing,label={right:description}] {}                
    child [missing,label={right:description}] {}                
    child { node [label={right:description}] {texdoc}};
\end{tikzpicture}
\end{document}
share|improve this question

1 Answer 1

up vote 5 down vote accepted

You can define a new node called descriptionheader, say, and then position the description nodes at the same horizontal position as that new node using shift={(0,0-|descriptionheader.west)} in the label options:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
    grow via three points={one child at (0.5,-0.7) and
    two children at (0.5,-0.7) and (0.5,-1.4)},
    edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)},
    every label/.append style={shift={(0,0-|descriptionheader.west)}}
    ]
    \node (descriptionheader) [draw=none, font=\bfseries] at (4,4ex) {Descriptions};
    \node [label={right:description}] {texmf}
        child { node [label={right:long description}] {doc}}     
        child { node [label={right:description}] {fonts}}
        child { node [label={right:description}] {source}}
        child { node [selected,label={right:description}] {tex}
        child { node [label={right:description}] {generic}}
        child { node [optional,label={right:description}] {latex}}
        child { node [label={right:description}] {plain}}
    }
    child [missing,label={right:description}c] {}               
    child [missing,label={right:description}] {}                
    child [missing,label={right:description}] {}                
    child { node [label={right:description}] {texdoc}};
\end{tikzpicture}
\end{document}
share|improve this answer

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.