Set width of org-babel generated dot graphs

1.1k views Asked by At

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?

2

There are 2 answers

0
jpeloquin On BEST ANSWER

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.

0
Evgeniy Berezovsky On

jpeloquin's answer made me aware of the fact that I can do it another way.

  • Set :exports none
  • Use a standard org-mode image link and set #+ATTR_HTML on that

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

  A -> B -> C
}
#+END_SRC

#+ATTR_HTML: :width 100%
[[file:x.svg]]

The resulting html is the same as jpeloquin's.

Tested on Org-mode version 8.2.10 and Emacs 24.4.1.