安装 MikTex、pandoc 和 nbconvert 后将 Jupyter Notebook 转换为 PDF 时出错

Posted

技术标签:

【中文标题】安装 MikTex、pandoc 和 nbconvert 后将 Jupyter Notebook 转换为 PDF 时出错【英文标题】:Error Converting Jupyter Notebook to PDF after installing MikTex, pandoc and nbconvert 【发布时间】:2020-09-24 04:50:51 【问题描述】:

我一直在尝试将我的 Jupyter Notebook 转换为 PDF(通过 LaTeX)。 我已经在 MikTeX 控制台、pandoc 和 nbconvert 中安装了所有软件包。 但是,我仍然看到以下错误。

非常感谢您的建议!

MikTeX packages all installed and updated

Pandoc installed

当我在 anaconda 提示符下运行代码并单击 File->Download as-> PDF via LaTeX (.pdf) 时出错:

(base) C:\Users\justm>jupyter nbconvert notebook.ipynb --to pdf
[NbConvertApp] Converting notebook notebook.ipynb to pdf
[NbConvertApp] Support files will be in notebook_files\
[NbConvertApp] Making directory .\notebook_files
[NbConvertApp] Making directory .\notebook_files
[NbConvertApp] Making directory .\notebook_files
[NbConvertApp] Making directory .\notebook_files
[NbConvertApp] Making directory .\notebook_files
[NbConvertApp] Making directory .\notebook_files
[NbConvertApp] Writing 66012 bytes to .\notebook.tex
[NbConvertApp] Building PDF
[NbConvertApp] Running xelatex 3 times: ['xelatex', '.\\notebook.tex', '-quiet']
[NbConvertApp] CRITICAL | x failed: xelatex .\notebook.tex -quiet
notebook.tex:385: Unable to load picture or PDF file '
notebook.tex:404: Missing number, treated as zero
notebook.tex:404: Illegal unit of measure (
notebook.tex:404: Missing number, treated as zero
notebook.tex:404: Illegal unit of measure (
notebook.tex:490: Missing number, treated as zero
notebook.tex:490: Illegal unit of measure (
notebook.tex:490: Missing number, treated as zero
notebook.tex:490: Illegal unit of measure (
notebook.tex:600: Missing number, treated as zero
notebook.tex:600: Illegal unit of measure (
notebook.tex:600: Missing number, treated as zero
notebook.tex:600: Illegal unit of measure (
notebook.tex:896: Missing number, treated as zero
notebook.tex:896: Illegal unit of measure (
notebook.tex:896: Missing number, treated as zero
notebook.tex:896: Illegal unit of measure (
notebook.tex:1007: Unable to load picture or PDF file '
Traceback (most recent call last):
  File "c:\users\justm\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\justm\anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\justm\anaconda3\Scripts\jupyter-nbconvert.EXE\__main__.py", line 7, in <module>
  File "c:\users\justm\anaconda3\lib\site-packages\jupyter_core\application.py", line 268, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "c:\users\justm\anaconda3\lib\site-packages\traitlets\config\application.py", line 664, in launch_instance
    app.start()
  File "c:\users\justm\anaconda3\lib\site-packages\nbconvert\nbconvertapp.py", line 340, in start
    self.convert_notebooks()
  File "c:\users\justm\anaconda3\lib\site-packages\nbconvert\nbconvertapp.py", line 510, in convert_notebooks
    self.convert_single_notebook(notebook_filename)
  File "c:\users\justm\anaconda3\lib\site-packages\nbconvert\nbconvertapp.py", line 481, in convert_single_notebook
    output, resources = self.export_single_notebook(notebook_filename, resources, input_buffer=input_buffer)
  File "c:\users\justm\anaconda3\lib\site-packages\nbconvert\nbconvertapp.py", line 410, in export_single_notebook
    output, resources = self.exporter.from_filename(notebook_filename, resources=resources)
  File "c:\users\justm\anaconda3\lib\site-packages\nbconvert\exporters\exporter.py", line 179, in from_filename
    return self.from_file(f, resources=resources, **kw)
  File "c:\users\justm\anaconda3\lib\site-packages\nbconvert\exporters\exporter.py", line 197, in from_file
    return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw)
  File "c:\users\justm\anaconda3\lib\site-packages\nbconvert\exporters\pdf.py", line 185, in from_notebook_node
    self.run_latex(tex_file)
  File "c:\users\justm\anaconda3\lib\site-packages\nbconvert\exporters\pdf.py", line 156, in run_latex
    self.latex_count, log_error, raise_on_failure)
  File "c:\users\justm\anaconda3\lib\site-packages\nbconvert\exporters\pdf.py", line 145, in run_command
    command=command, output=out))
nbconvert.exporters.pdf.LatexFailed: PDF creating failed, captured latex output:
Failed to run "xelatex .\notebook.tex -quiet" command:
notebook.tex:385: Unable to load picture or PDF file '
notebook.tex:404: Missing number, treated as zero
notebook.tex:404: Illegal unit of measure (
notebook.tex:404: Missing number, treated as zero
notebook.tex:404: Illegal unit of measure (
notebook.tex:490: Missing number, treated as zero
notebook.tex:490: Illegal unit of measure (
notebook.tex:490: Missing number, treated as zero
notebook.tex:490: Illegal unit of measure (
notebook.tex:600: Missing number, treated as zero
notebook.tex:600: Illegal unit of measure (
notebook.tex:600: Missing number, treated as zero
notebook.tex:600: Illegal unit of measure (
notebook.tex:896: Missing number, treated as zero
notebook.tex:896: Illegal unit of measure (
notebook.tex:896: Missing number, treated as zero
notebook.tex:896: Illegal unit of measure (
notebook.tex:1007: Unable to load picture or PDF file '

【问题讨论】:

【参考方案1】:

相当老的问题,但我也偶然发现了它。问题是 LaTeX 代码中的某些内容需要 calc 包,但该包并未导入。

在我的例子中,笔记本包含的表格包含列的计算,例如这个

\beginlongtable[]@>\raggedright\arraybackslashp(\columnwidth - 10\tabcolsep) * \real0.17lllll@

这是由 pandoc 2.11.2 引入的,带有 this commit。但是,当我使用自定义模板文件时,它错过了\usepackagecalc。添加后效果很好。它也不包含在当前的 nbconvert 包中(请参阅#1566)。

另请参阅this question 和this thread in pandoc-discuss。

【讨论】:

以上是关于安装 MikTex、pandoc 和 nbconvert 后将 Jupyter Notebook 转换为 PDF 时出错的主要内容,如果未能解决你的问题,请参考以下文章

从MikTex(通过Pandoc)写出表格字段,iTextSharp可以解析

windows使用Pandoc将Markdown转换为PDF文件

如何把 Markdown 文件转化为 PDF

如何把 Markdown 文件转化为 PDF

jupyter中,ipynb文件转pdf文件

如何把 Markdown 文件转化为 PDF