##// END OF EJS Templates
Add preamble support for fully self-contained output TeX files.
Fernando Perez -
Show More
@@ -0,0 +1,89 b''
1 %% This is the automatic preamble used by IPython. Note that it does *not*
2 %% include a documentclass declaration, that is added at runtime to the overall
3 %% document.
4
5 \usepackage{amsmath}
6 \usepackage{amssymb}
7 \usepackage{graphicx}
8
9 % Slightly bigger margins than the latex defaults
10 \usepackage{geometry}
11 \geometry{verbose,tmargin=3cm,bmargin=3cm,lmargin=2.5cm,rmargin=2.5cm}
12
13 % Define a few colors for use in code, links and cell shading
14 \usepackage{color}
15 \definecolor{orange}{cmyk}{0,0.4,0.8,0.2}
16 \definecolor{darkorange}{rgb}{.71,0.21,0.01}
17 \definecolor{darkgreen}{rgb}{.12,.54,.11}
18 \definecolor{myteal}{rgb}{.26, .44, .56}
19 \definecolor{gray}{rgb}{0.45, 0.45, 0.45}
20 \definecolor{lightgray}{rgb}{.95, .95, .95}
21 \definecolor{inputbackground}{rgb}{.95, .95, .85}
22 \definecolor{outputbackground}{rgb}{.95, .95, .95}
23 \definecolor{traceback}{rgb}{1, .95, .95}
24
25 % Framed environments for code cells (inputs, outputs, errors, ...). The
26 % various uses of \unskip (or not) at the end were fine-tuned by hand, so don't
27 % randomly change them unless you're sure of the effect it will have.
28 \usepackage{framed}
29
30 % remove extraneous vertical space in boxes
31 \setlength\fboxsep{0pt}
32
33 % codecell is the whole input+output set of blocks that a Code cell can
34 % generate.
35 \newenvironment{codecell}{%
36 \def\FrameCommand{\vrule width 0.5pt \hspace{5pt}}%
37 \MakeFramed{\FrameRestore}}
38 {\unskip\endMakeFramed}
39
40 \newenvironment{codeinput}{%
41 \def\FrameCommand{\colorbox{inputbackground}}%
42 \MakeFramed{\advance\hsize-\width \FrameRestore}}
43 {\unskip\endMakeFramed}
44
45 \newenvironment{codeoutput}{%
46 \def\FrameCommand{\colorbox{outputbackground}}%
47 \MakeFramed{\advance\hsize-\width \FrameRestore}}
48 {\unskip\medskip\endMakeFramed}
49
50 \newenvironment{traceback}{%
51 \def\FrameCommand{\colorbox{traceback}}%
52 \MakeFramed{\advance\hsize-\width \FrameRestore}}
53 {\endMakeFramed}
54
55 % The hyperref package gives us a pdf with properly built
56 % internal navigation ('pdf bookmarks' for the table of contents,
57 % internal cross-reference links, web links for URLs, etc.)
58 \usepackage{hyperref}
59 \hypersetup{
60 breaklinks=true, % so long urls are correctly broken across lines
61 colorlinks=true,
62 urlcolor=blue,
63 linkcolor=darkorange,
64 citecolor=darkgreen,
65 }
66
67 % Use and configure listings package for nicely formatted code
68 \usepackage{listings}
69 \lstset{
70 language=python,
71 aboveskip=\smallskipamount,
72 belowskip=\smallskipamount,
73 %xleftmargin=3mm,
74 breaklines=true,
75 basicstyle=\small \ttfamily,
76 showstringspaces=false,
77 keywordstyle=\color{blue}\bfseries,
78 commentstyle=\color{myteal},
79 stringstyle=\color{darkgreen},
80 identifierstyle=\color{darkorange},
81 }
82
83 % hardcode size of all verbatim environments to be a bit smaller
84 \makeatletter
85 \g@addto@macro\@verbatim\small
86 \makeatother
87
88 % Prevent overflowing lines due to urls and other hard-to-break entities.
89 \sloppy
@@ -86,6 +86,9 b' class Converter(object):'
86 86 infile_dir = str()
87 87 infile_root = str()
88 88 files_dir = str()
89 with_preamble = True
90 user_preamble = None
91 output = str()
89 92
90 93 def __init__(self, infile):
91 94 self.infile = infile
@@ -283,7 +286,7 b' class ConverterRST(Converter):'
283 286
284 287 @DocInherit
285 288 def _img_lines(self, img_file):
286 return ['.. image:: %s' % figfile, '']
289 return ['.. image:: %s' % img_file, '']
287 290
288 291 @DocInherit
289 292 def render_stream(self, output):
@@ -298,6 +301,7 b' class ConverterRST(Converter):'
298 301 def render_unknown(self, cell):
299 302 return rst_directive('.. warning:: Unknown cell') + [repr(cell)]
300 303
304
301 305 class ConverterQuickHTML(Converter):
302 306 extension = 'html'
303 307
@@ -400,7 +404,9 b' class ConverterLaTeX(Converter):'
400 404 (or set the equivalent flag at startup or in your configuration profile).
401 405 """
402 406 extension = 'tex'
403 heading_marker = {1: r'\section',
407 documentclass = 'article'
408 documentclass_options = '11pt,english'
409 heading_map = {1: r'\section',
404 410 2: r'\subsection',
405 411 3: r'\subsubsection',
406 412 4: r'\paragraph',
@@ -424,9 +430,48 b' class ConverterLaTeX(Converter):'
424 430 out.append(r'\end{%s}' % environment)
425 431 return out
426 432
433 def convert(self):
434 # The main body is done by the logic in the parent class, and that's
435 # all we need if preamble support has been turned off.
436 body = super(ConverterLaTeX, self).convert()
437 if not self.with_preamble:
438 return body
439 # But if preamble is on, then we need to construct a proper, standalone
440 # tex file.
441
442 # Tag the document at the top and set latex class
443 final = [ r'%% This file was auto-generated by IPython, do NOT edit',
444 r'%% Conversion from the original notebook file:',
445 r'%% {0}'.format(self.infile),
446 r'%%',
447 r'\documentclass[%s]{%s}' % (self.documentclass_options,
448 self.documentclass),
449 '',
450 ]
451 # Load our own preamble, which is stored next to the main file. We
452 # need to be careful in case the script entry point is a symlink
453 myfile = __file__ if not os.path.islink(__file__) else \
454 os.readlink(__file__)
455 with open(os.path.join(os.path.dirname(myfile), 'preamble.tex')) as f:
456 final.append(f.read())
457
458 # Load any additional user-supplied preamble
459 if self.user_preamble:
460 final.extend(['', '%% Adding user preamble from file:',
461 '%% {0}'.format(self.user_preamble), ''])
462 with open(self.user_preamble) as f:
463 final.append(f.read())
464
465 # Include document body
466 final.extend([ r'\begin{document}', '',
467 body,
468 r'\end{document}', ''])
469 # Retun value must be a string
470 return '\n'.join(final)
471
427 472 @DocInherit
428 473 def render_heading(self, cell):
429 marker = self.heading_marker[cell.level]
474 marker = self.heading_map[cell.level]
430 475 return ['%s{%s}\n\n' % (marker, cell.source) ]
431 476
432 477 @DocInherit
General Comments 0
You need to be logged in to leave comments. Login now