python - 通过 IPython Notebook 转换更改 PDF-Latex 输出的样式

标签 python pdf latex type-conversion ipython

目前我的 .ipynb file 使用以下命令:

$ ipython nbconvert --to latex --post PDF Untitled1.ipynb --SphinxTransformer.author="John Doe"
[NbConvertApp] Using existing profile dir: u'/Users/me-macmini/.ipython/profile_default'
[NbConvertApp] Converting notebook Untitled1.ipynb to latex
[NbConvertApp] Support files will be in Untitled1_files/
[NbConvertApp] Loaded template article.tplx
[NbConvertApp] Writing 12876 bytes to Untitled1.tex
[NbConvertApp] Building PDF
[NbConvertApp] Running pdflatex 3 times: [u'pdflatex', u'Untitled1.tex']
[NbConvertApp] Running bibtex 1 time: [u'bibtex', u'Untitled1']
[NbConvertApp] WARNING | bibtex had problems, most likely because there were no citations
[NbConvertApp] Removing temporary LaTeX files
[NbConvertApp] PDF successfully created

使用 IPython 2.1,我得到了使用标准经典样式格式化的 latex 文件:

enter image description here

我的问题是:

  1. 我应该怎么做才能直接从 ipython 命令获得以下样式? enter image description here

  2. 为什么上面的命令不能使作者出现?

最佳答案

与 OP 一样,我对 nbconvertIPython 2 输出不太满意。由于转换器不再使用 Sphinx 文档类或 Sphinx 预处理系统,因此您不能在 nbconverter 行上使用 SphinxTransformer 调用。

粗鲁无礼的方式

删除 --post PDF 以便 nbconvert 只创建 .tex 文件。然后,编辑 .tex 文件使其更漂亮。然后,在上面运行 pdflatex 几次。

要使您自己成为作者,请在 .tex 文件中的 \title 行之后添加如下一行:

\author{Never Saint}

您可以在 latextemplates.com 找到漂亮的模板来帮助您使输出看起来像您想要的。

根用户的方式

另一种方法是滚动一个新模板,从 .../IPython/nbconvert/templates/latex 中的模板开始。作为 root 用户,在 article.tplxreport.tplx 旁边添加一个 article1.tplx 文件。以下版本创建了一种我个人认为有用的不同输出样式。 "margins" block 为 LaTex 生成 front matter,而 "predoc" block 生成插入文档开头的命令和文本。我将“maketitle” block 清空,因此没有标题页。如果你想要一个带有作者和日期的标题页,请删除我的空“maketitle” block 。

用法:nbconvert --to latex yourNotebook.ipynb --template article1 --to PDF

% Default to the notebook output style
((* if not cell_style is defined *))
    ((* set cell_style = 'style_ipython.tplx' *))
((* endif *))

% Inherit from the specified cell style.
((* extends cell_style *))


%===============================================================================
% Latex article1, based on Article 
%===============================================================================

((* block docclass *))
\documentclass{article}
((* endblock docclass *))
((* block margins *))
\usepackage{blindtext}
\usepackage{mathptmx}% Times Roman font
\usepackage[scaled=.90]{helvet}
\usepackage{xcolor}
\usepackage{titlesec}
\titleformat{\title}[display]
  {\normalfont\sffamily\huge\bfseries\color{blue}}
  {\titlename}{20pt}{\Huge}
\titleformat{\section}
  {\normalfont\sffamily\Large\bfseries\color{darkgray}}
  {\subsection}{1em}{} 
\titleformat{\subsection}
  {\normalfont\sffamily\Large\bfseries\color{darkgray}}
  {\thesubsection}{1em}{} 
\parindent=0pt
\parskip=6pt
((* endblock margins *))

((* block predoc *))
\begin{document}

\pagestyle{plain}
\thispagestyle{plain}
\pagenumbering{arabic}
\setcounter{page}{5}
\lfoot{\copyright 2014}

This document is composed as an
\href{http://ipython.org/notebook.html}{IPython} notebook. The printed
version of this document was generated from the \texttt{IPython}
notebook, using the \texttt{ipython nbconvert} utility to generate a
\texttt{Latex} document. 
((* block maketitle *))((* endblock maketitle *))
((* block author *))\author{Put Your Name Here}((* endblock author *))
((* endblock predoc *))

关于python - 通过 IPython Notebook 转换更改 PDF-Latex 输出的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24624401/

相关文章:

当我传递 2 个参数时,Python 要求 3 个参数;当我传递 3 个参数时,Python 要求 2 个参数

python - 在 python 中将 excel 文件中选定的工作表打印为 pdf

javascript - 如何在 Phonegap 的 div 容器内显示 PDF?

r - 如何在 R-Markdown 的 R block 中使用 LaTeX 代码?

Vim - 如何使用 autocmd 和模板文件在新文件的行首插入反斜杠

latex - 如何将 .rtf 或 .doc 文档转换为 LaTeX?

python - 扩展布局模板时 Jinja 不渲染任何内容

python - 是否可以在 Google App Engine 中使用 PyMongo?

pdf - latex 制品类的纸张尺寸问题

python - Numba `cache=True ` 无效