igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Dot labels with newlines


From: Tamás Nepusz
Subject: Re: [igraph] Dot labels with newlines
Date: Thu, 2 Feb 2012 23:21:54 +0100

Hi Justin,

This is most likely a bug in igraph's DOT exporter; I will check the DOT format 
specification to see how we can deal with it. In the meanwhile, I think the 
easiest workaround is to use escaped newlines (e.g., r"foo\nbar") and then 
post-process your files, either using a simple sed script that replaces "\\n" 
with "\n", or straight from Python. Something like this (untested):

from tempfile import NamedTemporaryFile
import os

def write_dot_fixed(graph, filename):
    tmpfile = NamedTemporaryFile()
    tmpfile.close()
    graph.write_dot(tmpfile.name)
    
    outf = open(filename, "w")
    for line in open(tmpfile.name):
        outf.write(line.replace(r"\\n", r"\n"))
    outf.close()

    os.unlink(tmpfile.name)

Best,
Tamas

On 2 Feb 2012, at 18:03, Justin McCann wrote:

> Does anyone have any recommendations for putting newlines (\n) into
> labels in a way that will make dot happy?
> 
> Whenever I call g.write_dotfile(filename) with a graph that contains
> labels with newlines, the string formatting gets in the way a little
> bit.
> 
> For example
>   v['label'] = "foo\nbar"
> 
> Ends up getting rendered in the dot file as
>   1 [label="foo
> bar"]
> 
> which Dot doesn't like, and truncates at the end of the line (just
> "foo" gets rendered).
> 
> If I try
>   v['label'] = r"foo\nbar"
> or
>   v['label'] = "foo\\nbar"
> 
> the dotfile ends up with
>  1 [label="foo\\nbar"]
> 
> Which dot renders as the string "foo\nbar" in the resulting graph image file.
> 
> I've also tried HTML-formatted labels, but igraph puts them into the
> dotfile in double-quotes, and it needs to be in curly braces for HTML
> rendering.
> 
> Any ideas?
> 
> Thanks!
>    Justin
> 
> _______________________________________________
> igraph-help mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/igraph-help
> 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]