Search code examples
htmlorg-modegraphvizdotorg-babel

Set width of org-babel generated dot graphs


Sometimes my org-babel generated dot graphs are huge and currently get cut off when viewing the html export in the browser. So I'm trying to set the width of the generated image tag to 100%. I currently generate the graph something like the following:

#+BEGIN_SRC dot :file x.svg :cmdline -Kdot -Tsvg
digraph {
  rankdir=LR; // graph from left to right

  A -> B -> C
}
#+END_SRC

And org 8.2.10 spits out this:

<div class="figure">
<p><img src="x.svg" alt="x.svg" />
</p>
</div>

I tried to put

#+ATTR_HTML: :width 100%

before #+BEGIN_SRC - which does work for [[file:...] links. I also tried

#+BEGIN_SRC dot :file x.svg :width 100% :cmdline -Kdot -Tsvg

Neither works.

So how can it be done?


Solution

  • To modify the width of a figure generated by a babel source block, add the :width attribute to the results block, like this:

    #+BEGIN_SRC dot :file x.svg :cmdline -Kdot -Tsvg
    digraph {
      rankdir=LR; // graph from left to right
    
      A -> B -> C
    }
    #+END_SRC
    
    #+attr_html: :width 100%
    #+RESULTS:
    [[file:x.svg]]
    

    The resulting html is then:

    <div class="figure">
    <p><img src="x.svg" alt="x.svg" width="100%" />
    </p>
    </div>
    

    This code sample was tested in Org mode 8.2.9 and Emacs 24.3.1.