Creating documents with embedded diagrams
Tagged announcement, BlogLiterately, build, diagrams, document, dynamic, embedding, hint, LaTeX, Markdown
If you read my recent post about type algebra, you may have wondered how I got all those nice images in there. Surely creating the images and then inserting them into the post by hand would be rather tedious! Indeed, it would be, but that’s not what I did. I’m quite pleased to announce the release of several tools for making this sort of thing not only possible but even convenient.
Behind it all is the recently released diagrams-builder package. Diagrams backends such as diagrams-cairo give you a static way to render diagrams. diagrams-builder
makes the process dynamic: it can take arbitrary snippets of Haskell code, merge them intelligently, and call out to hint to render a diagram represented by some Haskell expression.
As a specific application of diagrams-builder
, I’ve released BlogLiterately-diagrams, a diagrams plugin for BlogLiterately. This is what I used to produce the type algebra post. The entire post was written in a single Markdown document, with inline diagrams code in specially marked code blocks. BlogLiterately-diagrams
handles compiling those code blocks and replacing them with the generated images; BlogLiterately
automatically uploads the images to the server. For example, including
```{.dia width='200'}
sq = square 1
foo 0 = sq
foo n = ((foo' ||| sq') === (sq' ||| foo')) # centerXY # scale 0.5
where
foo' = foo (n-1)
sq' = sq # fc (colors !! n)
colors = [black, red, orange, yellow, green, blue]
dia = foo 5 # lw 0
```
in the middle of a post results in
being included in the generated HTML.
Another exciting thing to mention is the LaTeX package diagrams-latex.sty
, included in the diagrams-builder
distribution. It lets you embed diagrams in LaTeX documents in much the same way that BlogLiterately-diagrams
lets you embed diagrams in blog posts. Just stick diagrams code between
and compile the document with pdflatex –enable-write18
. It probably needs more work to smooth out some rough edges, but it’s quite usable as it is—in fact, I’m currently using it to create the slides for my Haskell Symposium presentation in a few weeks.
Just to give a little perspective, this is essentially why I started building diagrams, over four years ago now—I wanted to produce some illustrations for a blog post, but was unsatisfied with the existing tools I found. With these tools, I can finally say that I’ve fully achieved my vision of four years ago—though don’t worry, my vision has grown much larger in the meantime!