TransWikia.com

The same left and right labels on every tree node - TikZ

TeX - LaTeX Asked by Hive7 on August 7, 2021

I’m am trying to form a tree where every node has a ‘(‘ before the node and a ‘)’ after the node. I understand that this is achievable through something like this:

documentclass{standalone}

usepackage{tikz}

begin{document}
begin{tikzpicture}[sibling distance=10em,
        every node/.style = {shape=circle, draw, align=center}]]
    node[label={left:(}, label={right:)}] {}
    child { node[label={left:(}, label={right:)}] {} }
    child { node[label={left:(}, label={right:)}] {}
            child { node[label={left:(}, label={right:)}] {}
                    child { node[label={left:(}, label={right:)}] {} }
                    child { node[label={left:(}, label={right:)}] {} }
                    child { node[label={left:(}, label={right:)}] {} } }
            child { node[label={left:(}, label={right:)}] {} } };
end{tikzpicture}
end{document}

Giving the following result:

enter image description here

However, this is not ideal and I would rather not have to copy and paste the same code each time (something something maintainability). I have tried placing the label code within the node style, however that doesn’t seem to be supported. I have tried to find a way to create a custom node to which I can assign labels, but I also have struggled to get something like that working / haven’t been able to find anything on it.

For context, I am trying to display a tree representation of a Dyck word where the pre and post numbers after performing DFS represent the indices of ‘(‘ and ‘)’.

Desired code form:

begin{figure}
    begin{tikzpicture}[sibling distance=10em,
            every node/.style = {shape=circle, draw, align=center}]] % <-- some style here for the ()?
        node {}
        child { node {} }
        child { node {}
                child { node {}
                        child { node {} }
                        child { node {} }
                        child { node {} } }
                child { node {} } };
    end{tikzpicture}
end{figure}

2 Answers

The problem with every node is that this style gets applied to labels, which are nodes as well, thus creating an infinite recursion. Use every child node instead. This style is designed to get only applied to the, well, child nodes but not the labels.

documentclass{standalone}

usepackage{tikz}

begin{document}
begin{tikzpicture}[sibling distance=10em,
        ears/.style={shape=circle, draw, align=center,
            label={left:(}, label={right:)}},
        every child node/.style = {ears}]]
    node[ears] {}
    child { node {} }
    child { node {}
            child { node {}
                    child { node {} }
                    child { node {} }
                    child { node {} } }
            child { node {} } };
end{tikzpicture}
end{document}

enter image description here

Correct answer by user240002 on August 7, 2021

For whatever reason, setting label in the every node/.style does not work, however, creating a new node style does seem to. The following code produces the desired results:

documentclass{standalone}

usepackage{tikz}

tikzset{dyck/.style={
    draw,
    circle,
    label={left:(},
    label={right:)},
    }
}

begin{document}
begin{tikzpicture}[sibling distance=10em,
        every node/.style = {shape=circle, draw, align=center}]]
    node[dyck] {}
    child { node[dyck] {} }
    child { node[dyck] {}
            child { node[dyck] {}
                    child { node[dyck] {} }
                    child { node[dyck] {} }
                    child { node[dyck] {} } }
            child { node[dyck] {} } };
end{tikzpicture}
end{document}

Answered by Hive7 on August 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP