From 749315e9f4e771cd4917d3f5110b27117c7727cf Mon Sep 17 00:00:00 2001 From: Matthias Geier Date: Mon, 10 Dec 2018 19:45:15 +0100 Subject: [PATCH] Use custom role "nbsphinx-math" for inline LaTeX --- src/nbsphinx.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/nbsphinx.py b/src/nbsphinx.py index fb19d594..a80e9e98 100644 --- a/src/nbsphinx.py +++ b/src/nbsphinx.py @@ -835,6 +835,15 @@ def parse(self, inputstring, document): env.doc2path(env.docname, base=None) + ':\n' + str(e)) + rststring = """ +.. role:: nbsphinx-math(raw) + :format: latex + html + :class: math + +.. + +""" + rststring + # Create additional output files (figures etc.), # see nbconvert.writers.FilesWriter.write() for filename, data in resources.get('outputs', {}).items(): @@ -1059,16 +1068,6 @@ def markdown2rst(text): """ - def displaymath(text): - return { - 't': 'Math', - 'c': [ - {'t': 'DisplayMath', 'c': []}, - # Special marker characters are removed below: - '\x0e:nowrap:\x0f\n\n' + text, - ] - } - def parse_html(obj): p = CitationParser() p.feed(obj['c'][1]) @@ -1088,9 +1087,17 @@ def object_hook(obj): if obj.get('t') == 'RawBlock' and obj['c'][0] == 'latex': obj['t'] = 'Para' - obj['c'] = [displaymath(obj['c'][1])] + obj['c'] = [{ + 't': 'Math', + 'c': [ + {'t': 'DisplayMath'}, + # Special marker characters are removed below: + '\x0e:nowrap:\x0f\n\n' + obj['c'][1], + ] + }] elif obj.get('t') == 'RawInline' and obj['c'][0] == 'tex': - obj = displaymath(obj['c'][1]) + obj = {'t': 'RawInline', + 'c': ['rst', ':nbsphinx-math:`{}`'.format(obj['c'][1])]} elif obj.get('t') == 'RawInline' and obj['c'][0] == 'html': p = parse_html(obj) if p.starttag: