Skip to content

Commit

Permalink
Updates to match IPython#4112
Browse files Browse the repository at this point in the history
  • Loading branch information
jdfreder committed Sep 24, 2013
1 parent 897c673 commit 945ea57
Show file tree
Hide file tree
Showing 17 changed files with 939 additions and 33 deletions.
2 changes: 1 addition & 1 deletion custom_latex_cell_style/README.md
@@ -1,6 +1,6 @@
## Features

NBConvert is distributed with 4 output templates and 6 cell styles. All 6 cell styles can be used by creating a custom template. Custom cell styles are also supported. The implementation of a custom cell style depends on the design requirements. Here are two possible scenarios, the first being the most flexible:
NBConvert is distributed with 2 output templates and 4 cell styles. All 4 cell styles can be used by creating a custom template. Custom cell styles are also supported. The implementation of a custom cell style depends on the design requirements. Here are two possible scenarios, the first being the most flexible:

1. **A custom cell style that will be used with multiple output templates, possibly including custom output templates**
This scenario requires two template files. The first template file is the cell style itself. The second template file is a custom template that chooses which cell style and output template to use.
Expand Down
36 changes: 5 additions & 31 deletions custom_latex_cell_style/scenario1/custom_cell_style.tplx
Expand Up @@ -3,7 +3,7 @@

% Inherit from the notebook style. If you do not want to inherit from an
% existing cell style, you can inherit from latex_base.tplx.
((*- extends 'style_notebook.tplx' -*))
((*- extends 'style_ipython.tplx' -*))


%===============================================================================
Expand All @@ -13,33 +13,7 @@
((* block definitions *))
((( super() )))

% Define the environment which puts the frame
\renewenvironment{notebookcell}[0]{%
\def\FrameCommand{\notebookcellframe}%
\def\FirstFrameCommand{\notebookcellframetop}%
\def\LastFrameCommand{\notebookcellframebottom}%
\def\MidFrameCommand{\notebookcellframemiddle}%
\par\vspace{1\baselineskip}%
\MakeFramed {\FrameRestore}%
\noindent { } \par}%
{\endMakeFramed}

% Remove the left margin
\setlength{\cellleftmargin}{0pt}
((* endblock definitions *))


((* block input scoped *))
\begin{notebookcell}%
{\smaller%
((( cell.input | highlight2latex )))}%
\end{notebookcell}%
((* endblock input *))


((* block output -*))
{\smaller%
((* block data_priority -*))
((( super() )))
((* endblock data_priority -*))}%
((*- endblock output *))
% Custom prompts colors
\definecolor{incolor}{rgb}{0.0, 0.8, 0.0}
\definecolor{outcolor}{rgb}{0.65, 0.6, 0.0}
((* endblock definitions *))
8 changes: 8 additions & 0 deletions hr_cell_style/ipython_nbconvert_config.py
@@ -0,0 +1,8 @@
c = get_config()

#Export all the notebooks in the current directory to the sphinx_howto format.
c.NbConvertApp.notebooks = ['*.ipynb']
c.NbConvertApp.export_format = 'latex'
c.NbConvertApp.postprocessor_class = 'PDF'

c.Exporter.template_file = 'my_output.tplx'
6 changes: 6 additions & 0 deletions hr_cell_style/my_output.tplx
@@ -0,0 +1,6 @@

% Tell the templating engine to use our cell style.
((* set cell_style = 'style_hr.tplx' *))

% Tell the templating engine what output template we want to use.
((* extends 'latex_article.tplx' *))
68 changes: 68 additions & 0 deletions hr_cell_style/style_hr.tplx
@@ -0,0 +1,68 @@
((= Simple input/output style =))

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

% Custom packages
((* block packages *))
((( super() )))

\usepackage{needspace} % Make prompts follow contents
((* endblock packages *))

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

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

\newlength{\promptspace}
\setlength{\promptspace}{4\baselineskip} % Space needed to start a new
% input/output
((* endblock definitions *))
%===============================================================================
% Input
%===============================================================================

((*- block in_prompt scoped -*))
\par\vspace{-1\baselineskip}
((( draw_prompt('Input') )))
((*- endblock in_prompt -*))

((*- block input_group scoped -*))
(((- super() )))
\par\vspace{-2\baselineskip}
((* endblock input_group -*))

((* block input scoped *))
((( cell.input | highlight2latex )))
((* endblock input *))

%===============================================================================
% Output
%===============================================================================

((*- block output_prompt scoped -*))
((( draw_prompt('Output') )))
((*- endblock output_prompt -*))

((*- block output_group scoped -*))
(((- super() )))
\par\vspace{-1\baselineskip}
((* endblock output_group -*))

%==============================================================================
% Support Macros
%==============================================================================

% Name: draw_prompt
% Purpose: Renders an output/input prompt for notebook style pdfs
((* macro draw_prompt(prompt) *))
% Add a horizantal break, along with break title.
\needspace{\promptspace}
\br\br
{\scriptsize ((( prompt )))}
\br
\rule[10pt]{\linewidth}{0.5pt}
\vspace{-2.5em}
((* endmacro *))
143 changes: 143 additions & 0 deletions hr_cell_style/test.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion latex_cell_style/use_cell_style.tplx
@@ -1,5 +1,5 @@
((= This line selects the cell style. =))
((* set cell_style = 'style_notebook.tplx' *))
((* set cell_style = 'style_python.tplx' *))

((= This line inherits from the built in template that you want to use. =))
((* extends 'latex_article.tplx' *))
8 changes: 8 additions & 0 deletions notebook_cell_style/ipython_nbconvert_config.py
@@ -0,0 +1,8 @@
c = get_config()

#Export all the notebooks in the current directory to the sphinx_howto format.
c.NbConvertApp.notebooks = ['*.ipynb']
c.NbConvertApp.export_format = 'latex'
c.NbConvertApp.postprocessor_class = 'PDF'

c.Exporter.template_file = 'my_output.tplx'
6 changes: 6 additions & 0 deletions notebook_cell_style/my_output.tplx
@@ -0,0 +1,6 @@

% Tell the templating engine to use our cell style.
((* set cell_style = 'style_notebook.tplx' *))

% Tell the templating engine what output template we want to use.
((* extends 'latex_article.tplx' *))
191 changes: 191 additions & 0 deletions notebook_cell_style/style_notebook.tplx
@@ -0,0 +1,191 @@
((= Notebook input/output style =))

((* extends 'latex_base.tplx' *))

% Custom packages
((* block packages *))
((( super() )))

\usepackage{tikz} % Needed to box output/input
\usepackage{scrextend} % Used to indent output
\usepackage{needspace} % Make prompts follow contents
\usepackage{framed} % Used to draw output that spans multiple pages
((* endblock packages *))

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

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

% NB prompt colors
\definecolor{nbframe-border}{rgb}{0.867,0.867,0.867}
\definecolor{nbframe-bg}{rgb}{0.969,0.969,0.969}
\definecolor{nbframe-in-prompt}{rgb}{0.0,0.0,0.502}
\definecolor{nbframe-out-prompt}{rgb}{0.545,0.0,0.0}

% NB prompt lengths
\newlength{\inputpadding}
\setlength{\inputpadding}{0.5em}
\newlength{\cellleftmargin}
\setlength{\cellleftmargin}{0.15\linewidth}
\newlength{\borderthickness}
\setlength{\borderthickness}{0.4pt}
\newlength{\smallerfontscale}
\setlength{\smallerfontscale}{9.5pt}

% NB prompt font size
\def\smaller{\fontsize{\smallerfontscale}{\smallerfontscale}\selectfont}

% Define a background layer, in which the nb prompt shape is drawn
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\usetikzlibrary{calc}

% define styles for the normal border and the torn border
\tikzset{
normal border/.style={draw=nbframe-border, fill=nbframe-bg,
rectangle, rounded corners=2.5pt, line width=\borderthickness},
torn border/.style={draw=white, fill=white, line width=\borderthickness}}

% Macro to draw the shape behind the text, when it fits completly in the
% page
\def\notebookcellframe#1{%
\tikz{%
\node[inner sep=\inputpadding] (A) {#1};% Draw the text of the node
\begin{pgfonlayer}{background}% Draw the shape behind
\fill[normal border]%
(A.south east) -- ($(A.south west)+(\cellleftmargin,0)$) --
($(A.north west)+(\cellleftmargin,0)$) -- (A.north east) -- cycle;
\end{pgfonlayer}}}%

% Macro to draw the shape, when the text will continue in next page
\def\notebookcellframetop#1{%
\tikz{%
\node[inner sep=\inputpadding] (A) {#1}; % Draw the text of the node
\begin{pgfonlayer}{background}
\fill[normal border] % Draw the ``complete shape'' behind
(A.south east) -- ($(A.south west)+(\cellleftmargin,0)$) --
($(A.north west)+(\cellleftmargin,0)$) -- (A.north east) -- cycle;
\fill[torn border] % Add the torn lower border
($(A.south east)-(0,.1)$) -- ($(A.south west)+(\cellleftmargin,-.1)$) --
($(A.south west)+(\cellleftmargin,.1)$) -- ($(A.south east)+(0,.1)$) -- cycle;
\end{pgfonlayer}}}

% Macro to draw the shape, when the text continues from previous page
\def\notebookcellframebottom#1{%
\tikz{%
\node[inner sep=\inputpadding] (A) {#1}; % Draw the text of the node
\begin{pgfonlayer}{background}
\fill[normal border] % Draw the ``complete shape'' behind
(A.south east) -- ($(A.south west)+(\cellleftmargin,0)$) --
($(A.north west)+(\cellleftmargin,0)$) -- (A.north east) -- cycle;
\fill[torn border] % Add the torn upper border
($(A.north east)-(0,.1)$) -- ($(A.north west)+(\cellleftmargin,-.1)$) --
($(A.north west)+(\cellleftmargin,.1)$) -- ($(A.north east)+(0,.1)$) -- cycle;
\end{pgfonlayer}}}

% Macro to draw the shape, when both the text continues from previous page
% and it will continue in next page
\def\notebookcellframemiddle#1{%
\tikz{%
\node[inner sep=\inputpadding] (A) {#1}; % Draw the text of the node
\begin{pgfonlayer}{background}
\fill[normal border] % Draw the ``complete shape'' behind
(A.south east) -- ($(A.south west)+(\cellleftmargin,0)$) --
($(A.north west)+(\cellleftmargin,0)$) -- (A.north east) -- cycle;
\fill[torn border] % Add the torn lower border
($(A.south east)-(0,.1)$) -- ($(A.south west)+(\cellleftmargin,-.1)$) --
($(A.south west)+(\cellleftmargin,.1)$) -- ($(A.south east)+(0,.1)$) -- cycle;
\fill[torn border] % Add the torn upper border
($(A.north east)-(0,.1)$) -- ($(A.north west)+(\cellleftmargin,-.1)$) --
($(A.north west)+(\cellleftmargin,.1)$) -- ($(A.north east)+(0,.1)$) -- cycle;
\end{pgfonlayer}}}

% Define the environment which puts the frame
% In this case, the environment also accepts an argument with an optional
% title (which defaults to ``Example'', which is typeset in a box overlaid
% on the top border
\newenvironment{notebookcell}[1][0]{%
\def\FrameCommand{\notebookcellframe}%
\def\FirstFrameCommand{\notebookcellframetop}%
\def\LastFrameCommand{\notebookcellframebottom}%
\def\MidFrameCommand{\notebookcellframemiddle}%
\par\vspace{1\baselineskip}%
\MakeFramed {\FrameRestore}%
\noindent\tikz\node[inner sep=0em] at ($(A.north west)-(0,0)$) {%
((( draw_prompt("In", "#1", "nbframe-in-prompt", "2pt") )))%
}; \par}%
{\endMakeFramed}

((* endblock definitions *))

%===============================================================================
% Input
%===============================================================================

((* block input scoped *))
% Add contents below.

{\par%
\vspace{-1\baselineskip}%
\needspace{4\baselineskip}}%
\begin{notebookcell}[((( cell.prompt_number )))]%
\begin{addmargin}[\cellleftmargin]{0em}% left, right
{\smaller%
\par%
((* block extra_input_spacing *))((* endblock extra_input_spacing *))%
\vspace{-1\smallerfontscale}%
((( cell.input | highlight2latex )))%
\par%
\vspace{-1\smallerfontscale}}%
\end{addmargin}
\end{notebookcell}

((* endblock input *))

%===============================================================================
% Output
%===============================================================================

((* block output -*))
\par\vspace{1\smallerfontscale}%
\needspace{4\baselineskip}%
% Only render the prompt if the cell is pyout. Note, the outputs prompt
% block isn't used since we need to check each indiviual output and only
% add prompts to the pyout ones.
((* if output.output_type in ['pyout'] *))
{\par%
\vspace{-1\smallerfontscale}%
\noindent%
((( draw_prompt("Out", cell.prompt_number, "nbframe-out-prompt", "0em") )))%
((* block extra_output_spacing *))((* endblock extra_output_spacing *))}%
((* endif *))%
%
\begin{addmargin}[\cellleftmargin]{0em}% left, right
{\smaller%
\vspace{-1\smallerfontscale}%
((( super() )))}%
\end{addmargin}%
((*- endblock output *))

%==============================================================================
% Support Macros
%==============================================================================

% Name: draw_prompt
% Purpose: Renders an output/input prompt for notebook style pdfs
((* macro draw_prompt(prompt, number, color, space) -*))
\begin{minipage}{\cellleftmargin}%
\hfill%
{\smaller%
\tt%
\color{(((color)))}%
(((prompt)))[(((number)))]:}%
\hspace{\inputpadding}%
\hspace{(((space)))}%
\hspace{3pt}%
\end{minipage}%
((*- endmacro *))

0 comments on commit 945ea57

Please sign in to comment.