4

I'm trying to create a nbconvert (6.x) template that slightly modifies the default latex template.

I created a "no_header" template in <userprofile>\Miniconda3\share\jupyter\nbconvert\templates\no_header:

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

The conf.json contains:

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

The no_header.tex.j2 contains:

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

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

I took the block name packages from the style_jupyter.tex.j2. This section contains some usepackage commands.

I then created a simple notebook and tried using the template to convert it to tex via the command:

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

The command successfully creates the tex output - But it does not seem to use the template. The output is the exact same if I omit the template parameter to the cli call. Also, the string asdf is not present in the generated file, further indicating that the template was not activated.

When running the above command with --debug, the no_header template is listed under the 'template paths' listing. Also, when I misspelled the template name I got an error, so I think the call does find the template, but somehow does not use it.

What I tried so far:

  • All the tutorials online only describe the old template format (single files)
  • The documentation of nbconvert does not contain an mwe
  • For the approach described above, I followed this github issue.

Can anyone help me here?

LcdDrm
  • 1,009
  • 7
  • 14

1 Answers1

1

The conf.json file is fine, but I think you need to call your entrypoint index.tex.j2. The name of the template is defined by the directory, not the .j2 file name.

Then in your no_header/index.tex.j2, you then specify which template you want to inherit from, e.g. borrowing from the standard latex article you start like this:

((=- 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 -*))

and to customize the packages section, you then add as in the question:

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

Presumably you'll want to put a lot more in the "packages" block. If you want to add to certain blocks rather than override, include a super() call.

The default latex template directory has a number of files which add quite a lot of latex commands; you can track the inheritance chain (in the above example starting from style_jupyter.tex.j2) to see what all the different blocks are called so you can tweak them as needed (e.g. "margins").

stefandw
  • 36
  • 3
  • Does this work? – faceclean Apr 15 '22 at 01:34
  • 1
    it does for me (nbconvert version 6.3.0). The point is that the directory name determines the template name, your content needs to go into a file called index.tex.j2 and the conf.json file should be as suggested in the question. You don't need other files in your custom template directory. – stefandw Apr 22 '22 at 19:02