导出笔记本时隐藏 stderr 块

Hide stderr blocks when exporting notebooks

我有一个自定义模板,用于从我的 Jupyter 笔记本导出到 LaTeX。

((*- extends 'article.tplx' -*))

% Disable input cells
((* block input_group *))
((* endblock input_group *))

这工作正常,但有时笔记本中的 Python 代码会产生对笔记本有用但对导出没有帮助的警告和错误。例如,在一个笔记本中有这个块:

  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\Users\me\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: FutureWarning: currently extract(expand=None) means expand=False (return Index/Series/DataFrame) but in a future version of pandas this will be changed to expand=True (return DataFrame)\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    }
   ],
   "source": [
    "# Some Python code using Pandas\n",
    "df = d.join(df.mycol.str.extract((?P<foo>\d)')\n",
    ")"
   ]
  }

我想在导出时排除此类错误。我尝试将以下行添加到我的模板中:

% Hide Stderr output
((* block stream_stderr *))
((* endblock stream_stderr *))

但是,我仍然看到相应的错误出现在 LaTeX 输出中。我做错了什么?

我发现自己遇到了同样的问题并想出了一个解决办法。将其放入您的模板文件中:

((* block stream *))
((*- if output.name == 'stderr' -*))
((*- else -*))
    \begin{Verbatim}[commandchars=\\{\}]
((( output.text | escape_latex | ansi2latex )))
    \end{Verbatim}
((*- endif -*))
((* endblock stream *))