-
Notifications
You must be signed in to change notification settings - Fork 598
Description
Hello,
I have seen a lot of requests for removing Section Numbering or adding a Table of Contents when using pdf conversion (or LaTeX).
The only solution I have seen so far is with a custom template.
Yet this seems to be too common a problem to demand this solution (the average academic user doesn't necessarily know to create his own template).
I propose that, similarly to the manner LatexPreprocessor works for author and title, we could add an injection
LatexPreprocessor.secnumdepth
This could add to the notebook metadata, for example, a:
'latex' : {'secnumdepth' : 0}
which can be later inferred from the Jinja Template.
This can be easily called from the CLI using:
jupyter nbconvert --to pdf {notebook_name}.ipynb \
--output-dir /content/ \
--LatexPreprocessor.title "{report_title}" \
--LatexPreprocessor.secnumdepth "0" \
--LatexPreprocessor.toc True
etc...
This will additionally open up a connection between the metadata and the Jinja template that can be exploited additionally by template creators, and open up a more dynamic templates interface that can also be accesible form the CLI.
*For the wide use-case of secnumdepth, we would need to slightly modify the base template.
Concretely:
Edit LatexPreprocessor to include a general property injector:
from traitlets import Dict
class LatexPreprocessor(Preprocessor):
# ... existing code ...
# The single "Gateway" for all future customizations
latex_params = Dict(
default_value={},
help="Dictionary of parameters to pass to the LaTeX template."
).tag(config=True)
def preprocess(self, nb, resources):
# ... existing code ...
# Ensure the 'latex' resource dict exists
if 'latex' not in resources:
resources['latex'] = {}
# Inject the entire dictionary into resources
# Users can now pass {'toc': True, 'secnumdepth': 0, 'my_custom_flag': 'xyz'}
resources['latex'].update(self.latex_params)
return nb, resources
Edit current base.tex.j2 to include toc and secnumdepth
...
((* block docclass *))
\documentclass{article}
% ... existing setup ...
% 1. Section Numbering Depth
% Allows users to pass {'secnumdepth': 0} to remove numbering
((* if resources.latex and resources.latex.get('secnumdepth') is not none *))
\setcounter{secnumdepth}{((( resources.latex.secnumdepth )))}
((* endif *))
% 2. Table of Contents Depth (Optional extra win)
((* if resources.latex and resources.latex.get('tocdepth') is not none *))
\setcounter{tocdepth}{((( resources.latex.tocdepth )))}
((* endif *))
((* endblock docclass *))
...
...
((* block maketitle *))
\maketitle
% 3. Optional Table of Contents
((* if resources.latex and resources.latex.get('toc', False) *))
\tableofcontents
\newpage
((* endif *))
((* endblock maketitle *))
...
Further behavior was described here:
Notbook Issue 7722