##// END OF EJS Templates
Include mdframed with nbconvert, reference directly
Jonathan Frederic -
Show More
@@ -1,73 +1,75 b''
1 1 """Module that allows latex output notebooks to be conditioned before
2 2 they are converted.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2013, the IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 from __future__ import print_function, absolute_import
17 import os
17 18
18 19 # Third-party import, needed for Pygments latex definitions.
19 20 from pygments.formatters import LatexFormatter
20 21
21 22 # ipy imports
22 23 from .base import (Preprocessor)
23 24 from IPython.nbconvert import filters
24 25
25 26 #-----------------------------------------------------------------------------
26 27 # Classes
27 28 #-----------------------------------------------------------------------------
28 29
29 30 class LatexPreprocessor(Preprocessor):
30 31 """
31 32 Converter for latex destined documents.
32 33 """
33 34
34 35 def preprocess(self, nb, resources):
35 36 """
36 37 Preprocessing to apply on each notebook.
37 38
38 39 Parameters
39 40 ----------
40 41 nb : NotebookNode
41 42 Notebook being converted
42 43 resources : dictionary
43 44 Additional resources used in the conversion process. Allows
44 45 preprocessors to pass variables into the Jinja engine.
45 46 """
46 47 # Generate Pygments definitions for Latex
47 48 resources["latex"] = {}
48 49 resources["latex"]["pygment_definitions"] = LatexFormatter().get_style_defs()
50 resources["latex"]["mdframed_path"] = os.path.join(os.path.dirname(__file__), '..', 'templates', 'latex', 'mdframed')
49 51 return super(LatexPreprocessor, self).preprocess(nb, resources)
50 52
51 53
52 54 def preprocess_cell(self, cell, resources, index):
53 55 """
54 56 Apply a transformation on each cell,
55 57
56 58 Parameters
57 59 ----------
58 60 cell : NotebookNode cell
59 61 Notebook cell being processed
60 62 resources : dictionary
61 63 Additional resources used in the conversion process. Allows
62 64 preprocessors to pass variables into the Jinja engine.
63 65 index : int
64 66 Modified index of the cell being processed (see base.py)
65 67 """
66 68
67 69 #If the cell is a markdown cell, preprocess the ampersands used to
68 70 #remove the space between them and their contents. Latex will complain
69 71 #if spaces exist between the ampersands and the math content.
70 72 #See filters.latex.rm_math_space for more information.
71 73 if hasattr(cell, "source") and cell.cell_type == "markdown":
72 74 cell.source = filters.strip_math_space(cell.source)
73 75 return cell, resources
@@ -1,126 +1,126 b''
1 1 ((= Notebook input/output style =))
2 2
3 3 ((* extends 'latex_base.tplx' *))
4 4
5 5 % Custom packages
6 6 ((* block packages *))
7 7 ((( super() )))
8 8
9 9 % Needed to box output/input
10 10 \usepackage{tikz}
11 11 \usetikzlibrary{calc,arrows,shadows}
12 \usepackage[framemethod=tikz]{mdframed}
12 \usepackage[framemethod=tikz]{((( resources.latex.mdframed_path | posix_path )))/mdframed}
13 13
14 14 \usepackage{scrextend} % Used to indent output
15 15 \usepackage{needspace} % Make prompts follow contents
16 16 ((* endblock packages *))
17 17
18 18 % Custom definitions
19 19 ((* block definitions *))
20 20 ((( super() )))
21 21
22 22 \listfiles
23 23
24 24 \def\smaller{\fontsize{9.5pt}{9.5pt}\selectfont}
25 25
26 26 \definecolor{nbframe-border}{rgb}{0.867,0.867,0.867}
27 27 \definecolor{nbframe-bg}{rgb}{0.969,0.969,0.969}
28 28 \definecolor{nbframe-in-prompt}{rgb}{0.0,0.0,0.502}
29 29 \definecolor{nbframe-out-prompt}{rgb}{0.545,0.0,0.0}
30 30
31 31 \newenvironment{ColorVerbatim}
32 32 {\vspace{-2\baselineskip}
33 33 \leavevmode\begin{mdframed}[%
34 34 roundcorner=1.0pt, %
35 35 backgroundcolor=nbframe-bg, %
36 36 userdefinedwidth=1\linewidth, %
37 37 leftmargin=0.1\linewidth, %
38 38 innerleftmargin=0pt, %
39 39 innerrightmargin=0pt, %
40 40 linecolor=nbframe-border, %
41 41 linewidth=1pt, %
42 42 usetwoside=false, %
43 43 everyline=false, %
44 44 innerlinewidth=3pt, %
45 45 innerlinecolor=nbframe-bg, %
46 46 middlelinewidth=1pt, %
47 47 middlelinecolor=nbframe-bg, %
48 48 outerlinewidth=0.5pt, %
49 49 outerlinecolor=nbframe-border, %
50 50 needspace=3em, %
51 51 nobreak=false
52 52 ]}
53 53 {\end{mdframed}\vspace{-1\baselineskip}}
54 54
55 55 % Space needed to start a new input/output
56 56 \newlength{\promptspace}
57 57 \setlength{\promptspace}{4\baselineskip}
58 58 ((* endblock definitions *))
59 59
60 60 %===============================================================================
61 61 % Input
62 62 %===============================================================================
63 63
64 64
65 65 ((* block in_prompt scoped *))
66 66 \br
67 67 ((( draw_prompt("In", cell.prompt_number, "nbframe-in-prompt") )))
68 68 ((* endblock in_prompt *))
69 69
70 70 ((* block input scoped *))
71 71 % Add contents below.
72 72
73 73 \begin{ColorVerbatim}
74 74 \smaller{\leavevmode\hspace*{-0.1\linewidth}((( super() )))}
75 75 \end{ColorVerbatim}
76 76 ((* endblock input *))
77 77
78 78 ((* block input_group scoped *))
79 79 ((( super() )))
80 80 ((* endblock input_group *))
81 81
82 82
83 83 %===============================================================================
84 84 % Output
85 85 %===============================================================================
86 86
87 87 ((* block output_group *))
88 88 {\br}((( super() )))
89 89 ((* endblock output_group *))
90 90
91 91 ((* block output *))
92 92
93 93 % Only render the prompt if the cell is pyout. Note, the outputs prompt
94 94 % block isn't used since we need to check each indiviual output and only
95 95 % add prompts to the pyout ones.
96 96 ((* if output.output_type in ['pyout'] *))
97 97 ((( draw_prompt("Out", cell.prompt_number, "nbframe-out-prompt") )))
98 98 ((* endif *))
99 99
100 100 \begin{addmargin}[0.1\linewidth]{0em} % left, right
101 101 \smaller{((( super() )))}
102 102 \end{addmargin}
103 103 ((* endblock output *))
104 104
105 105 %==============================================================================
106 106 % Support Macros
107 107 %==============================================================================
108 108
109 109 % Name: draw_prompt
110 110 % Purpose: Renders an output/input prompt for notebook style pdfs. Prompt is
111 111 % rendered at the current location, the cursor position is left
112 112 % unmodified.
113 113 ((* macro draw_prompt(prompt, number, color) *))
114 114 \needspace{\promptspace}
115 115
116 116 {
117 117 \smaller
118 118 \tt
119 119 \color{((( color )))}
120 120 \noindent
121 121 ((( prompt )))
122 122 {[}((( number ))){]}:
123 123 }
124 124
125 125 \vspace{-1\baselineskip}
126 126 ((* endmacro *))
General Comments 0
You need to be logged in to leave comments. Login now