##// END OF EJS Templates
Added notebook output formatting style
Jonathan Frederic -
Show More
@@ -1,356 +1,371 b''
1 1 ((= NBConvert Sphinx-Latex Template
2 2
3 3 Purpose: Allow export of PDF friendly Latex inspired by Sphinx. Most of the
4 4 template is derived directly from Sphinx source.
5 5
6 6 Inheritance: null>display_priority>latex_base
7 7
8 8 Note: For best display, use latex syntax highlighting. =))
9 9
10 10 ((*- extends 'display_priority.tplx' -*))
11 11
12 12 %==============================================================================
13 13 % Declarations
14 14 %==============================================================================
15 15
16 16 % In order to make sure that the input/output header follows the code it
17 17 % preceeds, the needspace package is used to request that a certain
18 18 % amount of lines (specified by this variable) are reserved. If those
19 19 % lines aren't available on the current page, the documenter will break
20 20 % to the next page and the header along with accomanying lines will be
21 21 % rendered together. This value specifies the number of lines that
22 22 % the header will be forced to group with without a page break.
23 23 ((*- set min_header_lines = 4 -*))
24 24
25 25 % This is the number of characters that are permitted per line. It's
26 26 % important that this limit is set so characters do not run off the
27 27 % edges of latex pages (since latex does not always seem smart enough
28 28 % to prevent this in some cases.) This is only applied to textual output
29 ((*- set wrap_size = 85 -*))
29 ((* if resources.sphinx.outputstyle == 'simple' *))
30 ((*- set wrap_size = 85 -*))
31 ((* elif resources.sphinx.outputstyle == 'notebook' *))
32 ((*- set wrap_size = 70 -*))
33 ((* endif *))
30 34
31 35 %==============================================================================
32 36 % Header
33 37 %==============================================================================
34 38 ((* block header *))
35 39
36 40 % Header, overrides base
37 41
38 42 % Make sure that the sphinx doc style knows who it inherits from.
39 43 \def\sphinxdocclass{(((parentdocumentclass)))}
40 44
41 45 % Declare the document class
42 46 \documentclass[letterpaper,10pt,english]{((( resources.sphinx.texinputs )))/sphinx(((documentclass)))}
43 47
44 48 % Imports
45 49 \usepackage[utf8]{inputenc}
46 50 \DeclareUnicodeCharacter{00A0}{\\nobreakspace}
47 51 \usepackage[T1]{fontenc}
48 52 \usepackage{babel}
49 53 \usepackage{times}
50 54 \usepackage{import}
51 55 \usepackage[((( resources.sphinx.chapterstyle )))]{((( resources.sphinx.texinputs )))/fncychap}
52 56 \usepackage{longtable}
53 57 \usepackage{((( resources.sphinx.texinputs )))/sphinx}
54 58 \usepackage{multirow}
55 59
56 60 \usepackage{amsmath}
57 61 \usepackage{amssymb}
58 62 \usepackage{ucs}
59 63 \usepackage{enumerate}
60 64
61 65 % Used to make the Input/Output rules follow around the contents.
62 66 \usepackage{needspace}
63 67
64 68 % Pygments requirements
65 69 \usepackage{fancyvrb}
66 70 \usepackage{color}
67 71
68 72 % Needed to box output/input
69 73 \usepackage{mdframed}
70 74 \usepackage{alltt}
71 75
72 76 % Used to load and display graphics
73 77 \usepackage{graphicx}
74 78 \graphicspath{ {figs/} }
75 79
76 80 % For formatting output while also word wrapping.
77 81 \usepackage{listings}
78 82 \lstset{breaklines=true}
79 83 \lstset{basicstyle=\small\ttfamily}
80 84
81 85 %Pygments definitions
82 86 ((( resources.sphinx.pygment_definitions )))
83 87
84 88 %Set pygments styles if needed...
85 89 ((* if resources.sphinx.outputstyle == 'notebook' *))
86 90 \definecolor{nbframe-border}{rgb}{0.8,0.8,0.8}
87 91 \definecolor{nbframe-bg}{rgb}{0.95,0.95,0.95}
88 92
89 \newenvironment{ColorVerbatim}[1][\unskip]
90 {#1\\*\vspace{-2.4\baselineskip}\begin{mdframed}[backgroundcolor=nbframe-bg, userdefinedwidth=1\linewidth, leftmargin=0.08\linewidth, linecolor=nbframe-border, linewidth=1pt, usetwoside=false]}
93 \newenvironment{ColorVerbatim}
94 {\begin{mdframed}[backgroundcolor=nbframe-bg, userdefinedwidth=1\linewidth, leftmargin=0.08\linewidth, linecolor=nbframe-border, linewidth=1pt, usetwoside=false]}
91 95 {\end{mdframed}}
92 96
93 97 \renewenvironment{Verbatim}[1][\unskip]
94 98 {\begin{alltt}}
95 99 {\end{alltt}}
96 100 ((* endif *))
97 101
98 102 % Help prevent overflowing lines due to urls and other hard-to-break
99 103 % entities. This doesn't catch everything...
100 104 \sloppy
101 105
102 106 % Document level variables
103 107 \title{((( nb.metadata.name | escape_tex )))}
104 108 \date{((( nb.metadata._draft.date | escape_tex )))}
105 109 \release{((( nb.metadata._draft.version | escape_tex )))}
106 110 \author{((( nb.metadata._draft.author | escape_tex )))}
107 111 \renewcommand{\releasename}{((( nb.metadata._draft.release | escape_tex )))}
108 112
109 113 % TODO: Add option for the user to specify a logo for his/her export.
110 114 \newcommand{\sphinxlogo}{}
111 115
112 116 % Make the index page of the document.
113 117 \makeindex
114 118
115 119 % Import sphinx document type specifics.
116 120 ((* block sphinxheader *))((* endblock sphinxheader *))
117 121 ((* endblock header *))
118 122
119 123 %==============================================================================
120 124 % Body
121 125 %==============================================================================
122 126 ((* block body *))
123 127 ((* block bodyBegin *))
124 128 % Body
125 129
126 130 % Start of the document
127 131 \begin{document}
128 132 \maketitle
129 133 \tableofcontents
130 134
131 135 ((* endblock bodyBegin *))((( super() )))((* block bodyEnd *))
132 136
133 137 \renewcommand{\indexname}{Index}
134 138 \printindex
135 139
136 140 % End of document
137 141 \end{document}
138 142 ((* endblock bodyEnd *))
139 143 ((* endblock body *))
140 144
141 145 %==============================================================================
142 146 % Footer
143 147 %==============================================================================
144 148 ((* block footer *))
145 149 ((* endblock footer *))
146 150
147 151 %==============================================================================
148 152 % Headings
149 153 %
150 154 % Purpose: Format pynb headers as sphinx headers. Depending on the Sphinx
151 155 % style that is active, this will change. Thus sphinx styles will
152 156 % override the values here.
153 157 %==============================================================================
154 158 ((* block headingcell -*))
155 159 \
156 160 ((*- if cell.level == 1 -*))
157 161 ((* block h1 -*))part((* endblock h1 -*))
158 162 ((*- elif cell.level == 2 -*))
159 163 ((* block h2 -*))chapter((* endblock h2 -*))
160 164 ((*- elif cell.level == 3 -*))
161 165 ((* block h3 -*))section((* endblock h3 -*))
162 166 ((*- elif cell.level == 4 -*))
163 167 ((* block h4 -*))subsection((* endblock h4 -*))
164 168 ((*- elif cell.level == 5 -*))
165 169 ((* block h5 -*))subsubsection((* endblock h5 -*))
166 170 ((*- elif cell.level == 6 -*))
167 171 ((* block h6 -*))paragraph((* endblock h6 -*))
168 172
169 173 ((= It's important to make sure that underscores (which tend to be common
170 174 in IPYNB file titles) do not make their way into latex. Sometimes this
171 175 causes latex to barf. =))
172 176 ((*- endif -*)){((( escape_underscores(cell.source | markdown2latex ) )))}
173 177 ((*- endblock headingcell *))
174 178
175 179 %==============================================================================
176 180 % Markdown
177 181 %
178 182 % Purpose: Convert markdown to latex. Here markdown2latex is explicitly
179 183 % called since we know we want latex output.
180 184 %==============================================================================
181 185 ((*- block markdowncell scoped-*))
182 186 ((( cell.source | markdown2latex )))
183 187 ((*- endblock markdowncell -*))
184 188
185 189 %==============================================================================
186 190 % Rawcell
187 191 %
188 192 % Purpose: Raw text cells allow the user to manually inject document code that
189 193 % will not get touched by the templating system.
190 194 %==============================================================================
191 195 ((*- block rawcell *))
192 196 ((( cell.source | wrap(wrap_size) )))
193 197 ((* endblock rawcell -*))
194 198
195 199 %==============================================================================
196 200 % Unknowncell
197 201 %
198 202 % Purpose: This is the catch anything unhandled. To display this data, we
199 203 % remove all possible latex conflicts and wrap the characters so they
200 204 % can't flow off of the page.
201 205 %==============================================================================
202 206 ((* block unknowncell scoped*))
203 207
204 208 % Unsupported cell type, no formatting
205 209 ((( cell.source | wrap | escape_tex )))
206 210 ((* endblock unknowncell *))
207 211
208 212 %==============================================================================
209 213 % Input
210 214 %==============================================================================
211 215 ((* block input *))
212 216 ((* if resources.sphinx.outputstyle == 'simple' *))
213 217 % Make sure that atleast 4 lines are below the HR
214 218 \needspace{((( min_header_lines )))\baselineskip}
215 219
216 220 % Add a horizantal break, along with break title.
217 221 \vspace{10pt}
218 222 {\scriptsize Input}\\*
219 223 \rule[10pt]{\linewidth}{0.5pt}
220 224 \vspace{-25pt}
221 225
222 226 % Add contents below.
223 227 ((( cell.input | highlight )))
224 228
225 229 ((* elif resources.sphinx.outputstyle == 'notebook' *))
226 \begin{ColorVerbatim}[\textbf{In {[}((( cell.prompt_number ))){]}:}]
230 \textbf{In {[}((( cell.prompt_number ))){]}:}\\*
231 \vspace{-2.4\baselineskip}
232 \begin{ColorVerbatim}
227 233 ((( cell.input | highlight )))
228 234 \end{ColorVerbatim}
229 235 ((* endif *))
230 236 ((* endblock input *))
231 237
232 238 %==============================================================================
233 239 % Output_Group
234 240 %
235 241 % Purpose: Make sure that only one header bar only attaches to the output
236 242 % once. By keeping track of when an input group is started
237 243 %==============================================================================
238 244 ((* block output_group *))
239 245 ((* if resources.sphinx.outputstyle == 'simple' *))
240 246
241 247 % If the first block is an image, minipage the image. Else
242 248 % request a certain amount of space for the input text.
243 249 ((( iff_figure(cell.outputs[0], "\\begin{minipage}{1.0\\textwidth}", "\\needspace{" ~ min_header_lines ~ "\\baselineskip}") )))
244 250
245 251 % Add a horizantal break, along with break title.
246 252 \vspace{10pt}
247 253 {\scriptsize Output}\\*
248 254 \rule[10pt]{\linewidth}{0.5pt}
249 255 \vspace{-20pt}
250 256
251 257 % Add the contents of the first block.
252 258 ((( render_output(cell.outputs[0]) )))
253 259
254 260 % Close the minipage.
255 261 ((( iff_figure(cell.outputs[0], "\\end{minipage}", "") )))
256 262
257 263 % Add remainer of the document contents below.
258 264 ((* for output in cell.outputs[1:] *))
259 265 ((( render_output(output) )))
260 266 ((* endfor *))
261 267 ((* elif resources.sphinx.outputstyle == 'notebook' *))
262 268
263 % Add remainer of the document contents below.
264 ((* for output in cell.outputs *))
265 ((( render_output(output) )))
266 ((* endfor *))
269 %Skip one line down.
270 \vspace{1\baselineskip}
271
272 % Add document contents.
273 \begin{ColorVerbatim}
274 ((* for output in cell.outputs *))
275 ((( render_output(output) )))
276 ((* endfor *))
277 \end{ColorVerbatim}
267 278 ((* endif *))
268 279 ((* endblock *))
269 280
270 281 %==============================================================================
271 282 % Additional formating
272 283 %==============================================================================
273 284 ((* block data_text *))
274 285 ((( custom_verbatim(output.text) )))
275 286 ((* endblock *))
276 287
277 288 ((* block traceback_line *))
278 289 ((( line | indent| rm_ansi )))
279 290 ((* endblock traceback_line *))
280 291
281 292 %==============================================================================
282 293 % Supported image formats
283 294 %==============================================================================
284 295 ((*- block data_png -*))
285 296 ((( insert_graphics(output.key_png) )))
286 297 ((*- endblock -*))
287 298
288 299 ((*- block data_svg -*))
289 300 ((( insert_graphics(output.key_svg) )))
290 301 ((*- endblock -*))
291 302
292 303 %==============================================================================
293 304 % Support Macros
294 305 %==============================================================================
295 306
296 307 % Name: render_output
297 308 % Purpose: Renders an output block appropriately.
298 309 ((* macro render_output(output) -*))
299 310 ((*- if output.output_type == 'pyout' -*))
300 311 ((*- block pyout scoped -*))
301 312 ((( custom_verbatim(super()) )))
302 313 ((*- endblock pyout -*))
303 314 ((*- elif output.output_type == 'stream' -*))
304 315 ((*- block stream scoped -*))
305 316 ((( custom_verbatim(output.text) )))
306 317 ((*- endblock stream -*))
307 318 ((*- elif output.output_type == 'display_data' -*))
308 319 ((*- block display_data scoped -*))
309 320 ((( super() )))
310 321 ((*- endblock display_data -*))
311 322 ((*- elif output.output_type == 'pyerr' -*))
312 323 ((*- block pyerr scoped -*))
313 324 ((( custom_verbatim(super()) )))
314 325 ((*- endblock pyerr -*))
315 326 ((*- endif -*))
316 327 ((*- endmacro *))
317 328
318 329 % Name: iff_figure
319 330 % Purpose: If the output block provided is a figure type, the 'true_content'
320 331 % parameter will be returned. Else, the 'false_content'.
321 332 ((* macro iff_figure(output, true_content, false_content) -*))
322 333 ((* if output.output_type in ['display_data'] -*))
323 334 ((( true_content )))
324 335 ((*- else -*))
325 336 ((( false_content )))
326 337 ((*- endif *))
327 338 ((*- endmacro *))
328 339
329 340 % Name: custom_verbatim
330 341 % Purpose: This macro creates a verbatim style block that fits the existing
331 342 % sphinx style more readily than standard verbatim blocks.
332 343 ((* macro custom_verbatim(text) -*))
333
344 ((* if resources.sphinx.outputstyle == 'simple' *))
334 345 \begin{lstlisting}
335 346 ((( text | wrap(wrap_size) )))
336 \end{lstlisting}
337
347 \end{lstlisting}
348 ((* elif resources.sphinx.outputstyle == 'notebook' *))
349 \begin{alltt}
350 ((( text | wrap(wrap_size) )))
351 \end{alltt}
352 ((* endif *))
338 353 ((*- endmacro *))
339 354
340 355 % Name: insert_graphics
341 356 % Purpose: This macro will insert an image in the latex document given a path.
342 357 ((* macro insert_graphics(path) -*))
343 358 \begin{center}
344 359 \includegraphics[width=0.7\textwidth]{(((path)))}
345 360 \par
346 361 \end{center}
347 362 ((*- endmacro *))
348 363
349 364 % Name: escape_underscores
350 365 % Purpose: Underscores cause a problem in latex. It's important that we
351 366 % escape any underscores that appear.
352 367 ((* macro escape_underscores(text) -*))
353 368 ((*- set text = text|replace("_","\\_") -*))
354 369 ((( text )))
355 370 ((*- endmacro *))
356 371
General Comments 0
You need to be logged in to leave comments. Login now