Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improve positioning of subscript and superscript on node labels

Tags:

graphviz

dot

When using both subscript and superscripts on a node label, is it possible to alter the positioning so that they are directly above each other.

Example:

digraph G {
        x11[label=<X<SUB>1</SUB><SUP>(1)</SUP>>];   
        x21[label=<X<SUB>2</SUB><SUP>(1)</SUP>>];
        x11 -> x21 
    }

Which produces

enter image description here

Is it possible to have the (#) directly above the # rather than slightly to the right? thanks


I tried to add a custom css script (re: HTML: can I place subscript text right under the superscript?) to my dot script with stylesheet = "styles.css"; (re: Using CSS classes in HTML labels on Graphviz), however, it it returns an error

Error: Unknown HTML element <span> on line 1

like image 274
user2957945 Avatar asked Sep 05 '16 16:09

user2957945


3 Answers

The native HTML-like node rendering of Graphviz is quite limited. The Graphviz docs say this clearly. I don't believe there's a way to coax it to do what you want. Even if there is a way to tweak e.g. a <table> definition to do it, the results are likely to look bad.

Therefore, I recommend you look at dot2tex. Its whole purpose is to allow the full power of LaTeX for rendering nodes. Setup isn't trivial, but results are worth it.

Here's a page showing examples of graphs containing LaTeX-set math in nodes.

You didn't say what the output should be. But there are ways to convert LaTeX to many different forms. Easiest are Postscript and PDF. But images formats are also possible.

Addition

Okay I installed dot2tex, and here's a result:

GraphViz graph with stacked subscripts under superscripts

Here's the corresponding dot code:

digraph G {
  a_1 [texlbl="$X_{1}^{(1)}$"];
  a_2 [texlbl="$X_{1}^{(2)}$"];
  a_3 [texlbl="$X_{1}^{(3)}$"];
  a_1-> a_2 -> a_3 -> a_1;
}

I compiled with

$ dot2tex foo.gv -f tikz > foo.tex
$ pdflatex foo.tex

Since you're already using LaTeX, you should be able to adjust this to meet your exact requirements without much trouble.

like image 197
Gene Avatar answered Oct 23 '22 14:10

Gene


Per the Graphviz docs, their 'HTML-Like Labels' do not include the span element, and the font-styling tags do not allow the style attribute (or any other):

<SUB
  <!-- No attributes -->
>

<SUP
  <!-- No attributes -->
>

The two best options for precise text layout seem to be:

1) Use a label with an html-like table, which does allow for custom styling (and handles vertically placed text fine anyway).

x11[label=<
    <TABLE border="0" cellborder="0" cellspacing="0">
    <TR><TD rowspan="2" style="...">X</TD><TD style="...">(1)</TD></TR>
    <TR>                                  <TD style="...">1</TD></TR>
    </TABLE>>];

2) If using the PostScript driver, override the node generation entirely and supply a ps file that draws exactly what you want.

like image 4
gz. Avatar answered Oct 23 '22 15:10

gz.


Maybe we could introduce a more powerful HTML renderer (I just noticed litehtml, and sciter which is not open source) which probably has its own make system, potential security issues, its own font and color name management, shared strings, C++ compiler dependencies, memory leaks, and litehtml for instance is only 16K lines of code which these days barely moves the meter for us, just I always feel a little guilty when I see tweets about how building graphviz is harder than the linux kernel. Stephen North

like image 4
north at graphviz Avatar answered Oct 23 '22 16:10

north at graphviz