You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Jupyter Notebook转PDF报错:未找到名为‘latex’的模板子目录

Fixing "No template sub-directory with name 'latex'" in nbconvert PDF export

Hey Stan, let's work through this issue together. That error pops up because nbconvert can't locate the required LaTeX templates to build your PDF, even though you've got LaTeX and the necessary Python packages installed. Here are actionable steps to resolve it:

1. Force-reinstall nbconvert to restore missing templates

Sometimes template files don't get properly installed or go missing during updates. Reinstalling nbconvert will ensure all required files are placed correctly:

pip install --force-reinstall nbconvert

After running this, try your original conversion command again to see if the issue is fixed.

2. Manually specify the template directory

If reinstalling doesn't help, you can point nbconvert directly to the template folder. First, find where nbconvert is installed on your system:

pip show nbconvert

Look for the Location field in the output (e.g., /opt/homebrew/lib/python3.11/site-packages). Navigate to that directory and you should find a nbconvert/templates folder containing a latex subfolder.

Then run the conversion command with the --template-dir flag to explicitly tell nbconvert where to find the templates:

python -m jupyter nbconvert notebook.ipynb --to pdf --template-dir /path/to/nbconvert/templates

Replace /path/to/nbconvert/templates with the actual path from the pip show output plus /nbconvert/templates.

3. Update Jupyter's exporter configuration

You can also add the template path to Jupyter's config so it always recognizes the templates by default:

  1. Generate a Jupyter config file if you don't have one already:
    jupyter notebook --generate-config
    
  2. Open the config file (located at ~/.jupyter/jupyter_notebook_config.py on Mac/Linux) and find the line starting with #c.Exporter.template_path. Uncomment it and add the path to nbconvert's templates:
    c.Exporter.template_path = ['/path/to/nbconvert/templates']
    
  3. Save the file, restart any running Jupyter sessions, then try the PDF conversion again.

4. Verify you have a full LaTeX distribution

Even if latex --version runs successfully, a minimal LaTeX install might lack packages nbconvert needs to generate PDFs. For Mac users, make sure you installed MacTeX (the full distribution) instead of BasicTeX. On Linux, install texlive-full (e.g., sudo apt-get install texlive-full on Ubuntu) to cover all required LaTeX components.


内容的提问来源于stack exchange,提问作者StanFlint

火山引擎 最新活动