@@ -492,6 +492,25 @@ def count(x):
492
492
return (list (x ) for _ , x in groupby (it , count ))
493
493
494
494
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
+
495
514
def cell_output_to_nodes (cell , data_priority , write_stderr , dir , thebe_config ):
496
515
"""Convert a jupyter cell with outputs and filenames to doctree nodes.
497
516
@@ -582,7 +601,7 @@ def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
582
601
))
583
602
elif mime_type == 'text/latex' :
584
603
to_add .append (math_block (
585
- text = data ,
604
+ text = strip_latex_delimiters ( data ) ,
586
605
nowrap = False ,
587
606
number = None ,
588
607
classes = ["output" , "text_latex" ]
0 commit comments