Show More
@@ -66,6 +66,7 b' from IPython.core.prefilter import PrefilterManager' | |||||
66 | from IPython.core.profiledir import ProfileDir |
|
66 | from IPython.core.profiledir import ProfileDir | |
67 | from IPython.core.pylabtools import pylab_activate |
|
67 | from IPython.core.pylabtools import pylab_activate | |
68 | from IPython.core.prompts import PromptManager |
|
68 | from IPython.core.prompts import PromptManager | |
|
69 | from IPython.lib.latextools import LaTeXTool | |||
69 | from IPython.utils import PyColorize |
|
70 | from IPython.utils import PyColorize | |
70 | from IPython.utils import io |
|
71 | from IPython.utils import io | |
71 | from IPython.utils import py3compat |
|
72 | from IPython.utils import py3compat | |
@@ -497,6 +498,7 b' class InteractiveShell(SingletonConfigurable):' | |||||
497 | self.init_display_pub() |
|
498 | self.init_display_pub() | |
498 | self.init_displayhook() |
|
499 | self.init_displayhook() | |
499 | self.init_reload_doctest() |
|
500 | self.init_reload_doctest() | |
|
501 | self.init_latextool() | |||
500 | self.init_magics() |
|
502 | self.init_magics() | |
501 | self.init_logstart() |
|
503 | self.init_logstart() | |
502 | self.init_pdb() |
|
504 | self.init_pdb() | |
@@ -690,6 +692,12 b' class InteractiveShell(SingletonConfigurable):' | |||||
690 | except ImportError: |
|
692 | except ImportError: | |
691 | warn("doctest module does not exist.") |
|
693 | warn("doctest module does not exist.") | |
692 |
|
694 | |||
|
695 | def init_latextool(self): | |||
|
696 | """Configure LaTeXTool.""" | |||
|
697 | cfg = LaTeXTool(config=self.config) | |||
|
698 | if cfg not in self.configurables: | |||
|
699 | self.configurables.append(cfg) | |||
|
700 | ||||
693 | def init_virtualenv(self): |
|
701 | def init_virtualenv(self): | |
694 | """Add a virtualenv to sys.path so the user can import modules from it. |
|
702 | """Add a virtualenv to sys.path so the user can import modules from it. | |
695 | This isn't perfect: it doesn't use the Python interpreter with which the |
|
703 | This isn't perfect: it doesn't use the Python interpreter with which the |
@@ -25,13 +25,50 b' import shutil' | |||||
25 | import subprocess |
|
25 | import subprocess | |
26 |
|
26 | |||
27 | from IPython.utils.process import find_cmd, FindCmdError |
|
27 | from IPython.utils.process import find_cmd, FindCmdError | |
|
28 | from IPython.config.configurable import SingletonConfigurable | |||
|
29 | from IPython.utils.traitlets import Instance, List, CBool, CUnicode | |||
28 |
|
30 | |||
29 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
30 | # Tools |
|
32 | # Tools | |
31 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
32 |
|
34 | |||
33 |
|
35 | |||
34 | def latex_to_png(s, encode=False, backend='mpl', wrap=False): |
|
36 | class LaTeXTool(SingletonConfigurable): | |
|
37 | """An object to store configuration of the LaTeX tool.""" | |||
|
38 | ||||
|
39 | backends = List( | |||
|
40 | CUnicode, ["matplotlib", "dvipng"], | |||
|
41 | help="Preferred backend to draw LaTeX math equations. " | |||
|
42 | "Backends in the list are checked one by one and the first " | |||
|
43 | "usable one is used. Note that `matplotlib` backend " | |||
|
44 | "is usable only for inline style equations. To draw " | |||
|
45 | "display style equations, `dvipng` backend must be specified. ", | |||
|
46 | # It is a List instead of Enum, to make configuration more | |||
|
47 | # flexible. For example, to use matplotlib mainly but dvipng | |||
|
48 | # for display style, the default ["matplotlib", "dvipng"] can | |||
|
49 | # be used. To NOT use dvipng so that other repr such as | |||
|
50 | # unicode pretty printing is used, you can use ["matplotlib"]. | |||
|
51 | config=True) | |||
|
52 | ||||
|
53 | use_breqn = CBool( | |||
|
54 | True, | |||
|
55 | help="Use breqn.sty to automatically break long equations. " | |||
|
56 | "This configuration takes effect only for dvipng backend.", | |||
|
57 | config=True) | |||
|
58 | ||||
|
59 | packages = List( | |||
|
60 | ['amsmath', 'amsthm', 'amssymb', 'bm'], | |||
|
61 | help="A list of packages to use for dvipng backend. " | |||
|
62 | "'breqn' will be automatically appended when use_breqn=True.", | |||
|
63 | config=True) | |||
|
64 | ||||
|
65 | preamble = CUnicode( | |||
|
66 | help="Additional preamble to use when generating LaTeX source " | |||
|
67 | "for dvipng backend.", | |||
|
68 | config=True) | |||
|
69 | ||||
|
70 | ||||
|
71 | def latex_to_png(s, encode=False, backend=None, wrap=False): | |||
35 | """Render a LaTeX string to PNG. |
|
72 | """Render a LaTeX string to PNG. | |
36 |
|
73 | |||
37 | Parameters |
|
74 | Parameters | |
@@ -40,7 +77,7 b" def latex_to_png(s, encode=False, backend='mpl', wrap=False):" | |||||
40 | The raw string containing valid inline LaTeX. |
|
77 | The raw string containing valid inline LaTeX. | |
41 | encode : bool, optional |
|
78 | encode : bool, optional | |
42 | Should the PNG data bebase64 encoded to make it JSON'able. |
|
79 | Should the PNG data bebase64 encoded to make it JSON'able. | |
43 | backend : {mpl, dvipng} |
|
80 | backend : {matplotlib, dvipng} | |
44 | Backend for producing PNG data. |
|
81 | Backend for producing PNG data. | |
45 | wrap : bool |
|
82 | wrap : bool | |
46 | If true, Automatically wrap `s` as a LaTeX equation. |
|
83 | If true, Automatically wrap `s` as a LaTeX equation. | |
@@ -48,7 +85,12 b" def latex_to_png(s, encode=False, backend='mpl', wrap=False):" | |||||
48 | None is returned when the backend cannot be used. |
|
85 | None is returned when the backend cannot be used. | |
49 |
|
86 | |||
50 | """ |
|
87 | """ | |
51 | if backend == 'mpl': |
|
88 | allowed_backends = LaTeXTool.instance().backends | |
|
89 | if backend is None: | |||
|
90 | backend = allowed_backends[0] | |||
|
91 | if backend not in allowed_backends: | |||
|
92 | return None | |||
|
93 | if backend == 'matplotlib': | |||
52 | f = latex_to_png_mpl |
|
94 | f = latex_to_png_mpl | |
53 | elif backend == 'dvipng': |
|
95 | elif backend == 'dvipng': | |
54 | f = latex_to_png_dvipng |
|
96 | f = latex_to_png_dvipng | |
@@ -120,14 +162,17 b' def kpsewhich(filename):' | |||||
120 |
|
162 | |||
121 | def genelatex(body, wrap): |
|
163 | def genelatex(body, wrap): | |
122 | """Generate LaTeX document for dvipng backend.""" |
|
164 | """Generate LaTeX document for dvipng backend.""" | |
123 | breqn = wrap and kpsewhich("breqn.sty") |
|
165 | lt = LaTeXTool.instance() | |
|
166 | breqn = wrap and lt.use_breqn and kpsewhich("breqn.sty") | |||
124 | yield r'\documentclass{article}' |
|
167 | yield r'\documentclass{article}' | |
125 | packages = ['amsmath', 'amsthm', 'amssymb', 'bm'] |
|
168 | packages = lt.packages | |
126 | if breqn: |
|
169 | if breqn: | |
127 |
packages |
|
170 | packages = packages + ['breqn'] | |
128 | for pack in packages: |
|
171 | for pack in packages: | |
129 | yield r'\usepackage{{{0}}}'.format(pack) |
|
172 | yield r'\usepackage{{{0}}}'.format(pack) | |
130 | yield r'\pagestyle{empty}' |
|
173 | yield r'\pagestyle{empty}' | |
|
174 | if lt.preamble: | |||
|
175 | yield lt.preamble | |||
131 | yield r'\begin{document}' |
|
176 | yield r'\begin{document}' | |
132 | if breqn: |
|
177 | if breqn: | |
133 | yield r'\begin{dmath*}' |
|
178 | yield r'\begin{dmath*}' |
General Comments 0
You need to be logged in to leave comments.
Login now