diff --git a/IPython/core/display.py b/IPython/core/display.py index 7f5cc6d..dc80c3a 100644 --- a/IPython/core/display.py +++ b/IPython/core/display.py @@ -298,6 +298,13 @@ class HTML(DisplayObject): class Math(DisplayObject): def _repr_latex_(self): + s = self.data.strip('$') + return "$$%s$$" % s + + +class Latex(DisplayObject): + + def _repr_latex_(self): return self.data diff --git a/IPython/core/formatters.py b/IPython/core/formatters.py index cadf59d..893e0a9 100644 --- a/IPython/core/formatters.py +++ b/IPython/core/formatters.py @@ -524,7 +524,8 @@ class LatexFormatter(BaseFormatter): this. The return value of this formatter should be a valid LaTeX equation, - enclosed in either ```$``` or ```$$```. + enclosed in either ```$```, ```$$``` or another LaTeX equation + environment. """ format_type = Unicode('text/latex') diff --git a/docs/examples/notebooks/00_notebook_tour.ipynb b/docs/examples/notebooks/00_notebook_tour.ipynb index c1db0df..1b34ca1 100644 --- a/docs/examples/notebooks/00_notebook_tour.ipynb +++ b/docs/examples/notebooks/00_notebook_tour.ipynb @@ -855,7 +855,7 @@ "Note that this is *different* from the above examples. Above we were typing mathematical expressions", "in Markdown cells (along with normal text) and letting the browser render them; now we are displaying", "the output of a Python computation as a LaTeX expression wrapped by the `Math()` object so the browser", - "renders it:" + "renders it. The `Math` object will add the needed LaTeX delimiters (`$$`) if they are not provided:" ] }, { @@ -863,22 +863,59 @@ "collapsed": false, "input": [ "from IPython.core.display import Math", - "Math(r'$F(k) = \\int_{-\\infty}^{\\infty} f(x) e^{2\\pi i k} dx$')" + "Math(r'F(k) = \\int_{-\\infty}^{\\infty} f(x) e^{2\\pi i k} dx')" ], "language": "python", "outputs": [ { "latex": [ - "$F(k) = \\int_{-\\infty}^{\\infty} f(x) e^{2\\pi i k} dx$" + "$$F(k) = \\int_{-\\infty}^{\\infty} f(x) e^{2\\pi i k} dx$$" ], "output_type": "pyout", - "prompt_number": 8, + "prompt_number": 1, "text": [ - "<IPython.core.display.Math at 0x45840d0>" + "" ] } ], - "prompt_number": 8 + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "source": [ + "With the `Latex` class, you have to include the delimiters yourself. This allows you to use other LaTeX modes such as `eqnarray`:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from IPython.core.display import Latex", + "Latex(r\"\"\"\\begin{eqnarray}", + "\\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} & = \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\", + "\\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\", + "\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\", + "\\nabla \\cdot \\vec{\\mathbf{B}} & = 0 ", + "\\end{eqnarray}\"\"\")" + ], + "language": "python", + "outputs": [ + { + "latex": [ + "\\begin{eqnarray}", + "\\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} & = \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\ \\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\", + "\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\", + "\\nabla \\cdot \\vec{\\mathbf{B}} & = 0 ", + "\\end{eqnarray}" + ], + "output_type": "pyout", + "prompt_number": 5, + "text": [ + "" + ] + } + ], + "prompt_number": 5 }, { "cell_type": "markdown",