2

I have seen many posts on how to use a custom template when rendering LaTeX from a jupyter notebook (eg hide code in jupyter notebook) and have successfully implemented the answer described here (custom template location) eg:

~/dev/notebooks$ jupyter nbconvert --to=latex filename.ipynb --Application.log_level='DEBUG'

works fine (and applies the custom template specified in my ~/.jupyter/jupyter_nbconvert_config.py).

But how can I set the jupyter web UI to function the same way using:

File->Download as->LaTeX (.tex)

Thanks in advance.

w15p
  • 39
  • 6
  • what happens when you use the File->Download as->LaTeX (.tex)? DO you get any kind of error message? – MrE Oct 25 '18 at 20:49
  • there is no error message in the UI. I'm working to get access to the logs. – w15p Oct 25 '18 at 23:24

1 Answers1

1

I found the solution jupyter_notebook_config.py. Basically, the jupyter application determines the applied template, which is why it was working fine with:

jupyter nbconvert ...

but was not working with:

File->Download as->LaTeX (.tex)

So, by using this ~/.jupyter/jupyter_notebook_config.py:

import os

c = get_config()

c.TemplateExporter.template_path = [os.path.expanduser('~/.jupyter/templates'), \
'/usr/local/bin/miniconda3/envs/MyEnv/lib/python3.6/site-\
packages/jupyter_contrib_nbextensions/templates', '.']
c.LatexExporter.template_file = 'custom_latex.tplx'

and placing a 'custom_latex.tplx' in the base template directory. I can set a default template (that does nothing) and then each user can override that default template as they choose (this is for a jupyter hub installation). If you don't need this generic behavior (ie you aren't on a jupyter hub), you can ignore the default template portion entirely.

For completeness...

default template:

((= Nbconvert custom style for LaTeX export =))

((*- extends 'article.tplx' -*))

%==============================================================================\
=
% Custom definitions
%==============================================================================\
=
((* block definitions *))
    ((( super() )))

    % Pygments definitions
    ((( resources.latex.pygments_definitions )))

    % Exact colors from NB
    \definecolor{incolor}{rgb}{0.0, 0.0, 0.5}
    \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0}

    % Don't number sections
    \renewcommand{\thesection}{\hspace*{-0.5em}}
    \renewcommand{\thesubsection}{\hspace*{-0.5em}}

((* endblock definitions *))

% No title
((* block maketitle *))((* endblock maketitle *))



%==============================================================================\
=
% Latex Article
%==============================================================================\
=
% You can customize your LaTeX document here, e.g. you can
% - use a different documentclass like
%   \documentclass{report}
% - add/remove packages (like ngerman)

((* block docclass *))
% !TeX spellcheck = de_DE
% !TeX encoding = UTF-8
\documentclass{article}
((* endblock docclass *))

personal template:

((= Nbconvert custom style for LaTeX export =))

((*- extends 'nbextensions.tplx' -*))

%==============================================================================\
=
% Custom definitions
%==============================================================================\
=
((* block definitions *))
    ((( super() )))

    % Pygments definitions
    ((( resources.latex.pygments_definitions )))

    % Exact colors from NB
    \definecolor{incolor}{rgb}{0.0, 0.0, 0.5}
    \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0}

    % Don't number sections
    \renewcommand{\thesection}{\hspace*{-0.5em}}
    \renewcommand{\thesubsection}{\hspace*{-0.5em}}

((* endblock definitions *))

% No title
((* block maketitle *))((* endblock maketitle *))



%==============================================================================\
=
% Latex Article
%==============================================================================\
=
% You can customize your LaTeX document here, e.g. you can
% - use a different documentclass like
%   \documentclass{report}
% - add/remove packages (like ngerman)

((* block docclass *))
% !TeX spellcheck = de_DE
% !TeX encoding = UTF-8
\documentclass{article}
% this is all unnecessary when extending nbextensions.tplx + hide_input_all
\usepackage{verbatim} %for {comment}
\usepackage{fancyvrb}
\renewenvironment{Verbatim}{\comment}{\endcomment}
((* endblock docclass *))

Hope this helps someone else - I don't think the documentation is clear on this point at all.

w15p
  • 39
  • 6