Show More
@@ -1,72 +1,73 b'' | |||
|
1 | 1 | """Module that allows latex output notebooks to be conditioned before |
|
2 | 2 | they are converted. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | 8 | # |
|
9 | 9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | from __future__ import print_function, absolute_import |
|
17 | 17 | |
|
18 | 18 | # Third-party import, needed for Pygments latex definitions. |
|
19 | 19 | from pygments.formatters import LatexFormatter |
|
20 | 20 | |
|
21 | 21 | # ipy imports |
|
22 | 22 | from .base import (Preprocessor) |
|
23 | 23 | from IPython.nbconvert import filters |
|
24 | 24 | |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | # Classes |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | |
|
29 | 29 | class LatexPreprocessor(Preprocessor): |
|
30 | 30 | """ |
|
31 | 31 | Converter for latex destined documents. |
|
32 | 32 | """ |
|
33 | 33 | |
|
34 | 34 | def preprocess(self, nb, resources): |
|
35 | 35 | """ |
|
36 | 36 | Preprocessing to apply on each notebook. |
|
37 | 37 | |
|
38 | 38 | Parameters |
|
39 | 39 | ---------- |
|
40 | 40 | nb : NotebookNode |
|
41 | 41 | Notebook being converted |
|
42 | 42 | resources : dictionary |
|
43 | 43 | Additional resources used in the conversion process. Allows |
|
44 | 44 | preprocessors to pass variables into the Jinja engine. |
|
45 | 45 | """ |
|
46 | 46 | # Generate Pygments definitions for Latex |
|
47 | resources["pygment_definitions"] = LatexFormatter().get_style_defs() | |
|
47 | resources["latex"] = {} | |
|
48 | resources["latex"]["pygment_definitions"] = LatexFormatter().get_style_defs() | |
|
48 | 49 | return super(LatexPreprocessor, self).preprocess(nb, resources) |
|
49 | 50 | |
|
50 | 51 | |
|
51 | 52 | def preprocess_cell(self, cell, resources, index): |
|
52 | 53 | """ |
|
53 | 54 | Apply a transformation on each cell, |
|
54 | 55 | |
|
55 | 56 | Parameters |
|
56 | 57 | ---------- |
|
57 | 58 | cell : NotebookNode cell |
|
58 | 59 | Notebook cell being processed |
|
59 | 60 | resources : dictionary |
|
60 | 61 | Additional resources used in the conversion process. Allows |
|
61 | 62 | preprocessors to pass variables into the Jinja engine. |
|
62 | 63 | index : int |
|
63 | 64 | Modified index of the cell being processed (see base.py) |
|
64 | 65 | """ |
|
65 | 66 | |
|
66 | 67 | #If the cell is a markdown cell, preprocess the ampersands used to |
|
67 | 68 | #remove the space between them and their contents. Latex will complain |
|
68 | 69 | #if spaces exist between the ampersands and the math content. |
|
69 | 70 | #See filters.latex.rm_math_space for more information. |
|
70 | 71 | if hasattr(cell, "source") and cell.cell_type == "markdown": |
|
71 | 72 | cell.source = filters.strip_math_space(cell.source) |
|
72 | 73 | return cell, resources |
@@ -1,171 +1,174 b'' | |||
|
1 | 1 | ((= Latex base template (must inherit) |
|
2 | 2 | This template builds upon the abstract template, adding common latex output |
|
3 | 3 | functions. Figures, data_text, |
|
4 | 4 | This template does not define a docclass, the inheriting class must define this.=)) |
|
5 | 5 | |
|
6 | 6 | ((*- extends 'abstract.tplx' -*)) |
|
7 | 7 | |
|
8 | 8 | %=============================================================================== |
|
9 | 9 | % Abstract overrides |
|
10 | 10 | %=============================================================================== |
|
11 | 11 | |
|
12 | 12 | ((* block packages *)) |
|
13 | 13 | \usepackage{graphicx} % Used to insert images |
|
14 | 14 | \usepackage{adjustbox} % Used to constrain images to a maximum size |
|
15 | 15 | \usepackage{color} % Allow colors to be defined |
|
16 | 16 | \usepackage{enumerate} % Needed for markdown enumerations to work |
|
17 | 17 | \usepackage{fancyvrb} % Needed to support color codes (tex) in verbatim blocks |
|
18 | 18 | \usepackage{geometry} % Used to adjust the document margins |
|
19 | 19 | \usepackage{amsmath} % Equations |
|
20 | 20 | \usepackage{amssymb} % Equations |
|
21 | 21 | \usepackage[utf8]{inputenc} % Allow utf-8 characters in the tex document |
|
22 | 22 | \usepackage{ucs} % Extended unicode (utf-8) support |
|
23 | 23 | \usepackage{grffile} % extends the file name processing of package graphics |
|
24 | 24 | %to support a larger range |
|
25 | 25 | |
|
26 | 26 | % The hyperref package gives us a pdf with properly built |
|
27 | 27 | % internal navigation ('pdf bookmarks' for the table of contents, |
|
28 | 28 | % internal cross-reference links, web links for URLs, etc.) |
|
29 | 29 | \usepackage{hyperref} |
|
30 | ||
|
31 | % Pygments definitions | |
|
32 | ((( resources.latex.pygment_definitions ))) | |
|
30 | 33 | ((* endblock packages *)) |
|
31 | 34 | |
|
32 | 35 | ((* block definitions *)) |
|
33 | 36 | \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
|
34 | 37 | \definecolor{darkorange}{rgb}{.71,0.21,0.01} |
|
35 | 38 | \definecolor{darkgreen}{rgb}{.12,.54,.11} |
|
36 | 39 | \definecolor{myteal}{rgb}{.26, .44, .56} |
|
37 | 40 | \definecolor{gray}{gray}{0.45} |
|
38 | 41 | \definecolor{lightgray}{gray}{.95} |
|
39 | 42 | \definecolor{mediumgray}{gray}{.8} |
|
40 | 43 | \definecolor{inputbackground}{rgb}{.95, .95, .85} |
|
41 | 44 | \definecolor{outputbackground}{rgb}{.95, .95, .95} |
|
42 | 45 | \definecolor{traceback}{rgb}{1, .95, .95} |
|
43 | 46 | |
|
44 | 47 | % new ansi colors |
|
45 | 48 | \definecolor{brown}{rgb}{0.54,0.27,0.07} |
|
46 | 49 | \definecolor{purple}{rgb}{0.5,0.0,0.5} |
|
47 | 50 | \definecolor{darkgray}{gray}{0.25} |
|
48 | 51 | \definecolor{lightred}{rgb}{1.0,0.39,0.28} |
|
49 | 52 | \definecolor{lightgreen}{rgb}{0.48,0.99,0.0} |
|
50 | 53 | \definecolor{lightblue}{rgb}{0.53,0.81,0.92} |
|
51 | 54 | \definecolor{lightpurple}{rgb}{0.87,0.63,0.87} |
|
52 | 55 | \definecolor{lightcyan}{rgb}{0.5,1.0,0.83} |
|
53 | 56 | |
|
54 | 57 | ((* endblock definitions *)) |
|
55 | 58 | |
|
56 | 59 | ((* block commands *)) |
|
57 | 60 | \sloppy % Prevent overflowing lines due to hard-to-break entities |
|
58 | 61 | |
|
59 | 62 | % Setup hyperref package |
|
60 | 63 | \hypersetup{ |
|
61 | 64 | breaklinks=true, % so long urls are correctly broken across lines |
|
62 | 65 | colorlinks=true, |
|
63 | 66 | urlcolor=blue, |
|
64 | 67 | linkcolor=darkorange, |
|
65 | 68 | citecolor=darkgreen, |
|
66 | 69 | } |
|
67 | 70 | |
|
68 | 71 | % Slightly bigger margins than the latex defaults |
|
69 | 72 | \geometry{verbose,tmargin=3cm,bmargin=3cm,lmargin=2.5cm,rmargin=2.5cm} |
|
70 | 73 | |
|
71 | 74 | % Hardcode size of all verbatim environments to be a bit smaller |
|
72 | 75 | \makeatletter |
|
73 | 76 | \g@addto@macro\@verbatim\small\topsep=0.5em\partopsep=0pt |
|
74 | 77 | \makeatother |
|
75 | 78 | ((* endblock commands *)) |
|
76 | 79 | |
|
77 | 80 | %=============================================================================== |
|
78 | 81 | % Support blocks |
|
79 | 82 | %=============================================================================== |
|
80 | 83 | |
|
81 | 84 | % Displaying simple data text |
|
82 | 85 | ((* block data_text *)) |
|
83 | 86 | \begin{verbatim} |
|
84 | 87 | ((( output.text | escape_latex ))) |
|
85 | 88 | \end{verbatim} |
|
86 | 89 | ((* endblock data_text *)) |
|
87 | 90 | |
|
88 | 91 | % Display python error text as-is |
|
89 | 92 | ((* block pyerr *)) |
|
90 | 93 | \begin{verbatim} |
|
91 | 94 | ((( super() ))) |
|
92 | 95 | \end{verbatim} |
|
93 | 96 | ((* endblock pyerr *)) |
|
94 | 97 | ((* block traceback_line *)) |
|
95 | 98 | ((( line | indent | strip_ansi | escape_latex ))) |
|
96 | 99 | ((* endblock traceback_line *)) |
|
97 | 100 | |
|
98 | 101 | % Display stream ouput with coloring |
|
99 | 102 | ((* block stream *)) |
|
100 | 103 | \begin{Verbatim}[commandchars=\\\{\}] |
|
101 | 104 | ((( output.text | ansi2latex ))) |
|
102 | 105 | \end{Verbatim} |
|
103 | 106 | ((* endblock stream *)) |
|
104 | 107 | |
|
105 | 108 | % Display latex |
|
106 | 109 | ((* block data_latex -*)) |
|
107 | 110 | ((*- if output.latex.startswith('$'): -*)) |
|
108 | 111 | ((= Replace $ symbols with more explicit, equation block. =)) |
|
109 | 112 | \begin{equation*} |
|
110 | 113 | ((( output.latex | strip_dollars ))) |
|
111 | 114 | \end{equation*} |
|
112 | 115 | ((*- else -*)) |
|
113 | 116 | ((( output.latex ))) |
|
114 | 117 | ((*- endif *)) |
|
115 | 118 | ((* endblock data_latex *)) |
|
116 | 119 | |
|
117 | 120 | % Default mechanism for rendering figures |
|
118 | 121 | ((*- block data_png -*))((( draw_figure(output.png_filename) )))((*- endblock -*)) |
|
119 | 122 | ((*- block data_jpg -*))((( draw_figure(output.jpeg_filename) )))((*- endblock -*)) |
|
120 | 123 | ((*- block data_svg -*))((( draw_figure(output.svg_filename) )))((*- endblock -*)) |
|
121 | 124 | ((*- block data_pdf -*))((( draw_figure(output.pdf_filename) )))((*- endblock -*)) |
|
122 | 125 | |
|
123 | 126 | % Draw a figure using the graphicx package. |
|
124 | 127 | ((* macro draw_figure(filename) -*)) |
|
125 | 128 | ((* set filename = filename | posix_path *)) |
|
126 | 129 | ((*- block figure scoped -*)) |
|
127 | 130 | \begin{center} |
|
128 | 131 | \adjustimage{max size={0.9\linewidth}{..}}{((( filename )))} |
|
129 | 132 | \par |
|
130 | 133 | \end{center} |
|
131 | 134 | ((*- endblock figure -*)) |
|
132 | 135 | ((*- endmacro *)) |
|
133 | 136 | |
|
134 | 137 | % Draw heading cell. Explicitly map different cell levels. |
|
135 | 138 | ((* block headingcell scoped -*)) |
|
136 | 139 | ((*- if cell.level == 1 -*)) |
|
137 | 140 | ((* block h1 -*))\section((* endblock h1 -*)) |
|
138 | 141 | ((*- elif cell.level == 2 -*)) |
|
139 | 142 | ((* block h2 -*))\subsection((* endblock h2 -*)) |
|
140 | 143 | ((*- elif cell.level == 3 -*)) |
|
141 | 144 | ((* block h3 -*))\subsubsection((* endblock h3 -*)) |
|
142 | 145 | ((*- elif cell.level == 4 -*)) |
|
143 | 146 | ((* block h4 -*))\paragraph((* endblock h4 -*)) |
|
144 | 147 | ((*- elif cell.level == 5 -*)) |
|
145 | 148 | ((* block h5 -*))\subparagraph((* endblock h5 -*)) |
|
146 | 149 | ((*- elif cell.level == 6 -*)) |
|
147 | 150 | ((* block h6 -*))\\*\textit((* endblock h6 -*)) |
|
148 | 151 | ((*- endif -*)) |
|
149 | 152 | {((( cell.source | escape_latex )))} |
|
150 | 153 | ((*- endblock headingcell *)) |
|
151 | 154 | |
|
152 | 155 | % Redirect pyout to display data priority. |
|
153 | 156 | ((* block pyout scoped *)) |
|
154 | 157 | ((* block data_priority scoped *)) |
|
155 | 158 | ((( super() ))) |
|
156 | 159 | ((* endblock *)) |
|
157 | 160 | ((* endblock pyout *)) |
|
158 | 161 | |
|
159 | 162 | % Render markdown |
|
160 | 163 | ((* block markdowncell scoped *)) |
|
161 | 164 | ((( cell.source | markdown2latex ))) |
|
162 | 165 | ((* endblock markdowncell *)) |
|
163 | 166 | |
|
164 | 167 | % Spit out the contents of raw cells unmodified |
|
165 | 168 | ((* block rawcell scoped *)) |
|
166 | 169 | ((( cell.source ))) |
|
167 | 170 | ((* endblock rawcell *)) |
|
168 | 171 | |
|
169 | 172 | % Don't display unknown types |
|
170 | 173 | ((* block unknowncell scoped *)) |
|
171 | 174 | ((* endblock unknowncell *)) |
General Comments 0
You need to be logged in to leave comments.
Login now