Monday, 2 April 2012

Cheer up your LaTeX with SMILES support II

In Part I, I showed how to embed PNGs, automatically generated from SMILES by obabel, into LaTeX documents. An alternative approach is to use the SVG output from obabel.

In a comment to my earlier post, Billy suggests running the SVG through Inkscape or rsvg-convert:
You can also embed the material as a vector graphic, of course. Inkscape doesn't seem to support pipes, and rsvg-convert gives ugly output, but I'm sure there's other options.
\immediate\write18{obabel -:'#1' -osvg -p | rsvg-convert -f pdf -o smilesimg\arabic{smilescounter}.pdf}
\immediate\write18{obabel -:'#1' -O smilesimg\arabic{smilescounter}.svg -p ; inkscape -f smilesimg\arabic{smilescounter}.svg -A smilesimg\arabic{smilescounter}.pdf}
Also, if you don't want to call these applications when the graphic files aren't out of date, then use the code snippet found at the top of the 3rd page from this article.
I found a third approach on the interwebs soon after writing the initial post. It's by Jakob Lykke Andersen who converts the SVG to PDF with ImageMagick's convert. If you download the file graphviz.tex from his website, you can just include it and use it as in the following example:
The resulting PDF looks better than the original (though it could be because I didn't handle the PNGs properly in the first PDF). A nice little touch in Jakob's version is that an error box appears in the PDF if there is a problem generating the image.

Exercise for the reader:
A bit more polish is needed before these methods can be used wholesale by others. If you know a bit about LaTeX, have a go at an obabel package for CTAN.


Anonymous said...

Interesting, thanks a lot. Wonder what's your opinion on chemfig for LATeX. Also, is there a way to convert SMILES to say ppchtex or chemfig code directly -- that would be even better :)

Noel O'Boyle said...

I haven't used any of those, although I've glanced at the docs.

You're right - that would be better. All that's necessary is for someone to write an output format for Open Babel that takes a molecular structure and creates the necessary LaTeX.

In fact, you don't even need to use one of the chemistry libraries. It would be trivial to add a new painter that just draws the structure directly using LaTeX commands. If you can think of how to translate the paint commands at into LaTeX drawing commands, then I can add it to Open Babel...

Jakob Lykke Andersen said...

One way to depict molecules in Latex is to use Tikz. I tried to make some small examples (tex, pdf) which exploits the sub-/super-scripting of Latex. The position of some of the nodes are a bit off, as they come from the SVG output of obabel and has been manually corrected. However, a native Tikz generator should be able have less hacks.

Noel O'Boyle said...


In parallel, I've been playing with a pstricks implementation. Once I have some time, I'll add one or the other (or both) to Open Babel...I'll be in touch...

Acetaminophen said...

It may be too late, but I found this post only last month and made a new package, which may be useful to insert chemical structural formulas into LaTeX documents.
See my GitHub repository.
With this package, you can convert not only from SMILES notations but also from .cdx or .mol files easily.

I'd like you to check if this package is useful or not, and any comments are appreciated.
(Contact information is written in the header of chemobabel.sty)

Enjoy! :)