sphinx-simplepdf icon indicating copy to clipboard operation
sphinx-simplepdf copied to clipboard

Test latex equations

Open danwos opened this issue 3 years ago • 13 comments

Looks very nice, but it seems to display the latex input instead of rending the equations (as intended by latex) ? Sorry if you got two copies of this post, I was trying to fix a typo in the previous version.

Not sure which mechanism is used here. Maybe it is really raw-latex code, then it would not be possible.

But if the author is using some of the HTML math extensions, it should work: https://www.sphinx-doc.org/en/master/usage/extensions/math.html

But we should test and document it.

https://groups.google.com/g/sphinx-users/c/r4ahhcagNK0/m/PMZ9EDJYAQAJ

danwos avatar Aug 29 '22 06:08 danwos

Found the link for the discussion you're referring to: https://groups.google.com/g/sphinx-users/c/r4ahhcagNK0/m/PMZ9EDJYAQAJ

But that's not me.

btb avatar Aug 30 '22 07:08 btb

Ohh ok, sorry, I remove your name from the issue. :/

danwos avatar Aug 30 '22 08:08 danwos

Ran into this issue with our local documentation. Currently there seems to be no native math implementation for html in sphinx, just with mathjax or rendering as image. Cause image rendering is not so good (e.g. due to zooming/scaling in some html viewers used to display documentation) a better solution should be provided.

As it seems to fully ship mathjax in the html build seems to be no good idea. to have it locally installed for the pdf builds seems ok.

Due to your knowledge: Is there any other (maybe simpler) solution available, maybe with another directive?

kreuzberger avatar May 05 '23 07:05 kreuzberger

Seems to be this could be solved by https://github.com/jbms/sphinx-immaterial/pull/251 in future. See also https://github.com/sphinx-doc/sphinx/issues/6092

kreuzberger avatar May 05 '23 07:05 kreuzberger

The MathJax package seems to be about ~ 25 MB large. So deploying for the build with any document is not helpful. For the simplepdf build it is ok to use a centralised version, cause no html gets delivered

kreuzberger avatar May 05 '23 08:05 kreuzberger

Using https://pypi.org/project/sphinx-mathjax-offline/ Math output is working for the html builder. Simplepdf Builders seems not to add

<script async="async" src="_static/mathjax/tex-chtml.js"></script>

to the html So adding manually in conf.py[.in]

mathjax_path = "mathjax/tex-chtml.js"
html_js_files = [
    mathjax_path
]

fixes the problem.

Generated HTML shows now proper math equations. But generated pdf does not.

kreuzberger avatar May 05 '23 08:05 kreuzberger

Finaly solved for me by adding / using

 extensions + =  'sphinx.ext.imgmath'
 imgmath_image_format = 'svg'

Math output is properly shown in html and could also be scaled properly in html, singlepagehtml and weasyprint pdf.

kreuzberger avatar May 05 '23 09:05 kreuzberger

diff --git a/demo/conf.py b/demo/conf.py
index 593ed26..557aafb 100644
--- a/demo/conf.py
+++ b/demo/conf.py
@@ -26,6 +26,8 @@ extensions = [
     'sphinxcontrib.plantuml',
     'sphinx_needs',
     'sphinx.ext.autodoc',
+    'sphinx.ext.imgmath',
+
 ]
 
 version = "1.0"  # Will not be raised
@@ -74,6 +76,7 @@ plantuml_output_format = "svg_img"
 local_plantuml_path = os.path.join(os.path.dirname(__file__), "../", "docs", "utils", "plantuml.jar")
 plantuml = f"java -Djava.awt.headless=true -jar {local_plantuml_path}"
 
+imgmath_image_format = 'svg'
 
 def setup_jquery(app, exception):
     """

kreuzberger avatar May 05 '23 10:05 kreuzberger

Thanks for the investigation and the excellent report.

At least we should document this somewhere in the docs.

Do you think we should force the installation of sphinx.ext.imgmath and inject the needed configuration on our own when a simpledpdf build gets invoked?

danwos avatar May 08 '23 05:05 danwos

At least we should document this somewhere in the docs.

I think documentation would be necessary, also an example solution in the demo pdf

Do you think we should force the installation of sphinx.ext.imgmath and inject the needed configuration on our own when a simpledpdf build gets invoked?

I think not. Reasons for me are:

  • the provided solution may not satisfy some poeple or they have already a solution with mathjax
  • Regarding the issues sphinx itself will provide a better solution for html5 builds, so the extension would not be needed in future sphinx releases.

Documentation of the solution in the docs and demo config would be enough for me.

kreuzberger avatar May 08 '23 06:05 kreuzberger

This was the fix for jupyter-book + mathjax: https://github.com/executablebooks/jupyter-book/pull/1774/files with the problem described in the opening issue: https://github.com/executablebooks/jupyter-book/issues/1705

I guess a similar could apply here?

The solution from there was:

            # set the below flag to always to enable maths in singlehtml builder
            if app.builder.name == "singlehtml":
                app.set_html_assets_policy("always")

I haven't tried to do this yet, and I haven't even used simplepdf before (I came across this while checking for the solution in various builders), but thought it might help...

rkdarst avatar Sep 06 '23 20:09 rkdarst

@kreuzberger I am facing similar issue described here.

If I use imgmath_image_format = 'svg', the output I see is: 1 to 2^{31} - 1 in both HTML and PDF. Although this fixes the issue in PDF, I am trying to achieve the output without {}.

sachin-suresh-rapyuta avatar Jun 04 '24 02:06 sachin-suresh-rapyuta

@kreuzberger I am facing similar issue described here.

If I use imgmath_image_format = 'svg', the output I see is: 1 to 2^{31} - 1 in both HTML and PDF. Although this fixes the issue in PDF, I am trying to achieve the output without {}.

simple_pdf_math_equations

Added an equation like yours to my test document, everything seems to look good. (The equation is just a little to high, would be better to have it centered to the textline). But the rendering is ok. So maybe more or less a bug in the versions your may use. I am using sphinx-7.2.6 with Python 3.11 and newest simplepdf (builded from source, not pypi).

kreuzberger avatar Jun 05 '24 06:06 kreuzberger