# Respect the leaf order (chomsky tree alike))

**URL:** https://forum.graphviz.org/t/respect-the-leaf-order-chomsky-tree-alike/1836
**Category:** Help
**Created:** [October 18, 2023, 11:10am UTC](https://forum.graphviz.org/t/respect-the-leaf-order-chomsky-tree-alike/1836 "2023-10-18T11:10:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![eiro](https://avatars.discourse-cdn.com/v4/letter/e/67e7ee/32.png) [@eiro](https://forum.graphviz.org/u/eiro)
#### Post date: [October 18, 2023, 11:10am UTC](https://forum.graphviz.org/t/respect-the-leaf-order-chomsky-tree-alike/1836/1 "2023-10-18T11:10:21Z")

</div>

hello graphwizards,

I have this dotfile in my repo. the idea is to describe a grammar in a chomsky tree way. I tested 2 strategies but none of them looks good. As the final output will be a part of a research paper, we have to make it looking good. I consider writing a wrapper over pic to do so but I’d really love to keep it the graphviz way.

Anyone can help on it ?

```auto
// Ideal case should be
// digraph {
// { node [shape=plain] rank=same
// Alain conduit la voiture
// }
// nc[label="nom\ncommun"]
// sujet -> Alain
// verbe -> conduit
// article -> la
// nc -> voiture
// complement -> {article nc}
// phrase -> { rank=same; sujet verbe complement }
// }
// but rank=same doesn't guaranty the order and this seems to be
// a known possible improvement:
// https://forum.graphviz.org/t/how-to-arrange-node-in-order/1303/2

// so I used record but there are 2 drawbacks
// * the tree isn't a tree anymore. I mean: in the first version,
// edges where vertical, this looks very good. Not anymore with
// the record because graphviz lost the ability to position the
// nodes to do so.
// * I would like to get a grid of the record boxes

digraph {
	texte[shape=record,label="<s>Alain|<v>conduit|<a>la|<nc>voiture"]
	nc[label="nom\ncommun"]
	sujet -> texte:s
	verbe -> texte:v
	article -> texte:a
	nc -> texte:nc:n
	complement -> {article nc}
	phrase -> { rank=same; sujet verbe complement }
}

```

---

<div class="post-metadata">

### Author: ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)
#### Post date: [October 18, 2023, 2:15pm UTC](https://forum.graphviz.org/t/respect-the-leaf-order-chomsky-tree-alike/1836/2 "2023-10-18T14:15:41Z")

</div>

Unfortunately keeping nodes (usually on a shared rank) in the desired order is a common problem. ([Help needed: Can't keep nodes in order entered (#1569) · Issues · graphviz / graphviz · GitLab](https://gitlab.com/graphviz/graphviz/-/issues/1569))  
There seems to be no guaranteed solution.  
Invisible edges & reordering the sequence of the input frequently help.  
Does this produce the desired output?

```auto
// Ideal case with some modification
 digraph {
 	{ node [shape=plain] rank=same
 		Alain conduit la voiture
 	}
 	nc[label="nom\ncommun"]
 	sujet -> Alain
 	verbe -> conduit
 	article -> la
 	nc -> voiture
 	complement -> { rank=same nc article}
 	phrase -> { rank=same; sujet verbe complement } // added rank=same
	article -> nc [style=invis] // added
 } 

```

Giving:  
 ![chomsky2](https://global.discourse-cdn.com/graphviz/original/2X/a/abec4c631d7d4343f3dd20c539a85c3665fdd00e.png)

---

<div class="post-metadata">

### Author: ![eiro](https://avatars.discourse-cdn.com/v4/letter/e/67e7ee/32.png) [@eiro](https://forum.graphviz.org/u/eiro)
#### Post date: [October 18, 2023, 2:40pm UTC](https://forum.graphviz.org/t/respect-the-leaf-order-chomsky-tree-alike/1836/3 "2023-10-18T14:40:43Z")

</div>

Dear steveroush,

> a common problem. (Help needed: Can’t keep nodes in order entered (#1569)

The bug ID will help me to stay tunned on it. thank you.

> There seems to be no guaranteed solution.

But a perfectly working one in my case.

> Invisible edges & reordering the sequence of the input frequently help.

I tried to reorder without luck but haven’t thought about invis edges.  
Again: thank you so much for your help!

regards.  
marc
