##// END OF EJS Templates
Make lib.latextools configurable...
Takafumi Arakaki -
Show More
@@ -66,6 +66,7 b' from IPython.core.prefilter import PrefilterManager'
66 66 from IPython.core.profiledir import ProfileDir
67 67 from IPython.core.pylabtools import pylab_activate
68 68 from IPython.core.prompts import PromptManager
69 from IPython.lib.latextools import LaTeXTool
69 70 from IPython.utils import PyColorize
70 71 from IPython.utils import io
71 72 from IPython.utils import py3compat
@@ -497,6 +498,7 b' class InteractiveShell(SingletonConfigurable):'
497 498 self.init_display_pub()
498 499 self.init_displayhook()
499 500 self.init_reload_doctest()
501 self.init_latextool()
500 502 self.init_magics()
501 503 self.init_logstart()
502 504 self.init_pdb()
@@ -689,7 +691,13 b' class InteractiveShell(SingletonConfigurable):'
689 691 doctest_reload()
690 692 except ImportError:
691 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 701 def init_virtualenv(self):
694 702 """Add a virtualenv to sys.path so the user can import modules from it.
695 703 This isn't perfect: it doesn't use the Python interpreter with which the
@@ -25,13 +25,50 b' import shutil'
25 25 import subprocess
26 26
27 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 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 72 """Render a LaTeX string to PNG.
36 73
37 74 Parameters
@@ -40,7 +77,7 b" def latex_to_png(s, encode=False, backend='mpl', wrap=False):"
40 77 The raw string containing valid inline LaTeX.
41 78 encode : bool, optional
42 79 Should the PNG data bebase64 encoded to make it JSON'able.
43 backend : {mpl, dvipng}
80 backend : {matplotlib, dvipng}
44 81 Backend for producing PNG data.
45 82 wrap : bool
46 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 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 94 f = latex_to_png_mpl
53 95 elif backend == 'dvipng':
54 96 f = latex_to_png_dvipng
@@ -120,14 +162,17 b' def kpsewhich(filename):'
120 162
121 163 def genelatex(body, wrap):
122 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 167 yield r'\documentclass{article}'
125 packages = ['amsmath', 'amsthm', 'amssymb', 'bm']
168 packages = lt.packages
126 169 if breqn:
127 packages.append('breqn')
170 packages = packages + ['breqn']
128 171 for pack in packages:
129 172 yield r'\usepackage{{{0}}}'.format(pack)
130 173 yield r'\pagestyle{empty}'
174 if lt.preamble:
175 yield lt.preamble
131 176 yield r'\begin{document}'
132 177 if breqn:
133 178 yield r'\begin{dmath*}'
General Comments 0
You need to be logged in to leave comments. Login now