# Text wrapping in Graphviz?

**URL:** <https://forum.graphviz.org/t/text-wrapping-in-graphviz/753>\
**Category:** Help\
**Created:** [July 22, 2021, 6:14pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753 "2021-07-22T18:14:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/cjr/32/345_2.png) [@cjr](https://forum.graphviz.org/u/cjr)\
**Post date:** [July 22, 2021, 6:14pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/1 "2021-07-22T18:14:27Z")

</div>

I’m wondering if anything has changed with regard to supporting automatically wrapping text in Graphviz? I didn’t see anything on this forum and DuckDuckGo continues to point to the StackOverflow posts from a decade ago: [Text wrapping with dot](https://stackoverflow.com/questions/5277864/text-wrapping-with-dot-graphviz#5278224), [Automatic multiline labels in Graphviz?](https://stackoverflow.com/questions/1476432/automatic-multiline-labels-in-graphviz)

Using dot2tex may be just fine for my purposes; I’m just wondering if there have been other developments or solutions that I might have missed?

---

<div class="post-metadata">

**Author:** ![schmoo2k](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/schmoo2k/32/29_2.png) [@schmoo2k](https://forum.graphviz.org/u/schmoo2k)\
**Post date:** [July 23, 2021, 7:44pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/2 "2021-07-23T19:44:30Z")

</div>

You can insert “\n” for a line break - not sure if that counts as automatic?

---

<div class="post-metadata">

**Author:** ![cjr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/cjr/32/345_2.png) [@cjr](https://forum.graphviz.org/u/cjr)\
**Post date:** [July 28, 2021, 7:04pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/3 "2021-07-28T19:04:30Z")

</div>

Not if the newlines have to be manually inserted. 😉 I’m really just wondering if there have been any additional solutions, scripts, etc beyond the ones linked to above.

---

<div class="post-metadata">

**Author:** ![ronman](https://avatars.discourse-cdn.com/v4/letter/r/ac91a4/32.png) [@ronman](https://forum.graphviz.org/u/ronman)\
**Post date:** [July 28, 2021, 10:09pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/4 "2021-07-28T22:09:19Z")

</div>

Such a script would be very welcome and a valuable addition to Graphviz. It would need to take into account node size, font width in points, node margin, etc. Maybe something in Python, or an ancillary tool in Graphviz. I know nothing about gvpr but maybe it would be up to the task.

---

<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:** [February 14, 2022, 6:05pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/5 "2022-02-14T18:05:05Z")

</div>

For reasons that escape me, I’ve written a script that does word-wrap.  
Node labels only, no HTML and no record nodes, just strings with \\, \n, \r, \l, \G, and \N (sorry).  
It adds one new node attribute “maxTextWidth” - units are inches - e.g  
`mynode [maxTextWidth=3.3 label="bla bla bla ..."]`

It is one of the nastiest things I’ve written in 50 years of programming, so before I consider releasing it to the world:

1. Given the constraints listed above, does anyone want it?
2. While it has been tested a bit, (see below) it needs much more testing - non-ASCII chars, and who knows what.

If you are interested, please send me a test file via forum message (click on steveroush, then click message) and attach your best. Remember to add the new attribute as desired (otherwise, no word wrap).

[why a script and not changes to the codebase - good question]

 ![WWtest1](https://global.discourse-cdn.com/graphviz/original/1X/117b437046a11fc1114821750b1afa35db9c100c.png)

---

<div class="post-metadata">

**Author:** ![purva5](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@purva5](https://forum.graphviz.org/u/purva5)\
**Post date:** [March 15, 2023, 8:56am UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/6 "2023-03-15T08:56:11Z")

</div>

Hi @steveroush , I want to know the script since I want to use it for a project. I am automatically generating flowcharts so I will need text wrapping.

---

<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:** [March 15, 2023, 8:58pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/7 "2023-03-15T20:58:03Z")

</div>

I have posted it here: [word wrap for Graphviz · GitHub](https://gist.github.com/steveroush/4287562c90855fd0d6a991e6159003ce)  
(I think I grabbed the correct script, let me know if things don’t work)

---

<div class="post-metadata">

**Author:** ![cjr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/cjr/32/345_2.png) [@cjr](https://forum.graphviz.org/u/cjr)\
**Post date:** [March 15, 2023, 10:11pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/8 "2023-03-15T22:11:16Z")

</div>

Thanks for doing this! Really appreciate it!

---

<div class="post-metadata">

**Author:** ![neeharikab](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@neeharikab](https://forum.graphviz.org/u/neeharikab)\
**Post date:** [January 22, 2024, 4:30pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/9 "2024-01-22T16:30:50Z")

</div>

Hi! So I found a way to do this; I’m working on a hierarchy model in Python (I’m not sure what you are using so sorry if this is irrelevant + I know its been a few years). My data is exported from excel, and I’m using two of the columns are part of node text.

This code automatically wraps them for me

```auto
# Wrap the text in nodes using textwrap
        wrapped_role = textwrap.fill(root.role, width=30)
        wrapped_responsibility = textwrap.fill(root.responsibilities, width=30)

node_label = f'{wrapped_role}\n{wrapped_responsibility}'

```

If you want to see how this fits in my overall code I’d be glad to share but for some reason this took me a while to figure out!

---

<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:** [January 22, 2024, 11:49pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/10 "2024-01-22T23:49:50Z")

</div>

Yep, an external solution is the best, as long as it considers character spacing of the desired font, the actual width/height available, …

---

<div class="post-metadata">

**Author:** ![neeharikab](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@neeharikab](https://forum.graphviz.org/u/neeharikab)\
**Post date:** [January 23, 2024, 8:42am UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/11 "2024-01-23T08:42:51Z")

</div>

Of course! After your comment, I just realised that the character spacing in my output was a bit off (and that it could definitely look better), so I’m going to try out the script you added earlier in the forum. 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:** [January 27, 2024, 2:10am UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/12 "2024-01-27T02:10:54Z")

</div>

**pango** has the ability to word wrap.  
The call is `pango_layout_set_width` ([Pango.Layout.set\_width](https://docs.gtk.org/Pango/method.Layout.set_width.html))

I hacked the following into **plugin/pango/gvtextlayout\_pango.c** and got word wrapping after 3 inches of text.

```auto
   pango_layout_set_width (layout, 1024*72*3); // 3 inches

```

There is much more to it that my simple hack, but its nice to think of **pango** doing the heavy lifting.  
The other part of the challenge is setting lengths for node labels, edge labels, xlabels, graph labels, and fields within records and html nodes. (probably forgot a few).  
How to do that without really hacking up the Graphviz language will take some real thought.

Example:

 ![WordWraptest1](https://global.discourse-cdn.com/graphviz/original/2X/3/353dd69e1851c6e87abec1daf02ea5c54b70b4d7.png)

---

<div class="post-metadata">

**Author:** ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)\
**Post date:** [January 27, 2024, 11:14pm UTC](https://forum.graphviz.org/t/text-wrapping-in-graphviz/753/13 "2024-01-27T23:14:47Z")

</div>

Yes, it’s a little complicated because we don’t necessarily have pangocairo.
