Repository: jupyter/jupyter-sphinx

Issue:
Dollar signs when displaying sympy objects
Check the following examples:

https://qsymm.readthedocs.io/en/stable-1.2/tutorial/basics.html

![2019-11-11-150618_851x262_scrot](https://user-images.githubusercontent.com/6654709/68596513-a69d7380-049b-11ea-859a-fc2158cedf74.png)

The dollar signs do not appear when viewing the notebook version of the documentation

Discussion:

Patch:
diff --git a/jupyter_sphinx/execute.py b/jupyter_sphinx/execute.py
index 336b1f6..4061aec 100644
--- a/jupyter_sphinx/execute.py
+++ b/jupyter_sphinx/execute.py
@@ -492,6 +492,25 @@ def split_on(pred, it):
     return (list(x) for _, x in groupby(it, count))
 
 
+def strip_latex_delimiters(source):
+    """Remove LaTeX math delimiters that would be rendered by the math block.
+
+    These are: ``\(…\)``, ``\[…\]``, ``$…$``, and ``$$…$$``.
+    This is necessary because sphinx does not have a dedicated role for
+    generic LaTeX, while Jupyter only defines generic LaTeX output, see
+    https://github.com/jupyter/jupyter-sphinx/issues/90 for discussion.
+    """
+    source = source.strip()
+    delimiter_pairs = (
+        pair.split() for pair in r'\( \),\[ \],$$ $$,$ $'.split(',')
+    )
+    for start, end in delimiter_pairs:
+        if source.startswith(start) and source.endswith(end):
+            return source[len(start):-len(end)]
+
+    return source
+
+
 def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
     """Convert a jupyter cell with outputs and filenames to doctree nodes.
 
@@ -582,7 +601,7 @@ def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
                 ))
             elif mime_type == 'text/latex':
                 to_add.append(math_block(
-                    text=data,
+                    text=strip_latex_delimiters(data),
                     nowrap=False,
                     number=None,
                     classes=["output", "text_latex"]


Test patch:
diff --git a/tests/test_execute.py b/tests/test_execute.py
index f1908ab..851c487 100644
--- a/tests/test_execute.py
+++ b/tests/test_execute.py
@@ -407,3 +407,21 @@ def test_thebe_button_none(doctree):
     source = "No Jupyter cells"
     tree = doctree(source, config)
     assert len(tree.traverse(ThebeButtonNode)) == 0
+
+
+def test_latex(doctree):
+    source = r"""
+    .. jupyter-execute::
+
+        from IPython.display import Latex
+        Latex(r"{}\int{}")
+    """
+
+    delimiter_pairs = (
+        pair.split() for pair in r'\( \),\[ \],$$ $$,$ $'.split(',')
+    )
+
+    for start, end in delimiter_pairs:
+        tree = doctree(source.format(start, end))
+        cell, = tree.traverse(JupyterCellNode)
+        assert cell.children[1].astext() == r'\int'