Skip to content

Instantly share code, notes, and snippets.

@parthi2929
Created September 25, 2018 18:02
Show Gist options
  • Select an option

  • Save parthi2929/809f99a616b97ffe947df148cf525989 to your computer and use it in GitHub Desktop.

Select an option

Save parthi2929/809f99a616b97ffe947df148cf525989 to your computer and use it in GitHub Desktop.
My jinja2 template for customized latex format from nbconvert..
%((*- extends 'style_ipython.tplx' -*))
% 1. CHANGE DOCUMENTCLASS TO STANDALONE
((* block docclass *))
\documentclass[float=false, crop=false, 11pt]{standalone}
((* endblock docclass *))
% 2. REMOVE PREAMBLE AND REPLACE WITH STYLE FILE INCLUSION
((* block packages *))
\usepackage{../myipy2tex} % NOTE WE ARE ASSSUMING THE STYLE FILE TO BE ONE FOLDER ABOVE
\usepackage[breakable]{tcolorbox}
% if you need to cross reference to any raw tex file from this resultant tex file you need to refer them here..
% it is not needed when you compile main.tex but make sure the labels are unique
\ifstandalone
\usepackage{xr-hyper} % Needed for external references
\externaldocument{"24. Confidence Intervals - Theory"}
\fi
((* endblock packages *))
((* block definitions *))
% Exact colors from NB
\definecolor{incolor}{rgb}{0.0, 0.0, 0.5}
\definecolor{outcolor}{rgb}{0.545, 0.0, 0.0}
% to create background box for code cells..
% https://github.com/t-makaro/nb_pdf_template/blob/master/nb_pdf_template/templates/style_jupyter.tplx
% Exact colors from NB
((*- block style_colors *))
\definecolor{incolor}{HTML}{303F9F}
\definecolor{outcolor}{HTML}{D84315}
\definecolor{cellborder}{HTML}{CFCFCF}
\definecolor{cellbackground}{HTML}{F7F7F7}
((*- endblock style_colors *))
% environments
((*- block style_environments *))
\newenvironment{InVerbatim}{\VerbatimEnvironment%
\begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,
colback=cellbackground, colframe=cellborder]
\begin{Verbatim}
}{
\end{Verbatim}
\end{tcolorbox}
}
\newenvironment{OutVerbatim}{\VerbatimEnvironment%
\begin{tcolorbox}[breakable, boxrule=.5pt, size=fbox, pad at break*=1mm, opacityfill=0]
\begin{Verbatim}
}{
\end{Verbatim}
\end{tcolorbox}
}
((* endblock style_environments *))
((* endblock definitions *))
((* block commands *))
((* endblock commands *))
% 3. BELOW STUFF ARE JUST FOR CODE CELL CORRECTIONS TO GET LINES WRAPPED PROPERLY..
%===============================================================================
% Input
%===============================================================================
((* block input scoped *))
((( custom_add_prompt(cell.source | wrap_text(88) | highlight_code(strip_verbatim=True), cell, 'In ', 'incolor') )))
((* endblock input *))
%===============================================================================
% Output
%===============================================================================
% Display stream ouput with coloring
((* block stream *))
\begin{Verbatim}[commandchars=\\\{\},fontsize=\footnotesize]
((( output.text | wrap_text(86) | escape_latex | ansi2latex )))
\end{Verbatim}
((* endblock stream *))
%==============================================================================
% Define macro custom_add_prompt() (derived from add_prompt() macro in style_ipython.tplx)
%==============================================================================
((* macro custom_add_prompt(text, cell, prompt, prompt_color) -*))
((*- if cell.execution_count is defined -*))
((*- set execution_count = "" ~ (cell.execution_count | replace(None, " ")) -*))
((*- else -*))
((*- set execution_count = " " -*))
((*- endif -*))
((*- set indention = " " * (execution_count | length + 7) -*))
\begin{Verbatim}[commandchars=\\\{\},fontsize=\scriptsize]
((( text | add_prompts(first='{\color{' ~ prompt_color ~ '}' ~ prompt ~ '[{\\color{' ~ prompt_color ~ '}' ~ execution_count ~ '}]:} ', cont=indention) )))
\end{Verbatim}
((*- endmacro *))
@parthi2929

Copy link
Copy Markdown
Author

Currently the code cells bg are still not greyed with a box like in notebook.. to be fixed..

@t-makaro

t-makaro commented Sep 26, 2018

Copy link
Copy Markdown

I figure here is maybe a more appropriate place to have this conversation.

I pass a value of "In" and add the space later where needed. If you blank out the repygments block from my template, then everything else in style_jupyter.tplx is required for it to work (there were many small artifacts that I had to correct). After thinking about it, I might be able to move repygments up a level.

Text wrapping like you've done in line 71 won't work properly (syntax highlighting will be wrong).

I don't really see anything that would conflict with my template here. Presumably it's all in \usepackage{../myipy2tex}.

I am planning to add that ((* block stream *)) to my template already. It should be out later this week.

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