##// END OF EJS Templates
FIXED, latex characters not escaped properly in nbconvert
Jonathan Frederic -
Show More
@@ -1,115 +1,115 b''
1 """Latex filters.
1 """Latex filters.
2
2
3 Module of useful filters for processing Latex within Jinja latex templates.
3 Module of useful filters for processing Latex within Jinja latex templates.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 import re
16 import re
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Globals and constants
19 # Globals and constants
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 #Latex substitutions for escaping latex.
22 #Latex substitutions for escaping latex.
23 LATEX_SUBS = (
23 LATEX_SUBS = (
24 (re.compile('\033\[[0-9;]+m'),''), # handle console escapes
24 (re.compile('\033\[[0-9;]+m'),''), # handle console escapes
25 (re.compile(r'\\'), r'\\textbackslash'),
25 (re.compile(r'\\'), r'{\\textbackslash}'),
26 (re.compile(r'([{}_#%&$])'), r'\\\1'),
26 (re.compile(r'([{}_#%&$])'), r'\\\1'),
27 (re.compile(r'~'), r'\~{}'),
27 (re.compile(r'~'), r'\~{}'),
28 (re.compile(r'\^'), r'\^{}'),
28 (re.compile(r'\^'), r'\^{}'),
29 (re.compile(r'"'), r"''"),
29 (re.compile(r'"'), r"''"),
30 (re.compile(r'\.\.\.+'), r'\\ldots'),
30 (re.compile(r'\.\.\.+'), r'\\ldots'),
31 )
31 )
32
32
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 # Functions
34 # Functions
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36
36
37 __all__ = [
37 __all__ = [
38 'escape_latex',
38 'escape_latex',
39 'strip_math_space'
39 'strip_math_space'
40 ]
40 ]
41
41
42
42
43 def escape_latex(text):
43 def escape_latex(text):
44 """
44 """
45 Escape characters that may conflict with latex.
45 Escape characters that may conflict with latex.
46
46
47 Parameters
47 Parameters
48 ----------
48 ----------
49 text : str
49 text : str
50 Text containing characters that may conflict with Latex
50 Text containing characters that may conflict with Latex
51 """
51 """
52 return_text = text
52 return_text = text
53 for pattern, replacement in LATEX_SUBS:
53 for pattern, replacement in LATEX_SUBS:
54 return_text = pattern.sub(replacement, return_text)
54 return_text = pattern.sub(replacement, return_text)
55 return return_text
55 return return_text
56
56
57
57
58 def strip_math_space(text):
58 def strip_math_space(text):
59 """
59 """
60 Remove the space between latex math commands and enclosing $ symbols.
60 Remove the space between latex math commands and enclosing $ symbols.
61 This filter is important because latex isn't as flexible as the notebook
61 This filter is important because latex isn't as flexible as the notebook
62 front end when it comes to flagging math using ampersand symbols.
62 front end when it comes to flagging math using ampersand symbols.
63
63
64 Parameters
64 Parameters
65 ----------
65 ----------
66 text : str
66 text : str
67 Text to filter.
67 Text to filter.
68 """
68 """
69
69
70 # First, scan through the markdown looking for $. If
70 # First, scan through the markdown looking for $. If
71 # a $ symbol is found, without a preceding \, assume
71 # a $ symbol is found, without a preceding \, assume
72 # it is the start of a math block. UNLESS that $ is
72 # it is the start of a math block. UNLESS that $ is
73 # not followed by another within two math_lines.
73 # not followed by another within two math_lines.
74 math_regions = []
74 math_regions = []
75 math_lines = 0
75 math_lines = 0
76 within_math = False
76 within_math = False
77 math_start_index = 0
77 math_start_index = 0
78 ptext = ''
78 ptext = ''
79 last_character = ""
79 last_character = ""
80 skip = False
80 skip = False
81 for index, char in enumerate(text):
81 for index, char in enumerate(text):
82
82
83 #Make sure the character isn't preceeded by a backslash
83 #Make sure the character isn't preceeded by a backslash
84 if (char == "$" and last_character != "\\"):
84 if (char == "$" and last_character != "\\"):
85
85
86 # Close the math region if this is an ending $
86 # Close the math region if this is an ending $
87 if within_math:
87 if within_math:
88 within_math = False
88 within_math = False
89 skip = True
89 skip = True
90 ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
90 ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$'
91 math_regions.append([math_start_index, index+1])
91 math_regions.append([math_start_index, index+1])
92 else:
92 else:
93
93
94 # Start a new math region
94 # Start a new math region
95 within_math = True
95 within_math = True
96 math_start_index = index
96 math_start_index = index
97 math_lines = 0
97 math_lines = 0
98
98
99 # If we are in a math region, count the number of lines parsed.
99 # If we are in a math region, count the number of lines parsed.
100 # Cancel the math region if we find two line breaks!
100 # Cancel the math region if we find two line breaks!
101 elif char == "\n":
101 elif char == "\n":
102 if within_math:
102 if within_math:
103 math_lines += 1
103 math_lines += 1
104 if math_lines > 1:
104 if math_lines > 1:
105 within_math = False
105 within_math = False
106 ptext = ptext+text[math_start_index:index]
106 ptext = ptext+text[math_start_index:index]
107
107
108 # Remember the last character so we can easily watch
108 # Remember the last character so we can easily watch
109 # for backslashes
109 # for backslashes
110 last_character = char
110 last_character = char
111 if not within_math and not skip:
111 if not within_math and not skip:
112 ptext = ptext+char
112 ptext = ptext+char
113 if skip:
113 if skip:
114 skip = False
114 skip = False
115 return ptext
115 return ptext
@@ -1,469 +1,469 b''
1 ((= NBConvert Sphinx-Latex Template
1 ((= NBConvert Sphinx-Latex Template
2
2
3 Purpose: Allow export of PDF friendly Latex inspired by Sphinx. Most of the
3 Purpose: Allow export of PDF friendly Latex inspired by Sphinx. Most of the
4 template is derived directly from Sphinx source.
4 template is derived directly from Sphinx source.
5
5
6 Inheritance: null>display_priority
6 Inheritance: null>display_priority
7
7
8 Note: For best display, use latex syntax highlighting. =))
8 Note: For best display, use latex syntax highlighting. =))
9
9
10 ((*- extends 'display_priority.tplx' -*))
10 ((*- extends 'display_priority.tplx' -*))
11
11
12
12
13 \nonstopmode
13 \nonstopmode
14
14
15 %==============================================================================
15 %==============================================================================
16 % Declarations
16 % Declarations
17 %==============================================================================
17 %==============================================================================
18
18
19 % In order to make sure that the input/output header follows the code it
19 % In order to make sure that the input/output header follows the code it
20 % preceeds, the needspace package is used to request that a certain
20 % preceeds, the needspace package is used to request that a certain
21 % amount of lines (specified by this variable) are reserved. If those
21 % amount of lines (specified by this variable) are reserved. If those
22 % lines aren't available on the current page, the documenter will break
22 % lines aren't available on the current page, the documenter will break
23 % to the next page and the header along with accomanying lines will be
23 % to the next page and the header along with accomanying lines will be
24 % rendered together. This value specifies the number of lines that
24 % rendered together. This value specifies the number of lines that
25 % the header will be forced to group with without a page break.
25 % the header will be forced to group with without a page break.
26 ((*- set min_header_lines = 4 -*))
26 ((*- set min_header_lines = 4 -*))
27
27
28 % This is the number of characters that are permitted per line. It's
28 % This is the number of characters that are permitted per line. It's
29 % important that this limit is set so characters do not run off the
29 % important that this limit is set so characters do not run off the
30 % edges of latex pages (since latex does not always seem smart enough
30 % edges of latex pages (since latex does not always seem smart enough
31 % to prevent this in some cases.) This is only applied to textual output
31 % to prevent this in some cases.) This is only applied to textual output
32 ((* if resources.sphinx.outputstyle == 'simple' *))
32 ((* if resources.sphinx.outputstyle == 'simple' *))
33 ((*- set wrap_size = 85 -*))
33 ((*- set wrap_size = 85 -*))
34 ((* elif resources.sphinx.outputstyle == 'notebook' *))
34 ((* elif resources.sphinx.outputstyle == 'notebook' *))
35 ((*- set wrap_size = 70 -*))
35 ((*- set wrap_size = 70 -*))
36 ((* endif *))
36 ((* endif *))
37
37
38 %==============================================================================
38 %==============================================================================
39 % Header
39 % Header
40 %==============================================================================
40 %==============================================================================
41 ((* block header *))
41 ((* block header *))
42
42
43 % Header, overrides base
43 % Header, overrides base
44
44
45 % Make sure that the sphinx doc style knows who it inherits from.
45 % Make sure that the sphinx doc style knows who it inherits from.
46 \def\sphinxdocclass{(((parentdocumentclass)))}
46 \def\sphinxdocclass{(((parentdocumentclass)))}
47
47
48 % Declare the document class
48 % Declare the document class
49 \documentclass[letterpaper,10pt,english]{((( resources.sphinx.texinputs | posix_path )))/sphinx(((documentclass)))}
49 \documentclass[letterpaper,10pt,english]{((( resources.sphinx.texinputs | posix_path )))/sphinx(((documentclass)))}
50
50
51 % Imports
51 % Imports
52 \usepackage[utf8]{inputenc}
52 \usepackage[utf8]{inputenc}
53 \DeclareUnicodeCharacter{00A0}{\\nobreakspace}
53 \DeclareUnicodeCharacter{00A0}{\\nobreakspace}
54 \usepackage[T1]{fontenc}
54 \usepackage[T1]{fontenc}
55 \usepackage{babel}
55 \usepackage{babel}
56 \usepackage{times}
56 \usepackage{times}
57 \usepackage{import}
57 \usepackage{import}
58 \usepackage[((( resources.sphinx.chapterstyle )))]{((( resources.sphinx.texinputs | posix_path )))/fncychap}
58 \usepackage[((( resources.sphinx.chapterstyle )))]{((( resources.sphinx.texinputs | posix_path )))/fncychap}
59 \usepackage{longtable}
59 \usepackage{longtable}
60 \usepackage{((( resources.sphinx.texinputs | posix_path )))/sphinx}
60 \usepackage{((( resources.sphinx.texinputs | posix_path )))/sphinx}
61 \usepackage{multirow}
61 \usepackage{multirow}
62
62
63 \usepackage{amsmath}
63 \usepackage{amsmath}
64 \usepackage{amssymb}
64 \usepackage{amssymb}
65 \usepackage{ucs}
65 \usepackage{ucs}
66 \usepackage{enumerate}
66 \usepackage{enumerate}
67
67
68 % Used to make the Input/Output rules follow around the contents.
68 % Used to make the Input/Output rules follow around the contents.
69 \usepackage{needspace}
69 \usepackage{needspace}
70
70
71 % Pygments requirements
71 % Pygments requirements
72 \usepackage{fancyvrb}
72 \usepackage{fancyvrb}
73 \usepackage{color}
73 \usepackage{color}
74 % ansi colors additions
74 % ansi colors additions
75 \definecolor{darkgreen}{rgb}{.12,.54,.11}
75 \definecolor{darkgreen}{rgb}{.12,.54,.11}
76 \definecolor{lightgray}{gray}{.95}
76 \definecolor{lightgray}{gray}{.95}
77 \definecolor{brown}{rgb}{0.54,0.27,0.07}
77 \definecolor{brown}{rgb}{0.54,0.27,0.07}
78 \definecolor{purple}{rgb}{0.5,0.0,0.5}
78 \definecolor{purple}{rgb}{0.5,0.0,0.5}
79 \definecolor{darkgray}{gray}{0.25}
79 \definecolor{darkgray}{gray}{0.25}
80 \definecolor{lightred}{rgb}{1.0,0.39,0.28}
80 \definecolor{lightred}{rgb}{1.0,0.39,0.28}
81 \definecolor{lightgreen}{rgb}{0.48,0.99,0.0}
81 \definecolor{lightgreen}{rgb}{0.48,0.99,0.0}
82 \definecolor{lightblue}{rgb}{0.53,0.81,0.92}
82 \definecolor{lightblue}{rgb}{0.53,0.81,0.92}
83 \definecolor{lightpurple}{rgb}{0.87,0.63,0.87}
83 \definecolor{lightpurple}{rgb}{0.87,0.63,0.87}
84 \definecolor{lightcyan}{rgb}{0.5,1.0,0.83}
84 \definecolor{lightcyan}{rgb}{0.5,1.0,0.83}
85
85
86 % Needed to box output/input
86 % Needed to box output/input
87 \usepackage{tikz}
87 \usepackage{tikz}
88 \usetikzlibrary{calc,arrows,shadows}
88 \usetikzlibrary{calc,arrows,shadows}
89 \usepackage[framemethod=tikz]{mdframed}
89 \usepackage[framemethod=tikz]{mdframed}
90
90
91 \usepackage{alltt}
91 \usepackage{alltt}
92
92
93 % Used to load and display graphics
93 % Used to load and display graphics
94 \usepackage{graphicx}
94 \usepackage{graphicx}
95 \graphicspath{ {figs/} }
95 \graphicspath{ {figs/} }
96 \usepackage[Export]{adjustbox} % To resize
96 \usepackage[Export]{adjustbox} % To resize
97
97
98 % used so that images for notebooks which have spaces in the name can still be included
98 % used so that images for notebooks which have spaces in the name can still be included
99 \usepackage{grffile}
99 \usepackage{grffile}
100
100
101
101
102 % For formatting output while also word wrapping.
102 % For formatting output while also word wrapping.
103 \usepackage{listings}
103 \usepackage{listings}
104 \lstset{breaklines=true}
104 \lstset{breaklines=true}
105 \lstset{basicstyle=\small\ttfamily}
105 \lstset{basicstyle=\small\ttfamily}
106 \def\smaller{\fontsize{9.5pt}{9.5pt}\selectfont}
106 \def\smaller{\fontsize{9.5pt}{9.5pt}\selectfont}
107
107
108 %Pygments definitions
108 %Pygments definitions
109 ((( resources.sphinx.pygment_definitions )))
109 ((( resources.sphinx.pygment_definitions )))
110
110
111 %Set pygments styles if needed...
111 %Set pygments styles if needed...
112 ((* if resources.sphinx.outputstyle == 'notebook' *))
112 ((* if resources.sphinx.outputstyle == 'notebook' *))
113 \definecolor{nbframe-border}{rgb}{0.867,0.867,0.867}
113 \definecolor{nbframe-border}{rgb}{0.867,0.867,0.867}
114 \definecolor{nbframe-bg}{rgb}{0.969,0.969,0.969}
114 \definecolor{nbframe-bg}{rgb}{0.969,0.969,0.969}
115 \definecolor{nbframe-in-prompt}{rgb}{0.0,0.0,0.502}
115 \definecolor{nbframe-in-prompt}{rgb}{0.0,0.0,0.502}
116 \definecolor{nbframe-out-prompt}{rgb}{0.545,0.0,0.0}
116 \definecolor{nbframe-out-prompt}{rgb}{0.545,0.0,0.0}
117
117
118 \newenvironment{ColorVerbatim}
118 \newenvironment{ColorVerbatim}
119 {\begin{mdframed}[%
119 {\begin{mdframed}[%
120 roundcorner=1.0pt, %
120 roundcorner=1.0pt, %
121 backgroundcolor=nbframe-bg, %
121 backgroundcolor=nbframe-bg, %
122 userdefinedwidth=1\linewidth, %
122 userdefinedwidth=1\linewidth, %
123 leftmargin=0.1\linewidth, %
123 leftmargin=0.1\linewidth, %
124 innerleftmargin=0pt, %
124 innerleftmargin=0pt, %
125 innerrightmargin=0pt, %
125 innerrightmargin=0pt, %
126 linecolor=nbframe-border, %
126 linecolor=nbframe-border, %
127 linewidth=1pt, %
127 linewidth=1pt, %
128 usetwoside=false, %
128 usetwoside=false, %
129 everyline=true, %
129 everyline=true, %
130 innerlinewidth=3pt, %
130 innerlinewidth=3pt, %
131 innerlinecolor=nbframe-bg, %
131 innerlinecolor=nbframe-bg, %
132 middlelinewidth=1pt, %
132 middlelinewidth=1pt, %
133 middlelinecolor=nbframe-bg, %
133 middlelinecolor=nbframe-bg, %
134 outerlinewidth=0.5pt, %
134 outerlinewidth=0.5pt, %
135 outerlinecolor=nbframe-border, %
135 outerlinecolor=nbframe-border, %
136 needspace=0pt
136 needspace=0pt
137 ]}
137 ]}
138 {\end{mdframed}}
138 {\end{mdframed}}
139
139
140 \newenvironment{InvisibleVerbatim}
140 \newenvironment{InvisibleVerbatim}
141 {\begin{mdframed}[leftmargin=0.1\linewidth,innerleftmargin=3pt,innerrightmargin=3pt, userdefinedwidth=1\linewidth, linewidth=0pt, linecolor=white, usetwoside=false]}
141 {\begin{mdframed}[leftmargin=0.1\linewidth,innerleftmargin=3pt,innerrightmargin=3pt, userdefinedwidth=1\linewidth, linewidth=0pt, linecolor=white, usetwoside=false]}
142 {\end{mdframed}}
142 {\end{mdframed}}
143
143
144 \renewenvironment{Verbatim}[1][\unskip]
144 \renewenvironment{Verbatim}[1][\unskip]
145 {\begin{alltt}\smaller}
145 {\begin{alltt}\smaller}
146 {\end{alltt}}
146 {\end{alltt}}
147 ((* endif *))
147 ((* endif *))
148
148
149 % Help prevent overflowing lines due to urls and other hard-to-break
149 % Help prevent overflowing lines due to urls and other hard-to-break
150 % entities. This doesn't catch everything...
150 % entities. This doesn't catch everything...
151 \sloppy
151 \sloppy
152
152
153 % Document level variables
153 % Document level variables
154 \title{((( resources.metadata.name | escape_latex )))}
154 \title{((( resources.metadata.name | escape_latex )))}
155 \date{((( resources.sphinx.date | escape_latex )))}
155 \date{((( resources.sphinx.date | escape_latex )))}
156 \release{((( resources.sphinx.version | escape_latex )))}
156 \release{((( resources.sphinx.version | escape_latex )))}
157 \author{((( resources.sphinx.author | escape_latex )))}
157 \author{((( resources.sphinx.author | escape_latex )))}
158 \renewcommand{\releasename}{((( resources.sphinx.release | escape_latex )))}
158 \renewcommand{\releasename}{((( resources.sphinx.release | escape_latex )))}
159
159
160 % TODO: Add option for the user to specify a logo for his/her export.
160 % TODO: Add option for the user to specify a logo for his/her export.
161 \newcommand{\sphinxlogo}{}
161 \newcommand{\sphinxlogo}{}
162
162
163 % Make the index page of the document.
163 % Make the index page of the document.
164 \makeindex
164 \makeindex
165
165
166 % Import sphinx document type specifics.
166 % Import sphinx document type specifics.
167 ((* block sphinxheader *))((* endblock sphinxheader *))
167 ((* block sphinxheader *))((* endblock sphinxheader *))
168 ((* endblock header *))
168 ((* endblock header *))
169
169
170 %==============================================================================
170 %==============================================================================
171 % Body
171 % Body
172 %==============================================================================
172 %==============================================================================
173 ((* block body *))
173 ((* block body *))
174 ((* block bodyBegin *))
174 ((* block bodyBegin *))
175 % Body
175 % Body
176
176
177 % Start of the document
177 % Start of the document
178 \begin{document}
178 \begin{document}
179
179
180 ((* if resources.sphinx.header *))
180 ((* if resources.sphinx.header *))
181 \maketitle
181 \maketitle
182 ((* endif *))
182 ((* endif *))
183
183
184 ((* block toc *))
184 ((* block toc *))
185 \tableofcontents
185 \tableofcontents
186 ((* endblock toc *))
186 ((* endblock toc *))
187
187
188 ((* endblock bodyBegin *))
188 ((* endblock bodyBegin *))
189 ((( super() )))
189 ((( super() )))
190 ((* block bodyEnd *))
190 ((* block bodyEnd *))
191
191
192 \renewcommand{\indexname}{Index}
192 \renewcommand{\indexname}{Index}
193 \printindex
193 \printindex
194
194
195 % End of document
195 % End of document
196 \end{document}
196 \end{document}
197 ((* endblock bodyEnd *))
197 ((* endblock bodyEnd *))
198 ((* endblock body *))
198 ((* endblock body *))
199
199
200 %==============================================================================
200 %==============================================================================
201 % Footer
201 % Footer
202 %==============================================================================
202 %==============================================================================
203 ((* block footer *))
203 ((* block footer *))
204 ((* endblock footer *))
204 ((* endblock footer *))
205
205
206 %==============================================================================
206 %==============================================================================
207 % Headings
207 % Headings
208 %
208 %
209 % Purpose: Format pynb headers as sphinx headers. Depending on the Sphinx
209 % Purpose: Format pynb headers as sphinx headers. Depending on the Sphinx
210 % style that is active, this will change. Thus sphinx styles will
210 % style that is active, this will change. Thus sphinx styles will
211 % override the values here.
211 % override the values here.
212 %==============================================================================
212 %==============================================================================
213 ((* block headingcell -*))
213 ((* block headingcell -*))
214 \
214 \
215 ((*- if cell.level == 1 -*))
215 ((*- if cell.level == 1 -*))
216 ((* block h1 -*))part((* endblock h1 -*))
216 ((* block h1 -*))part((* endblock h1 -*))
217 ((*- elif cell.level == 2 -*))
217 ((*- elif cell.level == 2 -*))
218 ((* block h2 -*))chapter((* endblock h2 -*))
218 ((* block h2 -*))chapter((* endblock h2 -*))
219 ((*- elif cell.level == 3 -*))
219 ((*- elif cell.level == 3 -*))
220 ((* block h3 -*))section((* endblock h3 -*))
220 ((* block h3 -*))section((* endblock h3 -*))
221 ((*- elif cell.level == 4 -*))
221 ((*- elif cell.level == 4 -*))
222 ((* block h4 -*))subsection((* endblock h4 -*))
222 ((* block h4 -*))subsection((* endblock h4 -*))
223 ((*- elif cell.level == 5 -*))
223 ((*- elif cell.level == 5 -*))
224 ((* block h5 -*))subsubsection((* endblock h5 -*))
224 ((* block h5 -*))subsubsection((* endblock h5 -*))
225 ((*- elif cell.level == 6 -*))
225 ((*- elif cell.level == 6 -*))
226 ((* block h6 -*))paragraph((* endblock h6 -*))
226 ((* block h6 -*))paragraph((* endblock h6 -*))
227
227
228 ((= It's important to make sure that underscores (which tend to be common
228 ((= It's important to make sure that underscores (which tend to be common
229 in IPYNB file titles) do not make their way into latex. Sometimes this
229 in IPYNB file titles) do not make their way into latex. Sometimes this
230 causes latex to barf. =))
230 causes latex to barf. =))
231 ((*- endif -*))
231 ((*- endif -*))
232 {((( cell.source | markdown2latex )))}
232 {((( cell.source | markdown2latex )))}
233 ((*- endblock headingcell *))
233 ((*- endblock headingcell *))
234
234
235 %==============================================================================
235 %==============================================================================
236 % Markdown
236 % Markdown
237 %
237 %
238 % Purpose: Convert markdown to latex. Here markdown2latex is explicitly
238 % Purpose: Convert markdown to latex. Here markdown2latex is explicitly
239 % called since we know we want latex output.
239 % called since we know we want latex output.
240 %==============================================================================
240 %==============================================================================
241 ((*- block markdowncell scoped-*))
241 ((*- block markdowncell scoped-*))
242 ((( cell.source | markdown2latex )))
242 ((( cell.source | markdown2latex )))
243 ((*- endblock markdowncell -*))
243 ((*- endblock markdowncell -*))
244
244
245 %==============================================================================
245 %==============================================================================
246 % Rawcell
246 % Rawcell
247 %
247 %
248 % Purpose: Raw text cells allow the user to manually inject document code that
248 % Purpose: Raw text cells allow the user to manually inject document code that
249 % will not get touched by the templating system.
249 % will not get touched by the templating system.
250 %==============================================================================
250 %==============================================================================
251 ((*- block rawcell *))
251 ((*- block rawcell *))
252 ((( cell.source | wrap_text(wrap_size) )))
252 ((( cell.source | wrap_text(wrap_size) )))
253 ((* endblock rawcell -*))
253 ((* endblock rawcell -*))
254
254
255 %==============================================================================
255 %==============================================================================
256 % Unknowncell
256 % Unknowncell
257 %
257 %
258 % Purpose: This is the catch anything unhandled. To display this data, we
258 % Purpose: This is the catch anything unhandled. To display this data, we
259 % remove all possible latex conflicts and wrap the characters so they
259 % remove all possible latex conflicts and wrap the characters so they
260 % can't flow off of the page.
260 % can't flow off of the page.
261 %==============================================================================
261 %==============================================================================
262 ((* block unknowncell scoped*))
262 ((* block unknowncell scoped*))
263 % Unsupported cell type, no formatting
263 % Unsupported cell type, no formatting
264 ((( cell.source | wrap_text | escape_latex )))
264 ((( cell.source | wrap_text | escape_latex )))
265 ((* endblock unknowncell *))
265 ((* endblock unknowncell *))
266
266
267 %==============================================================================
267 %==============================================================================
268 % Input
268 % Input
269 %==============================================================================
269 %==============================================================================
270 ((* block input *))
270 ((* block input *))
271
271
272 % Make sure that atleast 4 lines are below the HR
272 % Make sure that atleast 4 lines are below the HR
273 \needspace{((( min_header_lines )))\baselineskip}
273 \needspace{((( min_header_lines )))\baselineskip}
274
274
275 ((* if resources.sphinx.outputstyle == 'simple' *))
275 ((* if resources.sphinx.outputstyle == 'simple' *))
276
276
277 % Add a horizantal break, along with break title.
277 % Add a horizantal break, along with break title.
278 \vspace{10pt}
278 \vspace{10pt}
279 {\scriptsize Input}\\*
279 {\scriptsize Input}\\*
280 \rule[10pt]{\linewidth}{0.5pt}
280 \rule[10pt]{\linewidth}{0.5pt}
281 \vspace{-25pt}
281 \vspace{-25pt}
282
282
283 % Add contents below.
283 % Add contents below.
284 ((( cell.input | highlight2latex )))
284 ((( cell.input | highlight2latex )))
285
285
286 ((* elif resources.sphinx.outputstyle == 'notebook' *))
286 ((* elif resources.sphinx.outputstyle == 'notebook' *))
287 \vspace{6pt}
287 \vspace{6pt}
288 ((( write_prompt("In", cell.prompt_number, "nbframe-in-prompt") )))
288 ((( write_prompt("In", cell.prompt_number, "nbframe-in-prompt") )))
289 \vspace{-2.65\baselineskip}
289 \vspace{-2.65\baselineskip}
290 \begin{ColorVerbatim}
290 \begin{ColorVerbatim}
291 \vspace{-0.7\baselineskip}
291 \vspace{-0.7\baselineskip}
292 ((( cell.input | highlight2latex )))
292 ((( cell.input | highlight2latex )))
293 ((* if cell.input == None or cell.input == '' *))
293 ((* if cell.input == None or cell.input == '' *))
294 \vspace{0.3\baselineskip}
294 \vspace{0.3\baselineskip}
295 ((* else *))
295 ((* else *))
296 \vspace{-0.2\baselineskip}
296 \vspace{-0.2\baselineskip}
297 ((* endif *))
297 ((* endif *))
298 \end{ColorVerbatim}
298 \end{ColorVerbatim}
299 ((* endif *))
299 ((* endif *))
300 ((* endblock input *))
300 ((* endblock input *))
301
301
302 %==============================================================================
302 %==============================================================================
303 % Output_Group
303 % Output_Group
304 %
304 %
305 % Purpose: Make sure that only one header bar only attaches to the output
305 % Purpose: Make sure that only one header bar only attaches to the output
306 % once. By keeping track of when an input group is started
306 % once. By keeping track of when an input group is started
307 %==============================================================================
307 %==============================================================================
308 ((* block output_group *))
308 ((* block output_group *))
309 ((* if cell.outputs.__len__() > 0 *))
309 ((* if cell.outputs.__len__() > 0 *))
310
310
311 % If the first block is an image, minipage the image. Else
311 % If the first block is an image, minipage the image. Else
312 % request a certain amount of space for the input text.
312 % request a certain amount of space for the input text.
313 ((( iff_figure(cell.outputs[0], "\\begin{minipage}{1.0\\textwidth}", "\\needspace{" ~ min_header_lines ~ "\\baselineskip}") )))
313 ((( iff_figure(cell.outputs[0], "\\begin{minipage}{1.0\\textwidth}", "\\needspace{" ~ min_header_lines ~ "\\baselineskip}") )))
314
314
315 ((* if resources.sphinx.outputstyle == 'simple' *))
315 ((* if resources.sphinx.outputstyle == 'simple' *))
316
316
317 % Add a horizantal break, along with break title.
317 % Add a horizantal break, along with break title.
318 \vspace{10pt}
318 \vspace{10pt}
319 {\scriptsize Output}\\*
319 {\scriptsize Output}\\*
320 \rule[10pt]{\linewidth}{0.5pt}
320 \rule[10pt]{\linewidth}{0.5pt}
321 \vspace{-20pt}
321 \vspace{-20pt}
322
322
323 % Add the contents of the first block.
323 % Add the contents of the first block.
324 ((( render_output(cell.outputs[0]) )))
324 ((( render_output(cell.outputs[0]) )))
325
325
326 % Close the minipage.
326 % Close the minipage.
327 ((( iff_figure(cell.outputs[0], "\\end{minipage}", "") )))
327 ((( iff_figure(cell.outputs[0], "\\end{minipage}", "") )))
328
328
329 % Add remainer of the document contents below.
329 % Add remainer of the document contents below.
330 ((* for output in cell.outputs[1:] *))
330 ((* for output in cell.outputs[1:] *))
331 ((( render_output(output, cell.prompt_number) )))
331 ((( render_output(output, cell.prompt_number) )))
332 ((* endfor *))
332 ((* endfor *))
333 ((* elif resources.sphinx.outputstyle == 'notebook' *))
333 ((* elif resources.sphinx.outputstyle == 'notebook' *))
334
334
335 % Add document contents.
335 % Add document contents.
336 ((* for output in cell.outputs *))
336 ((* for output in cell.outputs *))
337 ((( render_output(output, cell.prompt_number) )))
337 ((( render_output(output, cell.prompt_number) )))
338 ((* endfor *))
338 ((* endfor *))
339 ((* endif *))
339 ((* endif *))
340 ((* endif *))
340 ((* endif *))
341 ((* endblock *))
341 ((* endblock *))
342
342
343 %==============================================================================
343 %==============================================================================
344 % Additional formating
344 % Additional formating
345 %==============================================================================
345 %==============================================================================
346 ((* block data_text *))
346 ((* block data_text *))
347 ((( custom_verbatim(output.text) | ansi2latex )))
347 ((( custom_verbatim(output.text) | ansi2latex )))
348 ((* endblock *))
348 ((* endblock *))
349
349
350 ((* block traceback_line *))
350 ((* block traceback_line *))
351 ((( conditionally_center_output( line | indent| strip_ansi ) )))
351 ((( conditionally_center_output( line | indent| strip_ansi ) )))
352 ((* endblock traceback_line *))
352 ((* endblock traceback_line *))
353
353
354 %==============================================================================
354 %==============================================================================
355 % Supported image formats
355 % Supported image formats
356 %==============================================================================
356 %==============================================================================
357 ((*- block data_png -*))
357 ((*- block data_png -*))
358 ((( conditionally_center_output(insert_graphics(output.png_filename | posix_path)) )))
358 ((( conditionally_center_output(insert_graphics(output.png_filename | posix_path)) )))
359 ((*- endblock -*))
359 ((*- endblock -*))
360
360
361 ((*- block data_jpg -*))
361 ((*- block data_jpg -*))
362 ((( conditionally_center_output(insert_graphics(output.jpg_filename | posix_path)) )))
362 ((( conditionally_center_output(insert_graphics(output.jpg_filename | posix_path)) )))
363 ((*- endblock -*))
363 ((*- endblock -*))
364
364
365 ((*- block data_svg -*))
365 ((*- block data_svg -*))
366 ((( conditionally_center_output(insert_graphics(output.svg_filename | posix_path)) )))
366 ((( conditionally_center_output(insert_graphics(output.svg_filename | posix_path)) )))
367 ((*- endblock -*))
367 ((*- endblock -*))
368
368
369 ((*- block data_pdf -*))
369 ((*- block data_pdf -*))
370 ((( conditionally_center_output(insert_graphics(output.pdf_filename | posix_path)) )))
370 ((( conditionally_center_output(insert_graphics(output.pdf_filename | posix_path)) )))
371 ((*- endblock -*))
371 ((*- endblock -*))
372
372
373 ((*- block data_latex *))
373 ((*- block data_latex *))
374 ((* if resources.sphinx.centeroutput *))
374 ((* if resources.sphinx.centeroutput *))
375 \begin{center}
375 \begin{center}
376 ((* endif -*))
376 ((* endif -*))
377 ((( output.latex | strip_math_space )))
377 ((( output.latex | strip_math_space )))
378 ((*- if resources.sphinx.centeroutput *))
378 ((*- if resources.sphinx.centeroutput *))
379 \end{center}
379 \end{center}
380 ((* endif -*))
380 ((* endif -*))
381 ((*- endblock -*))
381 ((*- endblock -*))
382
382
383 %==============================================================================
383 %==============================================================================
384 % Support Macros
384 % Support Macros
385 %==============================================================================
385 %==============================================================================
386
386
387 % Name: write_prompt
387 % Name: write_prompt
388 % Purpose: Renders an output/input prompt for notebook style pdfs
388 % Purpose: Renders an output/input prompt for notebook style pdfs
389 ((* macro write_prompt(prompt, number, color) -*))
389 ((* macro write_prompt(prompt, number, color) -*))
390 \makebox[0.1\linewidth]{\smaller\hfill\tt\color{((( color )))}((( prompt )))\hspace{4pt}{[}((( number ))){]}:\hspace{4pt}}\\*
390 \makebox[0.1\linewidth]{\smaller\hfill\tt\color{((( color )))}((( prompt )))\hspace{4pt}{[}((( number ))){]}:\hspace{4pt}}\\*
391 ((*- endmacro *))
391 ((*- endmacro *))
392
392
393 % Name: render_output
393 % Name: render_output
394 % Purpose: Renders an output block appropriately.
394 % Purpose: Renders an output block appropriately.
395 ((* macro render_output(output, prompt_number) -*))
395 ((* macro render_output(output, prompt_number) -*))
396 ((*- if output.output_type == 'pyerr' -*))
396 ((*- if output.output_type == 'pyerr' -*))
397 ((*- block pyerr scoped *))
397 ((*- block pyerr scoped *))
398 ((( custom_verbatim(super()) )))
398 ((( custom_verbatim(super()) )))
399 ((* endblock pyerr -*))
399 ((* endblock pyerr -*))
400 ((*- else -*))
400 ((*- else -*))
401
401
402 ((* if resources.sphinx.outputstyle == 'notebook' *))
402 ((* if resources.sphinx.outputstyle == 'notebook' *))
403 ((*- if output.output_type == 'pyout' -*))
403 ((*- if output.output_type == 'pyout' -*))
404 ((( write_prompt("Out", prompt_number, "nbframe-out-prompt") )))
404 ((( write_prompt("Out", prompt_number, "nbframe-out-prompt") )))
405 \vspace{-2.55\baselineskip}
405 \vspace{-2.55\baselineskip}
406 ((*- endif -*))
406 ((*- endif -*))
407
407
408 \begin{InvisibleVerbatim}
408 \begin{InvisibleVerbatim}
409 \vspace{-0.5\baselineskip}
409 \vspace{-0.5\baselineskip}
410 ((*- endif -*))
410 ((*- endif -*))
411
411
412 ((*- block display_data scoped -*))
412 ((*- block display_data scoped -*))
413 ((( super() )))
413 ((( super() )))
414 ((*- endblock display_data -*))
414 ((*- endblock display_data -*))
415
415
416 ((* if resources.sphinx.outputstyle == 'notebook' *))
416 ((* if resources.sphinx.outputstyle == 'notebook' *))
417 \end{InvisibleVerbatim}
417 \end{InvisibleVerbatim}
418 ((*- endif -*))
418 ((*- endif -*))
419 ((*- endif -*))
419 ((*- endif -*))
420 ((*- endmacro *))
420 ((*- endmacro *))
421
421
422 % Name: iff_figure
422 % Name: iff_figure
423 % Purpose: If the output block provided is a figure type, the 'true_content'
423 % Purpose: If the output block provided is a figure type, the 'true_content'
424 % parameter will be returned. Else, the 'false_content'.
424 % parameter will be returned. Else, the 'false_content'.
425 ((* macro iff_figure(output, true_content, false_content) -*))
425 ((* macro iff_figure(output, true_content, false_content) -*))
426 ((*- set is_figure = false -*))
426 ((*- set is_figure = false -*))
427 ((*- for type in output | filter_data_type -*))
427 ((*- for type in output | filter_data_type -*))
428 ((*- if type in ['pdf', 'svg', 'png', 'jpeg','html']*))
428 ((*- if type in ['pdf', 'svg', 'png', 'jpeg','html']*))
429 ((*- set is_figure = true -*))
429 ((*- set is_figure = true -*))
430 ((*- endif -*))
430 ((*- endif -*))
431 ((*- endfor -*))
431 ((*- endfor -*))
432
432
433 ((* if is_figure -*))
433 ((* if is_figure -*))
434 ((( true_content )))
434 ((( true_content )))
435 ((*- else -*))
435 ((*- else -*))
436 ((( false_content )))
436 ((( false_content )))
437 ((*- endif *))
437 ((*- endif *))
438 ((*- endmacro *))
438 ((*- endmacro *))
439
439
440 % Name: custom_verbatim
440 % Name: custom_verbatim
441 % Purpose: This macro creates a verbatim style block that fits the existing
441 % Purpose: This macro creates a verbatim style block that fits the existing
442 % sphinx style more readily than standard verbatim blocks.
442 % sphinx style more readily than standard verbatim blocks.
443 ((* macro custom_verbatim(text) -*))
443 ((* macro custom_verbatim(text) -*))
444 \begin{alltt}
444 \begin{alltt}
445 ((*- if resources.sphinx.centeroutput *))\begin{center} ((* endif -*))
445 ((*- if resources.sphinx.centeroutput *))\begin{center} ((* endif -*))
446 ((( text | wrap_text(wrap_size) )))
446 ((( text | wrap_text(wrap_size) | escape_latex )))
447 ((*- if resources.sphinx.centeroutput *))\end{center}((* endif -*))
447 ((*- if resources.sphinx.centeroutput *))\end{center}((* endif -*))
448 \end{alltt}
448 \end{alltt}
449 ((*- endmacro *))
449 ((*- endmacro *))
450
450
451 % Name: conditionally_center_output
451 % Name: conditionally_center_output
452 % Purpose: This macro centers the output if the output centering is enabled.
452 % Purpose: This macro centers the output if the output centering is enabled.
453 ((* macro conditionally_center_output(text) -*))
453 ((* macro conditionally_center_output(text) -*))
454 ((* if resources.sphinx.centeroutput *))
454 ((* if resources.sphinx.centeroutput *))
455 {\centering
455 {\centering
456 ((* endif *))
456 ((* endif *))
457 ((( text )))
457 ((( text )))
458 ((* if resources.sphinx.centeroutput *))}
458 ((* if resources.sphinx.centeroutput *))}
459 ((* endif *))
459 ((* endif *))
460 ((*- endmacro *))
460 ((*- endmacro *))
461
461
462 % Name: insert_graphics
462 % Name: insert_graphics
463 % Purpose: This macro will insert an image in the latex document given a path.
463 % Purpose: This macro will insert an image in the latex document given a path.
464 ((* macro insert_graphics(path) -*))
464 ((* macro insert_graphics(path) -*))
465 \begin{center}
465 \begin{center}
466 \includegraphics[max size={\textwidth}{\textheight}]{((( path )))}
466 \includegraphics[max size={\textwidth}{\textheight}]{((( path )))}
467 \par
467 \par
468 \end{center}
468 \end{center}
469 ((*- endmacro *))
469 ((*- endmacro *))
General Comments 0
You need to be logged in to leave comments. Login now