##// END OF EJS Templates
Systematic test of ipynb -> * conversion...
Matthias BUSSONNIER -
Show More

The requested changes are too big and content was truncated. Show full diff

1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,191 +1,192 b''
1 1 from converters.base import Converter
2 2 from converters.utils import markdown2latex, remove_ansi
3 3 import os, sys
4 4 import subprocess
5 5
6 6
7 7 inkscape = 'inkscape'
8 8 if sys.platform == 'darwin':
9 9 inkscape = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape'
10 10 if not os.path.exists(inkscape):
11 11 inkscape = None
12 12
13 13 class ConverterLaTeX(Converter):
14 14 """Converts a notebook to a .tex file suitable for pdflatex.
15 15
16 16 Note: this converter *needs*:
17 17
18 18 - `pandoc`: for all conversion of markdown cells. If your notebook only
19 19 has Raw cells, pandoc will not be needed.
20 20
21 21 - `inkscape`: if your notebook has SVG figures. These need to be
22 22 converted to PDF before inclusion in the TeX file, as LaTeX doesn't
23 23 understand SVG natively.
24 24
25 25 You will in general obtain much better final PDF results if you configure
26 26 the matplotlib backend to create SVG output with
27 27
28 28 %config InlineBackend.figure_format = 'svg'
29 29
30 30 (or set the equivalent flag at startup or in your configuration profile).
31 31 """
32 inkscape = inkscape
32 33 extension = 'tex'
33 34 documentclass = 'article'
34 35 documentclass_options = '11pt,english'
35 36 heading_map = {1: r'\section',
36 37 2: r'\subsection',
37 38 3: r'\subsubsection',
38 39 4: r'\paragraph',
39 40 5: r'\subparagraph',
40 41 6: r'\subparagraph'}
41 42
42 43 def in_env(self, environment, lines):
43 44 """Return list of environment lines for input lines
44 45
45 46 Parameters
46 47 ----------
47 48 env : string
48 49 Name of the environment to bracket with begin/end.
49 50
50 51 lines: """
51 52 out = [ur'\begin{%s}' % environment]
52 53 if isinstance(lines, basestring):
53 54 out.append(lines)
54 55 else: # list
55 56 out.extend(lines)
56 57 out.append(ur'\end{%s}' % environment)
57 58 return out
58 59
59 60 def convert(self, *kwargs):
60 61 # The main body is done by the logic in the parent class, and that's
61 62 # all we need if preamble support has been turned off.
62 63 body = super(ConverterLaTeX, self).convert(*kwargs)
63 64 if not self.with_preamble:
64 65 return body
65 66 # But if preamble is on, then we need to construct a proper, standalone
66 67 # tex file.
67 68
68 69 # Tag the document at the top and set latex class
69 70 final = [ r'%% This file was auto-generated by IPython, do NOT edit',
70 71 r'%% Conversion from the original notebook file:',
71 72 r'%% {0}'.format(self.infile),
72 73 r'%%',
73 74 r'\documentclass[%s]{%s}' % (self.documentclass_options,
74 75 self.documentclass),
75 76 '',
76 77 ]
77 78 # Load our own preamble, which is stored next to the main file. We
78 79 # need to be careful in case the script entry point is a symlink
79 80 myfile = os.path.realpath(__file__)
80 81 with open(os.path.join(os.path.dirname(myfile), '../preamble.tex')) as f:
81 82 final.append(f.read())
82 83
83 84 # Load any additional user-supplied preamble
84 85 if self.user_preamble:
85 86 final.extend(['', '%% Adding user preamble from file:',
86 87 '%% {0}'.format(self.user_preamble), ''])
87 88 with open(self.user_preamble) as f:
88 89 final.append(f.read())
89 90
90 91 # Include document body
91 92 final.extend([ r'\begin{document}', '',
92 93 body,
93 94 r'\end{document}', ''])
94 95 # Retun value must be a string
95 96 return '\n'.join(final)
96 97
97 98 def render_heading(self, cell):
98 99 marker = self.heading_map[cell.level]
99 100 return ['%s{%s}' % (marker, cell.source) ]
100 101
101 102 def render_code(self, cell):
102 103 if not cell.input:
103 104 return []
104 105
105 106 # Cell codes first carry input code, we use lstlisting for that
106 107 lines = [ur'\begin{codecell}']
107 108
108 109 lines.extend(self.in_env('codeinput',
109 110 self.in_env('lstlisting', cell.input)))
110 111
111 112 outlines = []
112 113 for output in cell.outputs:
113 114 conv_fn = self.dispatch(output.output_type)
114 115 outlines.extend(conv_fn(output))
115 116
116 117 # And then output of many possible types; use a frame for all of it.
117 118 if outlines:
118 119 lines.extend(self.in_env('codeoutput', outlines))
119 120
120 121 lines.append(ur'\end{codecell}')
121 122
122 123 return lines
123 124
124 125
125 126 def _img_lines(self, img_file):
126 127 return self.in_env('center',
127 128 [r'\includegraphics[width=6in]{%s}' % img_file, r'\par'])
128 129
129 130 def _svg_lines(self, img_file):
130 131 base_file = os.path.splitext(img_file)[0]
131 132 pdf_file = base_file + '.pdf'
132 subprocess.check_call([ inkscape, '--export-pdf=%s' % pdf_file,
133 subprocess.check_call([ self.inkscape, '--export-pdf=%s' % pdf_file,
133 134 img_file])
134 135 return self._img_lines(pdf_file)
135 136
136 137 def render_markdown(self, cell):
137 138 return [markdown2latex(cell.source)]
138 139
139 140 def render_pyout(self, output):
140 141 lines = []
141 142
142 143 # output is a dictionary like object with type as a key
143 144 if 'latex' in output:
144 145 lines.extend(output.latex)
145 146
146 147 if 'text' in output:
147 148 lines.extend(self.in_env('verbatim', output.text))
148 149
149 150 return lines
150 151
151 152 def render_pyerr(self, output):
152 153 # Note: a traceback is a *list* of frames.
153 154 return self.in_env('traceback',
154 155 self.in_env('verbatim',
155 156 remove_ansi('\n'.join(output.traceback))))
156 157
157 158 def render_raw(self, cell):
158 159 if self.raw_as_verbatim:
159 160 return self.in_env('verbatim', cell.source)
160 161 else:
161 162 return [cell.source]
162 163
163 164 def _unknown_lines(self, data):
164 165 return [r'{\vspace{5mm}\bf WARNING:: unknown cell:}'] + \
165 166 self.in_env('verbatim', data)
166 167
167 168
168 169 def render_display_format_text(self, output):
169 170 lines = []
170 171
171 172 if 'text' in output:
172 173 lines.extend(self.in_env('verbatim', output.text.strip()))
173 174
174 175 return lines
175 176
176 177 def render_display_format_html(self, output):
177 178 return []
178 179
179 180 def render_display_format_latex(self, output):
180 181 if type(output.latex) == type([]):
181 182 return output.latex
182 183 return [output.latex]
183 184
184 185 def render_display_format_json(self, output):
185 186 # latex ignores json
186 187 return []
187 188
188 189
189 190 def render_display_format_javascript(self, output):
190 191 # latex ignores javascript
191 192 return []
General Comments 0
You need to be logged in to leave comments. Login now