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