Does anyone know how to put newline in the label of the node? \n is not working - instead some new nodes appear.
-
**TL;DR: Use `\n`.** – Andrew Feb 16 '21 at 18:53
5 Answers
This works for me as documented:
digraph {
n[label="two\nlines"]
"on\nthree\nlines"
}
Either put in in a label attribute (my preference), or use it as the node's name, but always enclose it with double quotes.
![](../../users/profiles/63733.webp)
- 49,690
- 10
- 152
- 168
-
It appears that `'n` is to explicitly center the lines. While this is default behavior (on mine at least), if you were to specify this globally (I assume there is such an option) then `'n` would override that setting while `\n` would not (presumably). – altendky Dec 05 '12 at 20:08
-
@altendky Which version is needed in order to get `'n` to work? With 2.29.20120504.0446, this still doesn't work. Only `\n` works (or `\r` to right align, and `\l` to left align). Furthermore, this only works for the first two lines. All the subsequent lines are centered, whatever escape sequence is used. – marapet Dec 05 '12 at 21:44
-
1My previous comment was based only on the link. I just tried it and `'r` does not seem to work, only `\r`. Also, `\r` applies right justify the line _before_ it (ditto for left). I have 2.29.20121205.0545 but I have no idea when this was implemented or to what degree. My test node is: `multilineNode [label="line 1 asdf\nline 2 asfdasdfd\nline 3 fd\rline 4 fdasfasddddddd\nline 5 fdsf\l d \N\r"]` Note `\N` to get the node name in there too. – altendky Dec 06 '12 at 15:46
-
3In case anyone wants to combine fonts, with a newline, html labels, specifically `
` can be used eg `label=<Word1
Word2>` – user2957945 Sep 28 '17 at 18:56 -
This answer is horrifically unclear. All it is saying is to use `\n`... – Andrew Feb 16 '21 at 18:53
You can use \n
character
With graphviz
package, this would give
from graphviz import Digraph
d=Digraph()
d.node('test',label='line 1\\nline 2')
print(d.source)
This would give
digraph {
test [label="line 1\nline 2"]
}
![](../../users/profiles/1478537.webp)
- 9,076
- 5
- 38
- 47
<BR/>
tag in a HTML-like label creates a line break.
digraph {
n[label=<two<BR/>lines>]
}
This can come handy when the \n
syntax cannot be used. Most notably, the graphviz
package for LaTeX can have problems parsing \
inside .tex
files, and using the HTML-like syntax is a workaround.
![](../../users/profiles/6099426.webp)
- 553
- 4
- 13
This issue was also important to me, as I was using graphviz to generate detailed UML diagrams and needed to use escape characters in the labels. However, using the Python package, I encountered a bug in how escape characters are handled, so some of the recommended solutions did not work.
For example:
from graphviz import Digraph
d=Digraph()
d.node('test',label='line 1\\nline 2')
print(d.source)
Generated the following (note that escaping does not work):
digraph {
test [label="line 1\\nline 2"]
}
Workarounds such as using a single backslash, rawstrings, are infuriatingly ineffective. However, the workaround that did ultimately work was the following:
s = graphviz.Source(d.source.replace('\\\\', '\\'))
s.render('my_uml')
I don't know if this bug in handling escape characters is in the Python bindings (v0.12) or graphviz itself (v2.44), but since others may encounter it, I wanted to offer this solution.
![](../../users/profiles/4921573.webp)
- 1
- 2