5

I'm drawing parse trees for formal grammar using the forest package and would like to have all terminals, that are not Epsilon, to be copied to the bottom and connected to the original node with a dotted line. By reading through the forest documentation I've got a somewhat working minimal example that is missing two essential things: for one, it doesn't copy the content of the original node and uses 'x' as a placeholder, and it currently isn't able to detect the Epsilon nodes and omit the copying for those.

MWE.tex:

\documentclass[paper=a4, fontsize=11pt]{scrartcl}
\usepackage{forest}

\begin{document}
\begin{forest}
    before typesetting nodes={
        where n children=0{
            append={
                [x,tier=terminal,edge=dotted]
            }
        }{}
    }
    [\(S\)
        [\(S_0\)
            [\(0\)]
            [\(S_0\)
                [\(0\)]
                [\(S_0\)
                    [\(0\)]
                    [\(S_0\)
                        [\(0\)]
                        [\(S_0\)
                            [\(0\)]
                        ]
                    [\(1\)]
                    ]
                [\(1\)]
                ]
            [\(1\)]
            ]
            [\(1\)]
        ]
        [\(S_1\)
            [\(2\)]
            [\(S_1\)
                [\(\epsilon\)]
            ]
        ]
    ]
\end{forest}
\end{document}

MWE.tex produces:

parse tree generated by MWE.tex

Desired result:

desired parse tree

0

2 Answers 2

4

This preamble will do the trick:

before typesetting nodes={
  where n children=0{
    if content={\(\epsilon\)}{}{
      append={
        [x,tier=terminal,edge=dotted,content/.pgfmath=content("!u")]
      }
    }
  }{}
}
0
3

Code originally written for version 1 of forest but compiles fine with version 2.


Just as a supplement to Sašo Živanović's answer, note that if you use math content, you do not need to specify maths mode within every node of the tree:

\documentclass[tikz,border=10pt]{standalone}

\usepackage{forest}

\begin{document}
\begin{forest}
  before typesetting nodes={
    where n children=0{
      if content={\epsilon}{}{
        append={
          [,math content, tier=terminal, edge=dotted, content/.pgfmath=content("!u")]
        }
      }
    }{}
  },
  for tree={
    math content
  }
  [S
        [S_0
            [0]
            [S_0
                [0]
                [S_0
                    [0]
                    [S_0
                        [0]
                        [S_0
                            [0]
                        ]
                    [1]
                    ]
                [1]
                ]
            [1]
            ]
            [1]
        ]
        [S_1
            [2]
            [S_1
                [\epsilon]
            ]
        ]
    ]
\end{forest}
\end{document}

same result with less typing

3
  • Thanks for this addition, I'd really like to incorporate it into my current tree. But somehow the math mode doesn't seem to correctly apply, do you have an idea? Updated MWE is here: pastebin.com/LEMMkK8H
    – Big-Blue
    Commented Nov 27, 2015 at 22:36
  • @Big-Blue I suggest asking a new follow-up question (linking to this one) rather than trying to address a new issue in comments. That's awkward, the system will get mad at us after a couple of comments and it makes it hard for other people to find if they have the same question.
    – cfr
    Commented Nov 27, 2015 at 22:59
  • @Big-Blue That said, the code you linked to compiles just fine for me.
    – cfr
    Commented Nov 27, 2015 at 23:04

You must log in to answer this question.

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