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