LaTeX chemfig库安装失败求助:Google Colab中安装Manim、Chanim及通过\tlmgr安装chemfig遇阻
Hey there! I’ve dealt with similar headaches setting up Manim, Chanim, and the chemfig LaTeX package on Google Colab before—let’s walk through a reliable, step-by-step fix to get everything working smoothly for you.
First, let’s clean up any half-installed packages or conflicting files from your previous attempts. Run these commands to wipe old TeX Live and Python package residues:
!pip uninstall -y manim chanim !sudo apt-get remove --purge texlive* !sudo apt-get autoremove --purge !sudo rm -rf /usr/local/texlive /var/lib/texmf
Colab’s default TeX setup is often missing niche packages like chemfig. Let’s install a minimal but complete base TeX distribution, then use tlmgr to add chemfig explicitly:
# Update apt repositories first !sudo apt-get update # Install core TeX packages !sudo apt-get install -y texlive-base texlive-latex-extra texlive-fonts-recommended # Install chemfig via tlmgr (TeX Live’s package manager) !tlmgr install chemfig
If you get a tlmgr: command not found error, install the full TeX Live distribution instead (it’s larger but avoids missing dependencies):
!sudo apt-get install -y texlive-full
Manim Community Edition is the most stable version for Colab, and Chanim relies on it. Install them in order to avoid dependency clashes:
# Install Manim Community Edition !pip install manim # Install Chanim (use the latest compatible version) !pip install chanim
If you hit dependency errors, try forcing a reinstall with updated pip:
!pip install --upgrade pip !pip install --force-reinstall manim chanim
Let’s confirm each component is installed correctly:
- Check Manim version:
!manim --version - Check Chanim import:
import chanim print(f"Chanim version: {chanim.__version__}") - Test chemfig compilation:
# Create a test LaTeX file with a chemical structure !echo '\documentclass{article} \usepackage{chemfig} \begin{document} \chemfig{CH3-CH2-OH} \end{document}' > test_chemfig.tex # Compile it with pdflatex !pdflatex test_chemfig.tex
If you see Output written on test_chemfig.pdf, chemfig is working.
- If Manim throws LaTeX errors: Make sure it’s using the correct TeX compiler path. Add this to your Manim code:
from manim import * config.tex_compiler = "pdflatex" config.tex_path = "/usr/bin/pdflatex" - If Chanim fails to import: Double-check that you installed Manim first—Chanim is a Manim extension and requires it as a dependency.
内容的提问来源于stack exchange,提问作者GameDev Rony

