SkillAgentSearch skills...

Tochemfig

Make Emacs write chemfig code from molfile or SMILES.

Install / Use

/learn @gicrisf/Tochemfig
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

#+title: tochemfig.el #+author: Giovanni Crisalfi

/Manipulating molecules with Emacs/.

=tochemfig= is an Emacs interface to =mol2chemfig=.

[[./default-demo.gif]]

  • Requirements From =mol2chemfig= documentation's abstract:

#+begin_quote =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. #+end_quote

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:

#+begin_src bash pip install -U mol2chemfigPy3 #+end_src

  • Quickstart The following is a SMILES representation of the caffeine:

#+begin_src CN1C=NC2=C1C(=O)N(C(=O)N2C)C #+end_src

You can turn this string into chemfig's code by running =M-x chemfig-input-direct=.

Gif

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.

#+begin_export latex \chemfig{ % 1 -[:42]N% 2 -[:96]% 3 =[:24]N% 4 -[:312]% 5 =[:240]% 6 ( -[:168]\phantom{N}% -> 2 ) -[:300]% 7 ( =[:240]O% 8 ) -N% 9 ( -[:300]% 14 ) -[:60]% 10 ( =O% 11 ) -[:120]N% 12 ( -[:180]% -> 5 ) -[:60]% 13 } #+end_export

If we export the LaTeX document as a PDF, we get the following result

#+DOWNLOADED: screenshot @ 2022-11-08 00:39:41 #+CAPTION: Caffeine rendered by LaTeX/TiKZ [[file:Quickstart/2022-11-08_00-39-41_screenshot.png]]

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.

#+DOWNLOADED: screenshot @ 2022-11-08 00:50:01 #+Caption: Caffeine rendered by LaTeX/TiKZ, flipped and rotated [[file:Quickstart/2022-11-08_00-50-01_screenshot.png]]

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).

#+begin_src emacs-lisp (setq tochemfig-default-input 'direct') #+end_src

If you're not familiar with changing the default variables in your config file, read the usage section of this file.

  • Installation 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:

#+begin_src emacs-lisp :tangle packages.el :noweb yes (package! tochemfig :recipe (:host github :repo "gicrisf/tochemfig")) #+end_src

On vanilla Emacs, you can do the same using =use-package= or =straight=. An example with =straight=:

#+begin_src emacs-lisp (straight-use-package '(tochemfig :type git :host github :repo "gicrisf/tochemfig")) #+end_src

Remember to add this to the =config.el= file to autoload the mode main function:

#+begin_src emacs-lisp ;; in ~/.doom.d/config.el (use-package! tochemfig) #+end_src

  • Usage Here are 4 ways to launch tochemfig:
  1. 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.
  2. To use =tochemfig-custom=, which opens a series of dialogs to customize the parameters. This is useful for beginners and casual users.

[[./custom-multiedit-demo.gif]]

  1. 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.
  2. 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.

[[./custom-one-edit-demo.gif]]

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.

#+begin_src emacs-lisp ;; 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) #+end_src

Launching =tochemfig-default= you ask Emacs to execute the command by the defaults without losing time asking anything and you must specify only the molecule you want to draw. If you usually use files as sources, maybe you will prefer to leave =tochemfig-default-input= as 'file'.

The other functions overwrite a specific argument over the default. Maybe sometimes you prefer throwing the SMILES of a molecule directly in the minibuffer, so, although your default is set to 'file', you can very easily by calling =tochemfig-input-direct=.

This way, every other preference is leaved untouched (and so you can keep the fancy bonds you usually expect from your outputs).

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=.

  • Features
  • [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

[ ] Wrapper for org mode (export block as LaTeX).

In questa libreria devo limitarmi SOLO a creare un'interfaccia per LaTeX

E quindi ricalcare SOLO le funzioni previste da mol2chemfig

[ ] Add a drawer for collecting metadata about the generated molecule

This could give me the opportunity to "re-render" the same molecule on place

Ma il drawer è una prerogativa di Org mode!

Meglio inserire queste funzioni in una libreria a parte

Potrebbe chiamarsi chemorg; chemutils;

(vedi wikinforg for wikinfo, anche per come ha implementato il drawer)

The drawer should collect:

- The name of the molecule (if given)

- The name of the file (if it's from file)

- A list of flag

This way I could edit the chemfig code, then re-render with other options

Per esempio, potrei scrivere la versione coi carbonio espliciti (più leggibile)

Alterare le funzioni che mi interessano, poi ri-renderizzare con carboni impliciti

Can I convert chemfig to SMILES?

L'ideale sarebbe produrre sul posto l'immagine

Sia per la maggiore integrazione in org-mode e quindi più facile esportazione in:

- HTML

View on GitHub
GitHub Stars12
CategoryDevelopment
Updated5mo ago
Forks0

Languages

Emacs Lisp

Security Score

87/100

Audited on Oct 31, 2025

No findings