Search code examples
jupyter-notebookjinja2pandocnbconvert

Change formatting behavior for a URL in nbconvert


I want to convert a Jupyter notebook to LaTeX using nbconvert. The default exporting behavior is to convert Jupyter hypertext links

[a link](http://some.website.com)

to a LaTeX string that can be rendered as a link in a PDF document:

\href{http://some.website.com}{a link}

I would like to change this behavior, so that links are rendered instead as footnotes:

a link\footnote{http://some.website.com}

What do I have to modify to do this? I've looked at the documentation for nbconvert but haven't been able to figure it out. Is it possible to do this within a .tplx template file? I've looked at the standard template files and don't see anything defining the URL behavior, so I'm guessing it's handled by pandoc somehow, but I'm confused about where I need to change something.


Solution

  • You can use LaTeX to redefine the \href command. Put the following in your template or using header-includes:

    \let\oldhref=\href
    \renewcommand{\href}[2]{\footnote{\oldhref{#1}{#2}}}
    

    Alternatively, you could write a pandoc filter to rewrite the actual output to a RawInline "latex" "\footnote"...