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