Julia环境下Jupyter导出PDF无法显示Unicode字符(如λ)求助
I’ve run into this exact issue before—Jupyter’s default PDF export can trip up on Unicode characters like Greek letters in Julia code cells, even though they display perfectly fine in the notebook itself. Here’s how to fix it while keeping the clean, professional PDF layout you get from Jupyter’s standard export:
Why This Happens
Jupyter converts notebooks to PDF via LaTeX under the hood. The default LaTeX setup (using pdflatex) has limited support for some Unicode characters like λ, so it drops them silently during compilation. Nteract works because it likely uses a Unicode-friendly LaTeX engine (like xelatex or lualatex) out of the box, but its PDF styling might not match what you need for reports or submissions.
Solution 1: Quick Command-Line Fix
If you’re comfortable using the terminal, this is the fastest way to get your λ back:
- Run this command to export your notebook with a Unicode-compatible LaTeX engine:
jupyter nbconvert --to pdf --pdf-engine=xelatex your_notebook.ipynbxelatexhandles UTF-8 characters natively, so yourλ=3will show up exactly as it does in the notebook.
Solution 2: Set Default PDF Engine for UI Exports
If you prefer using Jupyter’s UI to export PDFs, configure it to use xelatex by default:
- Create or edit the nbconvert config file:
- On Linux/macOS:
~/.jupyter/jupyter_nbconvert_config.py - On Windows:
%APPDATA%\jupyter\jupyter_nbconvert_config.py
- On Linux/macOS:
- Add this line to the file:
c.PDFExporter.pdf_engine = 'xelatex' - Save the file and restart Jupyter. Now when you export via the UI, your Greek letters will be preserved.
Solution 3: Advanced Template Customization
If you want to tweak the PDF appearance while fixing the character issue:
- Locate Jupyter’s LaTeX templates (create the folder if it doesn’t exist):
- Linux/macOS:
~/.jupyter/nbconvert/templates/latex/ - Windows:
%APPDATA%\jupyter\nbconvert\templates\latex
You can copy the default templates from nbconvert’s installation directory (find it withjupyter --paths).
- Linux/macOS:
- Edit the
base.tplxorarticle.tplxfile, and add these lines to the LaTeX preamble section:\usepackage{fontspec} \setmainfont{Latin Modern Roman} - Save the template and export using
xelatex(either via command line or the configured UI). This ensures Unicode support while keeping Jupyter’s standard PDF styling.
Bonus: Double-Check Notebook Encoding
Jupyter defaults to UTF-8 encoding, but it’s worth confirming:
- Go to
File > Download as > Notebook (.ipynb)and open the downloaded file in a text editor like VS Code to verify it uses UTF-8.
内容的提问来源于stack exchange,提问作者ecjb

