4

I want to number my lowest-level children in a tree with $\omega_i$, where $i$ are the 8 branches of the tree. In the forest documentation I found an example using a TeX counter, but it does not seem to work, as the counter is updated only once.

I understand there should also be a solution where no counter is used, but everything is handled inside forest.

This is where I am:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}

\begin{document}
\tikzset{
  dot/.style={circle,draw,inner sep=1.2,fill=black},
}

\newcount\xcount
\begin{forest}
  for tree={
    if n children=0{}{dot},    
    delay={TeX={\xcount=0},
      if n children=0{fill=white,
        TeX={\advance\xcount1},
        content/.expanded={$\omega_{\the\xcount}$}}{}
    },
  }
  [[
  [[][]]
  [[][]]
  ][
  [[][]]
  [[][]]
  ]]
\end{forest}
\end{document}

current progress

2 Answers 2

5

Your code resets the counter repeatedly, so your increment always increments from 0 to 1. You need to set the counter to zero only on the first level of the tree. (I also changed your TeX counter syntax to LaTeX syntax.)

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}

\begin{document}
\tikzset{
  dot/.style={circle,draw,inner sep=1.2,fill=black},
}

\newcounter{xcount}
\begin{forest}
  for tree={
  delay n=0{TeX={\setcounter{xcount}{0}}}
  delay={
    if n children=0{}{dot},    
      if n children=0{fill=white,
        TeX={\stepcounter{xcount}},
        content/.expanded={$\omega_{\thexcount}$}}{}
    }}
  [[
  [[][]]
  [[][]]
  ][
  [[][]]
  [[][]]
  ]]
\end{forest}
\end{document}

output of code

4
  • I've been missing a rush of Forest questions, it seems. But this one admits two answers, of course ;).
    – cfr
    Commented May 3, 2017 at 0:39
  • @cfr Of course. :)
    – Alan Munn
    Commented May 3, 2017 at 1:16
  • Whereas TeX would obviously score nothing but birdies?
    – cfr
    Commented May 3, 2017 at 1:28
  • You could combine the two if n children=0{}{}s?
    – cfr
    Commented Jul 26 at 1:32
5

Here's a version which doesn't need an external counter.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}
\begin{document}
\forestset{
  declare count register={omega},
  omega'=0,
  dot/.style={circle, draw, inner sep=1.2, fill=black},
}
\newcount\xcount
\begin{forest}
  before typesetting nodes={
    where n children=0{
      omega'+=1,
      content/.expanded=$\omega_{\foresteregister{omega}}$,
    }{dot},
  }
  [[
  [[][]]
  [[][]]
  ][
  [[][]]
  [[][]]
  ]]
\end{forest}
\end{document}

omega tree

4
  • forest will certainly not win any code golf competitions. :)
    – Alan Munn
    Commented May 3, 2017 at 1:21
  • @AlanMunn I have no idea what those are. (I've seen the term, but have no idea what it means.) Hence, I have no idea why Forest wouldn't win and no idea whether Forest should be distressed by that fact, proud of that fact or confused by that fact.
    – cfr
    Commented May 3, 2017 at 1:27
  • :D Code golf is a game in which you try to solve a problem using the fewest number of bytes of code. codegolf.stackexchange.com
    – Alan Munn
    Commented May 3, 2017 at 1:33
  • 1
    @AlanMunn I take it the L3 team aren't thinking of entering, then?
    – cfr
    Commented May 3, 2017 at 2:08

You must log in to answer this question.

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