5

I am using Jupyter Notebook to write up reports very heavy in code and LaTeX. I wish to change the title, date, and the format of them. Precisely, I would like all the text to be in a smaller size, 12 to 14px at the largest.

I've spent a few days unsuccessfully trying to create custom *.tplx files for nbconvert. Regardless of the file I use, the title and date remain unchanged. In fact, nothing seems to change when I use a custom .tplx.

I have found a couple other people asking about this which is where I found the information about templates and how to call them when using nbconvert. Every complete solution involved exporting as a '.tex' then making the changes manually. This is unideal as I often create several documents a day and that would slow me down a lot as opposed to a one-time solution.

Any suggestions?

Tyler
  • 51
  • 1
  • 3

2 Answers2

5

Ok, I hacked together some code that accomplished the date portion of this. (Hugely helped by this blog.) A similar edit would work to change the title and/or author. I'm not sure how robust this is but it did the trick.

Basically, you will need to edit the tplx files in your /nbconvert/templates/latex directory. Specifically, I edited base.tplx. Where it had

 ((* block date *))((* endblock date *))

I changed this to

    ((* block date *))  
    ((*- if nb.metadata["latex_metadata"]: -*))  
    ((*- if nb.metadata["latex_metadata"]["date"]: -*))  
        \date{((( nb.metadata["latex_metadata"]["date"] )))}  
    ((*- endif *))  
    ((*- endif *))  
    ((* endblock date *))  

Then in the notebook metadata, I added

  "latex_metadata": {  
      "date": "CUSTOM DATE HERE"  
  },  

The , at the very end depends on if it is the last line in your metadata structure. If no latex_metadata tag exists, it leaves the date empty and latex substitutes the current date.

The only issue I haven't been able to fix is that when I run Download as -> PDF via LaTeX, it doesn't always rerun the LaTeX generator (sometimes it just gives me the LAST pdf it created, not a new one.) I can't figure out why it runs it sometimes and doesn't othertimes.

*Edit* My exact metadata section in a jupyter notebook now looks like this (I'm using the date field to show a due date for my students):

{
  "anaconda-cloud": {},
  "kernelspec": {
    "name": "python3",
    "display_name": "Python 3",
    "language": "python"
  },
  "language_info": {
    "name": "python",
    "version": "3.7.9",
    "mimetype": "text/x-python",
    "codemirror_mode": {
      "name": "ipython",
      "version": 3
    },
    "pygments_lexer": "ipython3",
    "nbconvert_exporter": "python",
    "file_extension": ".py"
  },
  "latex_metadata": {
    "date": "Due April 7 at 11:59pm"
  }
}
Seth
  • 51
  • 4
  • For anyone who might stumble upon this answer... after updating nbconvert at some point, I found it no longer used `base.tplx`. It now uses `base.tex.j2` located (for me) in `share/jupyter/nbconvert/templates/latex` under your anaconda installation. Same solution as above still works though. – Seth Jan 18 '21 at 20:13
  • Does this hack still work for you? Have searched for a whole day on how to get this to work, followed what you have listed here and it's still not playing ball :-/ – microhaus Apr 04 '21 at 21:28
  • Yes, after updating to modifying `base.tex.j2` instead of the deprecated `base.tplx`, it works flawlessly. (In fact, the very last line of my original post of not rerunning the LaTeX generator is no longer an issue as it works perfectly every time.) – Seth Apr 05 '21 at 22:05
  • Would it be possible for you to edit your answer and post what your Jupyter metadata on a new notebook looks like *after* you have your snippet? I have followed your post to the letter and it didn't work, so currently my hunch is that I may have made a mistake with `.json` syntax, which I am illiterate with. Failing that I am guessing it may be due to some idiosyncracies of my setup perhaps. Am currently using the Jupyter save as `.tex` followed by manual edit, which is not ideal. – microhaus Apr 05 '21 at 23:42
  • Thank you for taking the time to edit your answer, it is greatly appreciated. I will give it another shot. – microhaus Apr 07 '21 at 01:23
0

Thanks for the useful input. The same operation can be done to customise also title and author. I edited the proper part in base.tex.j2 as below, and added the appropriate fields in the metadata, below the date field

((* block title -*))
((*- if nb.metadata["latex_metadata"]: -*))  
((*- if nb.metadata["latex_metadata"]["title"]: -*))  
    \title{((( nb.metadata["latex_metadata"]["title"] )))}  
((*- endif *))  
((*- endif *))  
((*- endblock title *))
((* block date *))  
((*- if nb.metadata["latex_metadata"]: -*))  
((*- if nb.metadata["latex_metadata"]["date"]: -*))  
    \date{((( nb.metadata["latex_metadata"]["date"] )))}  
((*- endif *))  
((*- endif *))  
((* endblock date *))  
((* block author *))
((*- if nb.metadata["latex_metadata"]: -*))  
((*- if nb.metadata["latex_metadata"]["author"]: -*))  
    \author{((( nb.metadata["latex_metadata"]["author"] )))}  
((*- endif *))  
((*- endif *))  
((* endblock author *))