# Way to center a node in a table

**URL:** <https://forum.graphviz.org/t/way-to-center-a-node-in-a-table/285>\
**Category:** Help\
**Created:** [August 4, 2020, 3:37pm UTC](https://forum.graphviz.org/t/way-to-center-a-node-in-a-table/285 "2020-08-04T15:37:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackdunnnz](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jackdunnnz](https://forum.graphviz.org/u/jackdunnnz)\
**Post date:** [August 4, 2020, 3:37pm UTC](https://forum.graphviz.org/t/way-to-center-a-node-in-a-table/285/1 "2020-08-04T15:37:01Z")

</div>

In the following example I am trying to add a caption above my “actual” node, which I have been doing by putting them both into rows of a table. When the caption is shorter than the node contents it is fine, but when the caption is larger, then the spacing in the table causes the node to be uncentered with respect to the caption

[dot verbose=true]

```auto
digraph {
    test [label=<
    <TABLE BORDER="0">
      <TR>
        <TD COLSPAN="3">This is a really long caption</TD>
      </TR>
      <TR>
        <TD></TD>
        <TD BORDER="1">Node</TD>
        <TD></TD>
      </TR>
    </TABLE>
>,style="invisible"];}

```

[/dot]

I tried playing around with the order of the cells to understand what was going on and it seems that the first cell in the row is assigned any extra width:

[dot verbose=true]

```auto
digraph {
    test [label=<
    <TABLE>
      <TR>
        <TD COLSPAN="3">This is a really long caption</TD>
      </TR>
      <TR>
        <TD>Node</TD>
        <TD></TD>
        <TD></TD>
      </TR>
    </TABLE>
>,style="invisible"];}

```

[/dot]

[dot verbose=true]

```auto
digraph {
    test [label=<
    <TABLE>
      <TR>
        <TD COLSPAN="3">This is a really long caption</TD>
      </TR>
      <TR>
        <TD></TD>
        <TD>Node</TD>
        <TD></TD>
      </TR>
    </TABLE>
>,style="invisible"];}

```

[/dot]

[dot verbose=true]

```auto
digraph {
    test [label=<
    <TABLE>
      <TR>
        <TD COLSPAN="3">This is a really long caption</TD>
      </TR>
      <TR>
        <TD></TD>
        <TD></TD>
        <TD>Node</TD>
      </TR>
    </TABLE>
>,style="invisible"];}

```

[/dot]

Ideally I would have the empty cells on the left and right grow to take up any extra space, and have the central cell just large enough to fix the contents. Is there any way to have this happen automatically, or a better way to achieve my desired end result of having the node and caption aligned together?

Thanks!

---

<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:** [August 4, 2020, 11:08pm UTC](https://forum.graphviz.org/t/way-to-center-a-node-in-a-table/285/2 "2020-08-04T23:08:27Z")

</div>

Curious. I can’t find a way to make the HTML record behave, but here is an alternative (use clusters instead):  
digraph {  
subgraph clusterTest {  
peripheries=0 // no box surrounding the cluster  
labelloc=t  
label=“This is a really long caption”  
n[label=“node” shape=rectangle]  
}  
}  
Giving:  
 ![longExternal3](https://global.discourse-cdn.com/graphviz/original/1X/486410a393c517f60def0063d298d59483f59668.png)

---

<div class="post-metadata">

**Author:** ![jackdunnnz](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jackdunnnz](https://forum.graphviz.org/u/jackdunnnz)\
**Post date:** [August 5, 2020, 5:28pm UTC](https://forum.graphviz.org/t/way-to-center-a-node-in-a-table/285/3 "2020-08-05T17:28:29Z")

</div>

Thanks @steveroush!

My actual example is a bit more involved, where I want to have captions above and below the box, so something like

[dot]

```auto
digraph {
    test [label=<
    <TABLE BORDER="0">
      <TR>
        <TD COLSPAN="3">This is a really long caption</TD>
      </TR>
      <TR>
        <TD></TD>
        <TD BORDER="1">Node</TD>
        <TD></TD>
      </TR>
      <TR>
        <TD COLSPAN="3">Bottom caption</TD>
      </TR>
    </TABLE>
>,style="invisible"];}

```

[/dot]

Any suggestions on how to get this to work?

---

<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:** [August 5, 2020, 6:27pm UTC](https://forum.graphviz.org/t/way-to-center-a-node-in-a-table/285/4 "2020-08-05T18:27:01Z")

</div>

Kludge on top of kludge, but legal & seems to look OK. Embed one cluster inside another, then set **margin=0** to tighten things up:

> digraph {  
> subgraph clusterLower {  
> peripheries=0 // no box surrounding the cluster  
> labelloc=b  
> margin=0 // reduce distance from node to upper/lower label  
> label=“This is a lower long caption”
> 
> subgraph clusterUpper {  
> peripheries=0 // no box surrounding the cluster  
> labelloc=t  
> margin=0 // reduce distance from node to upper/lower label  
> label=“This is a really long upper caption”  
> n[label=“node” shape=rectangle]  
> }  
> }  
> }  
> Gives:  
> ![longExternal3](https://global.discourse-cdn.com/graphviz/original/1X/37dba6f5aa0dee5dc77cd5f6aaf6b965c20d8bbb.png)

---

<div class="post-metadata">

**Author:** ![jackdunnnz](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jackdunnnz](https://forum.graphviz.org/u/jackdunnnz)\
**Post date:** [August 5, 2020, 6:40pm UTC](https://forum.graphviz.org/t/way-to-center-a-node-in-a-table/285/5 "2020-08-05T18:40:22Z")

</div>

Perfect I will give it a shot, thanks so much @steveroush!

---

<div class="post-metadata">

**Author:** ![jackdunnnz](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jackdunnnz](https://forum.graphviz.org/u/jackdunnnz)\
**Post date:** [August 6, 2020, 2:14pm UTC](https://forum.graphviz.org/t/way-to-center-a-node-in-a-table/285/6 "2020-08-06T14:14:53Z")

</div>

I tried this out with multiple nodes of this type, and it seems that the nodes are free to slide within the clusters:

[dot verbose=true]

```auto
digraph {
  compound=true;
  subgraph clusterLower {
    peripheries=0 // no box surrounding the cluster
    labelloc=b
    margin=0 // reduce distance from node to upper/lower label
    label="This is a lower long caption"

    subgraph clusterUpper {
      peripheries=0 // no box surrounding the cluster
      labelloc=t
      margin=0 // reduce distance from node to upper/lower label
      label="This is a really long upper caption"
      n[label="node" shape=rectangle]
    }
  }

  subgraph clusterLower2 {
    peripheries=0 // no box surrounding the cluster
    labelloc=b
    margin=0 // reduce distance from node to upper/lower label
    label="This is a lower long caption"

    subgraph clusterUpper2 {
      peripheries=0 // no box surrounding the cluster
      labelloc=t
      margin=0 // reduce distance from node to upper/lower label
      label="This is a really long upper caption"
      n2[label="node" shape=rectangle]
    }
  }

  subgraph clusterLower3 {
    peripheries=0 // no box surrounding the cluster
    labelloc=b
    margin=0 // reduce distance from node to upper/lower label
    label="This is a lower long caption"

    subgraph clusterUpper3 {
      peripheries=0 // no box surrounding the cluster
      labelloc=t
      margin=0 // reduce distance from node to upper/lower label
      label="This is a really long upper caption"
      n3[label="node" shape=rectangle]
    }
  }

  n -> n2;
  n -> n3;
}

```

[/dot]

Is there a way to prevent this from happening and lock them in place in the center?

Thanks so much again for all the help!

---

<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:** [August 7, 2020, 12:45am UTC](https://forum.graphviz.org/t/way-to-center-a-node-in-a-table/285/7 "2020-08-07T00:45:31Z")

</div>

The documentation is (slightly) ambiguous regarding whether you can set separate X & Y values for a cluster **margin** (e.g. **margin=“20,0”** ). That would probably solve the problem.  
Unfortunately, clusters only take one value for both X & Y. (Rats)  
So, we combine html & clusters into a _mondo_ kludge:

```
digraph {
graph [splines=false] // if you want straight lines & not curves
compound=true;
subgraph clusterLower {
peripheries=0 // no box surrounding the cluster
labelloc=b
margin="0,0" // reduce distance from node to upper/lower label
label="This is a lower long caption"

subgraph clusterUpper {
  peripheries=0 // no box surrounding the cluster
  labelloc=t
  margin="0,0" // reduce distance from node to upper/lower label
  label="This is a really long upper caption"
  //n[label="node" shape=rectangle]
    N1 [label=<
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
  <TR>
<TD BORDER="0"> </TD>
    <TD BORDER="1" PORT="n1" CELLPADDING="4">Node</TD>
<TD BORDER="0"> </TD>
  </TR>
</TABLE>>,style="invisible"];
 }
}

subgraph clusterLower2 {
//graph [margin="2,40"] // reduce distance from node to upper/lower label
margin=0
peripheries=0 // no box surrounding the cluster
labelloc=b

label="This is a lower long caption"

subgraph clusterUpper2 {
  //peripheries=0 // no box surrounding the cluster
  labelloc=t
  label="This is a really long upper caption"
  //n2[label="node" shape=rectangle]

 N2 [label=<
  <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
   <TR>
      <TD BORDER="1" PORT="n2" CELLPADDING="4">Node</TD>
  <TD BORDER="0"> </TD>
    </TR>
  </TABLE>>,style="invisible"];
  }
}

subgraph clusterLower3 {
  peripheries=0 // no box surrounding the cluster
  labelloc=b
  margin=0 // reduce distance from node to upper/lower label
  label="This is a lower long caption"

  subgraph clusterUpper3 {
    peripheries=0 // no box surrounding the cluster
    labelloc=t
    margin=0 // reduce distance from node to upper/lower label
    label="This is a really long upper caption"
    //n3[label="node" shape=rectangle]
    N3 [label=<
 <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
  <TR>
<TD BORDER="0"> </TD>
    <TD BORDER="1" PORT="n3" CELLPADDING="4">Node</TD>
  </TR>
 </TABLE>>,style="invisible"];
 }
}

N1:n1:s -> N2:n2;
N1:n1:s -> N3:n3;
}

```

Giving:

 ![longExternal7](https://global.discourse-cdn.com/graphviz/original/1X/4f1a6af3f94b5546b1d70a3ba1901520dabd123e.png)

I’m not happy with it, but it (maybe, kinda) works. I’ll ponder some more.
