求助:Jupyter Notebook中Markdown插入图片并导出PDF的方法及相关问题解决
Alright, let's tackle your two issues one by one—first fixing that broken PDF export when using Markdown images, then tweaking the output style to feel more like a polished article.
1. Fixing Broken PDF Exports with Markdown Images
The problem here is that Jupyter stores images added via  as internal notebook attachments, which the LaTeX-based nbconvert tool often struggles to parse correctly, leading to corrupted PDFs. Here are three reliable fixes:
Use absolute/relative file paths instead of attachments
Move your image file to the same directory as your notebook (or a subfolder like./images/), then update your Markdown to reference the file directly:For images in another folder, use a relative path like
./images/image.pngor a full absolute path (note Windows paths need forward slashes or double backslashes:C:/Users/my_folder/image.png). This lets the LaTeX exporter locate the image directly without relying on internal attachments.Adjust nbconvert export parameters
If you want to stick with LaTeX export, run the export via command line with this flag to ensure proper handling of resources:jupyter nbconvert --to pdf your_notebook.ipynb --allow-chromium-downloadAlternatively, you can switch to exporting via Chromium (which converts the notebook to HTML first, then to PDF) by using:
jupyter nbconvert --to pdf --webpdf your_notebook.ipynbThis method often handles embedded content more reliably than pure LaTeX.
Clear notebook output and re-export
Sometimes cached outputs or corrupted attachment data cause issues. Clear all outputs first:jupyter nbconvert --clear-output your_notebook.ipynbReopen the notebook, re-run any necessary cells, then try exporting to PDF again.
2. Styling the Exported PDF to Resemble a Proper Article
To ditch the gray code cell boxes and get a more article-like layout, you have a few options depending on whether you prefer LaTeX or HTML-based exports:
Option 1: Customize the LaTeX Template (for LaTeX-based PDF)
- First, export the default LaTeX template to edit:
jupyter nbconvert --show-template latex > custom_article_template.tplx - Open
custom_article_template.tplxin a text editor and make these changes:- Remove code cell gray backgrounds: Look for sections related to code blocks (usually using
lstlistingorVerbatimenvironments). Find lines settingbackgroundcolorand delete them, or set the color to white (\color{white}). - Adjust page layout: Add LaTeX packages to tweak margins, font, and spacing. For example, add this at the top of the template to set 1-inch margins:
\usepackage{geometry} \geometry{margin=1in} - Remove code cell borders: If there's a border around code blocks, find the corresponding
frameorbordersettings and disable them.
- Remove code cell gray backgrounds: Look for sections related to code blocks (usually using
- Export your notebook using the custom template:
jupyter nbconvert --to pdf your_notebook.ipynb --template custom_article_template.tplx
Option 2: Use HTML-to-PDF for More Flexible Styling
- Export your notebook to HTML first:
jupyter nbconvert --to html your_notebook.ipynb - Open the HTML file in a text editor and modify the CSS:
- Find the
.code_cellor.highlightclasses and setbackground-color: transparent;andborder: none;to remove gray boxes and borders. - Adjust other styles like font family, line spacing, or page margins to match article formatting.
- Find the
- Convert the modified HTML to PDF:
- Use Chrome's print function (Ctrl+P) and select "Save as PDF" for a quick option.
- Or use pandoc for command-line conversion:
pandoc your_notebook.html -o your_notebook.pdf
Option 3: Hide Code Cells Entirely (If You Don't Need Them)
If you only want to show your content (not code), export with the --no-input flag to hide all code cells:
jupyter nbconvert --to pdf your_notebook.ipynb --no-input
This instantly gives you a clean, article-like PDF focused on your text and images.
内容的提问来源于stack exchange,提问作者JCV

