Skip to content

Instantly share code, notes, and snippets.

@binary10
Last active November 29, 2022 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binary10/20b632dc8c3f5a3c9a1b7266aa016128 to your computer and use it in GitHub Desktop.
Save binary10/20b632dc8c3f5a3c9a1b7266aa016128 to your computer and use it in GitHub Desktop.
How can I export PDFs with custom LaTeX templates using the export button in cocalc?

The problem

Cocalc exports notebooks to LaTeX using a default provided by nbconvert when File > PDF via LaTeX is clicked in the menu.

I wanted to be able to customize the default LaTeX settings (found at /ext/jupyter/nbconvert/templates/latex), however this contains read-only files on the system. So, I created a local config, but they do not automatically override the system settings. After interpreting the docs, I found how to override the system default.

The recipe

  1. First, copy the folder /ext/jupyter/nbconvert/templates/latex to /home/user/.local/share/jupyter/nbconvert/templates/latex.
mkdir -p /home/user/.local/share/jupyter/nbconvert/templates/
cp -r /ext/jupyter/nbconvert/templates/latex /home/user/.local/share/jupyter/nbconvert/templates/
  1. Create a config file at .jupyter/jupyter_nbconvert_config.json with the following contents:
  "version": 1,
  "Exporter": {
    "template_path": [
      "/home/user/.local/share/jupyter",
      "/home/user/.nbconvert",
      "."
    ],
    "template_paths": ["/home/user/.local/share/jupyter/nbconvert/templates/latex"],
    "preprocessors": [
      "jupyter_contrib_nbextensions.nbconvert_support.CodeFoldingPreprocessor",
      "jupyter_contrib_nbextensions.nbconvert_support.PyMarkdownPreprocessor"
    ]
  }
}

The result

Now you will be able to edit the config files for LaTeX documents in /home/user/.local/share/jupyter/nbconvert/templates/latex and the cocalc system will use those settings when you click File > PDF via LaTeX. It also appears that defaults for other export types are not broken by this process, only the LaTeX export is changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment