【问题标题】:nbconvert 6: How to customize Latex templatenbconvert 6:如何自定义 Latex 模板
【发布时间】:2021-06-17 08:44:01
【问题描述】:

我正在尝试创建一个稍微修改默认乳胶模板的 nbconvert (6.x) 模板。

我在<userprofile>\Miniconda3\share\jupyter\nbconvert\templates\no_header中创建了一个“no_header”模板:

.
+-- no_header
|   +-- conf.json
|   +-- no_header.tex.j2

conf.json 包含:

{
    "base_template": "latex",
    "mimetypes": {
        "text/latex": true,
        "text/tex": true,
        "application/pdf": true
    }
}

no_header.tex.j2 包含:

((*- extends 'latex' -*))

((*- block packages -*))
asdf
((* endblock packages *))

我从style_jupyter.tex.j2 中取了块名称packages。本节包含一些 usepackage 命令。

然后我创建了一个简单的笔记本并尝试使用模板通过命令将其转换为 tex:

jupyter nbconvert --to=latex --template=no_header my_nb.ipynb

该命令成功创建了 tex 输出 - 但它似乎没有使用模板。如果我在 cli 调用中省略模板参数,则输出完全相同。另外,生成的文件中没有字符串asdf,进一步说明模板没有激活。

当使用--debug 运行上述命令时,no_header 模板列在“模板路径”列表下。另外,当我拼错模板名称时,我得到了一个错误,所以我认为调用确实找到了模板,但不知何故没有使用它。

到目前为止我尝试了什么:

  • 网上所有的教程都只描述了旧的模板格式(单个文件)
  • nbconvert 的文档不包含 mwe
  • 对于上述方法,我关注了this github issue。

谁能帮帮我?

【问题讨论】:

  • 你找到调整 LaTeX 模板的方法了吗?我想让表格环境字体更小。

标签: python jupyter nbconvert


【解决方案1】:

conf.json 文件很好,但我认为你需要调用你的入口点 index.tex.j2。模板的名称由目录定义,而不是.j2 文件名。

然后在你的 no_header/index.tex.j2 中,指定你想要继承的模板,例如借用标准的乳胶文章,你可以这样开始:

((=- Default to the notebook output style -=))
((*- if not cell_style is defined -*))
    ((* set cell_style = 'style_jupyter.tex.j2' *))
((*- endif -*))

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

((*- block docclass -*))
\documentclass[11pt]{article}
((*- endblock docclass -*))

并自定义包部分,然后在问题中添加:

((*- block packages -*))
asdf
((* endblock packages *))

大概您会想在“包”块中添加更多内容。如果您想添加到某些块而不是覆盖,请包含 super() 调用。

默认的latex模板目录有很多文件,其中添加了相当多的latex命令;您可以跟踪继承链(在上面的示例中,从 style_jupyter.tex.j2 开始)以查看所有不同块的调用情况,以便您可以根据需要调整它们(例如“边距”)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多