amcdawes / qutip-book

5 stars 8 forks source link

Formatting of qobj output #6

Open amcdawes opened 2 years ago

amcdawes commented 2 years ago

The output of qobj is awkward in the notebooks, the LaTeX formatting is nice for the math portion, but unnecessary for the text and structure parts. I've run across a discussion of this and need to track it down again.

Screen Shot 2022-01-06 at 6 54 35 AM
amcdawes commented 2 years ago

Yes, apparently I ran into this before: https://github.com/nteract/nteract/issues/2509

nteract version 0.28 does not show this error anymore, so hopefully there is a way to get jupyterbook to do the right thing too.

amcdawes commented 2 years ago

Inspecting a build file (Chapter 2) shows the following relevant snippet:

<div class="output text_latex math notranslate nohighlight">
\[\begin{split}Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket\begin{equation*}\left(\begin{array}{*{11}c}(0.810+1.262j)\\0.0\\\end{array}\right)\end{equation*}\end{split}\]</div>
</div>
</div>

so there is some wrapping (\begin{split}) that occurs, as well as a div assignment with classes:

I suspect the issue is the math class, because qobj._repr_latex_ is written to work as plain latex (i.e. not in math mode).

amcdawes commented 2 years ago

More digging. By comparing to the output of a qobj._repr_latex_ the wrapping is in fact \[\begin{split} on the front and \end{split}\] on the end. Removing these by hand un-breaks the output (i.e. it looks like it does in Jupyter notebook). Trying to figure out where this is added. The div classes are all ok (it looks correct with them in place, the formatting breaks from the extra LaTeX that was added.

This is a related open issue: https://github.com/executablebooks/jupyter-book/issues/1223

amcdawes commented 2 years ago

This appears to be coming from within Sphinx, according to https://www.sphinx-doc.org/en/1.0/ext/math.html "In addition, each single equation is set within a split environment"

Hint at a solution here: https://stackoverflow.com/questions/59875704/getting-multiline-docstring-equations-to-be-displayed-in-sphinx-potential-bug-i

to pass nowrap to Sphinx

which appears buried into the default output renderer: https://myst-nb.readthedocs.io/en/latest/_modules/myst_nb/render_outputs.html#CellOutputRenderer.render_text_latex

    def render_text_latex(self, output: NotebookNode, index: int):
        text = output["data"]["text/latex"]
        self.env.get_domain("math").data["has_equations"][self.env.docname] = True
        return [
            nodes.math_block(
                text=strip_latex_delimiters(text),
                nowrap=False,
                number=None,
                classes=["output", "text_latex"],
            )
        ]

It looks like there was some discussion on this in myst-nb and that nowrap=False is a hard-coded stopgap with known side-effects (https://github.com/executablebooks/MyST-NB/issues/99#issuecomment-608545104). I may open an issue there to raise the fact that QuTiP output breaks their assumption that latex output contains only math.

hodgestar commented 2 years ago

Thanks for digging into this. If improving something on the QuTiP side could help, let me know.

amcdawes commented 2 years ago

I've confirmed that one fix is setting nowrap=True within the myst_nb render_text_latex function mentioned above. I think this makes it something we can address there (either by overriding the default output renderer or by passing options to it somehow).