tochemfig
is an Emacs interface to mol2chemfig
.
From mol2chemfig
documentation’s abstract:
mol2chemfig
is a Python program that generates TeX graphics of chemical structures provided in molfile or SMILES format. Its output is written in the syntax of the chemfig package, which in turn is based on TiKZ.
Thus, both LaTeX/TiKZ and Python/mol2chemfig are required in order to compile tochemfig
’s output.
This package only requires access to mol2chemfig
through the shell.
This means you can set up a working Python 3 environment and launch mol2chemfigPy3
or keep using the original mol2chemfig
in Python 2.
You could use the LUA web client too, if you are more comfortable with it, but of course that way you would depend on internet access.
Most linux distros come with a working Python 3 version already installed.
To keep things simple, you could install mol2chemfigPy3
with this command:
pip install -U mol2chemfigPy3
The following is a SMILES representation of the caffeine:
CN1C=NC2=C1C(=O)N(C(=O)N2C)C
You can turn this string into chemfig’s code by running M-x chemfig-input-direct
.
The minibuffer will request an input: copy-paste the SMILES, press RET
and Emacs will do the magic, inserting the final result right into your buffer.
If we export the LaTeX document as a PDF, we get the following result
which is fine, but maybe we’re used to see the xanthines with a slight rotation, with the imidazole ring on the right and parallel to the ground.
To obtain this result, we can rotate the molecule by an angle of -30° and flip it horizontally, all with a single act: running M-X chemfig-rotate
on the same input (as long as the “direct” input is set as default).
(setq tochemfig-default-input 'direct')
If you’re not familiar with changing the default variables in your config file, read the usage section of this file.
This package is under development, so it’s not available on MELPA yet. If you really can’t wait, you can install it through this git repo. The package is already functional: some function could be renamed soon.
On Doom Emacs, you could install directly from the Github repo:
(package! tochemfig
:recipe (:host github :repo "gicrisf/tochemfig"))
On vanilla Emacs, you can do the same using use-package
or straight
.
An example with straight
:
(straight-use-package
'(tochemfig :type git :host github :repo "gicrisf/tochemfig"))
Remember to add this to the config.el
file to autoload the mode main function:
;; in ~/.doom.d/config.el
(use-package! tochemfig)
Here are 4 ways to launch tochemfig:
- To use
tochemfig-default
, which is the most immediate although less flexible method, you by simply introduce the molecule string in the minibuffer and the chemfig code will be generated instantly. You can configure this to open input from different sources such as files, SMILES or Pubchem identifiers. - To use
tochemfig-custom
, which opens a series of dialogs to customize the parameters. This is useful for beginners and casual users. - To use
tochemfig-custom-raw
, you can write the flags for the shell command. This method may not be as comfortable as the other options, but it is immediate if you are accustomed to the underlying CLI interface. - Other
tochemfig-
functions allow you to overwrite a single parameter with different settings than the defaults. For example,tochemfig-input-direct
changes the input mode to allow pasting a SMILES directly into the minibuffer.
The last case is better explained through a contextualized, applied example.
Assuming you always get your molecules from an external file and set “file” as the default input, what do you do if you need to input a SMILES directly into the minibuffer? You could use tochemfig-custom
, but that may be too time consuming if you only need to make a single change from the defaults. Instead, use tochemfig-input-direct
to change the input mode temporarily. Similarly, if you want to keep all default settings but remove whitespaces when rendering, use tochemfig-terse
. The same goes for any other parameter in the list.
It’s important to note that in this library the same defaults of the original mol2chemfig
have been kept to avoid confusion among users. To set your custom defaults, simply reset the default command by specifying new defaults in your configuration file.
;; We can change the default simply resetting the default var.
;; The default input expected is a mol file;
(setq tochemfig-default-input 'direct')
;; The default option provides absolute angles;
(setq tochemfig-default-relative-angles t)
;; This option improves the aesthetics of double and triple bonds;
(setq tochemfig-default-fancy-bonds t)
;; We can wrap the code in `\chemfig` latex command;
(setq tochemfig-default-wrap-chemfig t)
The defaults have impact on any function you launch except for tochemfig-custom-raw
. Yes, tochemfif-custom
is influenced too, because the first configuration is based on the actual defaults. You can change any value before the rendering and checking wheter a parameter is already selected or not.
To have a complete view on all the features provided, install the package and launch M-x tochemfig
.
- [X] Generate chemfig code from mol or SMILES
- [X] Generate chemfig code from files
- [X] Generate chemfig code from pubchem
- [X] Generate from verbatim string
- [X] Generate abiding to Indigo’s chemical validation
- [X] Flipping horizontally or vertically the drawing
- [X] Generate extended or compact chemfig code
- [X] Rotate the drawing by a custom angle
- [X] Recalculate coordinates on molfiles before drawing
- [X] Generate chemfig code specifying relative angles instead of absolute ones
- [X] Generate chemfig code showing carbons
- [X] Generate chemfig code showing methyls
- [X] Manipulate hydrogen on the given structure
- [X] Draw circles instead of double bonds inside aromatic rings
- [X] Teach chemfig how to draw fancier double and triple bonds
- [X] Show the molfile number of each atom next to it
- [X] Scale the lengths of bonds
- [X] Stretch the lengths of bonds
- [X] Wrap the code in “\chemfig{…}” LaTeX command
- [X] Define submols
- [X] Specify first atom to be rendered in submols
- [X] Specify last atom to be rendered in submols
- [X] Specify bonds that should be drawn on top of others they cross over
- [X] Generate chemfig code with customizable defaults
- [X] Generate chemfig code with customized arguments (inject mol2chemfig flags in the command)
- [X] Launch with customizable defaults while forcing a specific argument
- [X] Support for
mol2chemfigPy3
- [X] Support for
mol2chemfig
(the original Python 2 package) - [X] Support for
mol2chemfig
LUA web client - [X] Export submols as external
.tex
files - [X] Aided, step-by-step function to customize the command
Did you find this package useful? If you’d like to help me stay awake (writing code, wink wink), consider buying me a coffee.
Open sourced under the MIT license.