如何解决Matplotlib的RuntimeError:LaTeX未找到致文本处理失败
Fixes for Matplotlib TeX Errors in Jupyter/VS Code (Windows 11 + Anaconda)
1. Fix PATH inheritance for GUI apps
Even if you added MikTeX to your system PATH, GUI applications like Jupyter (launched via shortcut) or VS Code often don't pick up updated PATH variables until you restart your machine.
- Restart your computer to ensure the new PATH is loaded across all apps.
- For VS Code: If restarting doesn't work, launch it directly from the Anaconda Prompt where your code runs successfully—this guarantees it inherits the correct environment variables.
- For Jupyter shortcuts: A reliable workaround is to launch Jupyter from the Anaconda Prompt instead of using the shortcut. If you prefer the shortcut, edit its target to include the MikTeX path explicitly (e.g., prepend
C:\Program Files\MiKTeX\miktex\bin\x64;to the existing target command).
2. Resolve missing cmr10.tfm font file
This error happens because the Computer Modern font package containing cmr10.tfm isn't installed. Fix it with MikTeX:
- Open MikTeX Console.
- Navigate to Settings > Packages.
- Set "Install missing packages on-the-fly" to Yes.
- Restart Jupyter and run your code again—MikTeX will automatically download and install the required font package.
3. Force Matplotlib to use MikTeX explicitly
If PATH issues persist, tell Matplotlib exactly where your MikTeX latex.exe is located by adding these lines to your code:
import matplotlib.pyplot as plt plt.rcParams.update({ 'text.usetex': True, 'pgf.texsystem': 'miktex', # Replace with your actual MikTeX latex.exe path 'text.latex.command': r'C:\Program Files\MiKTeX\miktex\bin\x64\latex.exe' })
Adjust the path to match your MikTeX installation directory (it might be in C:\Program Files\MiKTeX or C:\Users\<YourName>\AppData\Local\Programs\MiKTeX).
4. Confirm Anaconda environment consistency
- Ensure Jupyter/VS Code is using the same Anaconda environment where your code works in the prompt.
- In Jupyter, check the kernel name (top-right corner) to verify it's the correct environment.
- In VS Code, select the appropriate Python interpreter from your Anaconda environment in the bottom-left corner of the window.
内容的提问来源于stack exchange,提问作者TheHunter
相关产品推荐
相关产品推荐

