@@ -492,6 +492,25 @@ def count(x):
492492 return (list (x ) for _ , x in groupby (it , count ))
493493
494494
495+ def strip_latex_delimiters (source ):
496+ """Remove LaTeX math delimiters that would be rendered by the math block.
497+
498+ These are: ``\(…\)``, ``\[…\]``, ``$…$``, and ``$$…$$``.
499+ This is necessary because sphinx does not have a dedicated role for
500+ generic LaTeX, while Jupyter only defines generic LaTeX output, see
501+ https://github.com/jupyter/jupyter-sphinx/issues/90 for discussion.
502+ """
503+ source = source .strip ()
504+ delimiter_pairs = (
505+ pair .split () for pair in r'\( \),\[ \],$$ $$,$ $' .split (',' )
506+ )
507+ for start , end in delimiter_pairs :
508+ if source .startswith (start ) and source .endswith (end ):
509+ return source [len (start ):- len (end )]
510+
511+ return source
512+
513+
495514def cell_output_to_nodes (cell , data_priority , write_stderr , dir , thebe_config ):
496515 """Convert a jupyter cell with outputs and filenames to doctree nodes.
497516
@@ -582,7 +601,7 @@ def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
582601 ))
583602 elif mime_type == 'text/latex' :
584603 to_add .append (math_block (
585- text = data ,
604+ text = strip_latex_delimiters ( data ) ,
586605 nowrap = False ,
587606 number = None ,
588607 classes = ["output" , "text_latex" ]
0 commit comments