0

I am trying to get a hierarchical tree built using the forest package where each node is labelled with the parent nodes label + the node index, e.g,: enter image description here

I've tried a lot of different things, including various node walks. There are two issues: 1) labels appear directly above nodes (e.g., right on the edge line) 2) I can't get this recursive labelling working at all - the best I can do is make each nodes label "parentIndex.nodeIndex"

  • 1
    You expect us to retype your tree from the image just in order to create a style which labels the nodes? Only the last bit is interesting. The rest you've already done, so it's old hat. – cfr Feb 3 '17 at 2:31
4

I think I ought not answer do-it-for-mes like this one, especially ones asked by those clearly capable of posting code. I'm going to regardless because I like trees.

I provide answers to do-it-for-mes on a strictly as-is basis. I answer these questions not just because I want to but only, solely and purely for that reason. Expect me to be less than sympathetic to requests for explanations or adjustments. I spent the time setting the problem up. If you'd wanted me to explain, you'd have helped me to help you.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
  forked edges,
  for tree={
    align=center,
    draw,
    rounded corners,
    l sep'+=15pt,
  },
  my label/.style={
    label={[anchor=south east]above:#1},
  },
  before typesetting nodes={
    for descendants={
      temptoksa/.option=n,
      for nodewalk={
        while={>On>{level}{1}}{u,+temptoksa=.,+temptoksa/.option=n}
      }{},
      my label/.register=temptoksa,
    },
  }
  [Things it is time to speak of \dots
    [Shoes
      [An invention designed to\\protect the wealthy and\\trample the poor.]
    ]
    [Ships
      [Occasionally to be\\found in bottles.]
    ]
    [Sealing was
      [Not to be confused\\with ceiling wax.]
    ]
    [Cabbages
      [{A traditional medicine for\\poverty, everywhere believed\\efficacious by the rich\\and known ineffective\\by the poor.}]
    ]
    [Kings
      [Hereditary obsolescence.]
    ]
    [Why the sea is boiling hot
      [\emph{Non sequitur}.]
    ]
    [Whether pigs have wings
      [Falsehood.
        [\emph{A posteriori.}]
        [Contingent.]
      ]
    ]
  ]
\end{forest}
\end{document}

auto-numbering

2

Since you not provide code for your tree, I use some my tree:

\documentclass[border=3mm]{standalone}
\usepackage[edges]{forest}

\begin{document}
\tikzset{every label/.style={xshift=-4ex, text width=6ex, align=right, 
                             inner sep=1pt, font=\footnotesize, text=red}}
  \begin{forest}
    for tree={              % style of tree nodes
      font=\footnotesize,
      draw, semithick, rounded corners,
            align = center,
        inner sep = 2mm,
                            % style of tree (edges, distances, direction)
             edge = {draw, semithick, -stealth},
    parent anchor = east,
     child anchor = west,
             grow = south,
    forked edge,            % for forked edge
            l sep = 12mm,   % level distance
         fork sep = 6mm,    % distance from parent to branching point
                }
    [Multimedia\\ RSS,label=1
      [Multimedia\\ Content,label=1.1]
      [Multimedia\\ Content,label=1.2.3.4]
    ]
  \end{forest}
\end{document}

enter image description here

  • Thanks, I actually wanted something where the numbers would be recursively appended, e.g., I wouldn't have to number them - I did figure something out but it's quite messy, I'll post it as an answer – Zack Newsham Feb 3 '17 at 3:23
  • @ZackNewsham And we actually wanted something like some code and not just a do-this-whole-thing-for-me picture. – cfr Feb 3 '17 at 4:09

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.