Skip to content

Instantly share code, notes, and snippets.

@uwezi
Last active January 24, 2026 12:55
Show Gist options
  • Select an option

  • Save uwezi/28f7240efca0434a680105d14c2e7355 to your computer and use it in GitHub Desktop.

Select an option

Save uwezi/28f7240efca0434a680105d14c2e7355 to your computer and use it in GitHub Desktop.
[Organic chemistry] Using LaTeX's chemfig macro in Manim. #manim #latex #tikz #chemfig #chemistry
from manim import *
from mol2chemfigPy3 import mol2chemfig
myChemTemplate = TexTemplate(
tex_compiler="latex",
output_format='.dvi',
preamble=r"""
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{chemfig}
\usepackage{mol2chemfig}
\setchemfig{atom sep=2em,angle increment=45,bond offset=2pt,double bond sep=2pt}
"""
)
class caffeine(Scene):
def construct(self):
chem = Tex(
r"\chemfig{*6((=O)-N(-CH_3)-*5(-N=-N(-CH_3)-=)--(=O)-N(-H_3C)-)}",
tex_template=myChemTemplate
).set_stroke(width=3)
self.play(Write(chem))
self.wait()
class pubchem(Scene):
def construct(self):
cmd = mol2chemfig('997', inline=True)
chem = Tex(
cmd,
tex_template=myChemTemplate
).set_stroke(width=3)
self.play(Write(chem))
self.wait()
class kadir(Scene):
def construct(self):
a = mol2chemfig('CN1C=NC2=C1C(=O)N(C(=O)N2C)C', inline=True, aromatic=False)
chem = Tex(
a,
tex_template=myChemTemplate
).set_stroke(width=3)
self.play(Write(chem))
self.wait()

Originally from December 2022: using chemfig in Manim

image image image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment