forked from jrjohansson/scientific-python-lectures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter.tplx
290 lines (251 loc) · 10.5 KB
/
chapter.tplx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
((= Notebook input/output style =))
((* extends 'display_priority.tplx' *))
((* block header *))
((* endblock header *))
((* block body *))
((( super() )))
((* endblock body *))
% 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 *))
%===============================================================================
% Support blocks
%===============================================================================
% Displaying simple data text
((* block data_text *))
\begin{verbatim}
((( output.text )))
\end{verbatim}
((* endblock data_text *))
% Display python error text as-is
((* block pyerr *))
\begin{Verbatim}[commandchars=\\\{\}]
((( super() )))
\end{Verbatim}
((* endblock pyerr *))
((* block traceback_line *))
((( line | indent | strip_ansi | escape_latex )))
((* endblock traceback_line *))
% Display stream ouput with coloring
((* block stream *))
\begin{Verbatim}[commandchars=\\\{\}]
((( output.text | escape_latex | ansi2latex )))
\end{Verbatim}
((* endblock stream *))
% Display latex
((* block data_latex -*))
((*- if output.latex.startswith('$'): -*))
((= Replace $ symbols with more explicit, equation block. =))
\begin{equation*}
((( output.latex | strip_dollars )))
\end{equation*}
((*- else -*))
((( output.latex )))
((*- endif *))
((* endblock data_latex *))
% Default mechanism for rendering figures
((*- block data_png -*))((( draw_figure(output.png_filename) )))((*- endblock -*))
((*- block data_jpg -*))((( draw_figure(output.jpeg_filename) )))((*- endblock -*))
((*- block data_svg -*))((( draw_figure(output.svg_filename) )))((*- endblock -*))
((*- block data_pdf -*))((( draw_figure(output['application/pdf_filename']) )))((*- endblock -*))
% Draw a figure using the graphicx package.
((* macro draw_figure(filename) -*))
((* set filename = filename | posix_path *))
((*- block figure scoped -*))
\begin{center}
\adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{((( filename )))}
\end{center}
{ \hspace*{\fill} \\}
((*- endblock figure -*))
((*- endmacro *))
% Draw heading cell. Explicitly map different cell levels.
((* block headingcell scoped *))
((* if cell.level == 1 -*))
((* block h1 -*))\chapter((* endblock h1 -*))
((* elif cell.level == 2 -*))
((* block h2 -*))\section((* endblock h2 -*))
((* elif cell.level == 3 -*))
((* block h3 -*))\subsection((* endblock h3 -*))
((* elif cell.level == 4 -*))
((* block h4 -*))\subsubsection((* endblock h4 -*))
((* elif cell.level == 5 -*))
((* block h5 -*))\subparagraph((* endblock h5 -*))
((* elif cell.level == 6 -*))
((* block h6 -*))\\*\textit((* endblock h6 -*))
((*- endif -*))
{((( cell.source | replace('\n', ' ') | citation2latex | strip_files_prefix | markdown2latex )))}
((* endblock headingcell *))
% Redirect pyout to display data priority.
((* block pyout scoped *))
((* block data_priority scoped *))
((( super() )))
((* endblock *))
((* endblock pyout *))
% Render markdown
((* block markdowncell scoped *))
((( cell.source | citation2latex | strip_files_prefix | markdown2latex )))
((* endblock markdowncell *))
% Don't display unknown types
((* block unknowncell scoped *))
((* endblock unknowncell *))