ENB: Improving Leo's support for mathematics

93 views
Skip to first unread message

Edward K. Ream

unread,
Dec 28, 2024, 1:38:36 PM12/28/24
to leo-editor

This Engineering Notebook post discusses recent experiments using Leo as a mathematical notebook.


My goal was to eliminate all friction from my mathematical workflow. This post tells how I did it.


References


PR #4253 contains improvements to VR. The public math.leo outline contains recent experiments, including tests of all aspects of VR related to mathematics.


Using LaTeX and pdf


The Overleaf website was a great way to learn LaTeX, but it can't be the basis of my workflow. Cutting snippets from the website to math.leo quickly becomes intolerable.


Overleaf renders LaTeX by generating an internal pdf file using pdflatex and then rendering that file. My first thought was to emulate that approach. However, that approach wasn't good enough. The PostScript contains the gruesome details.


Using mathjax


Another problem with pdf files became apparent only in retrospect. As stated here, TeX/LaTeX is a print layout engine. When using Overleaf, I spent considerable time configuring the page layout to look good on the Overleaf web page. But that work isn't necessary!


Instead of futzing with page layout, mathjax renders only LaTeX math mode. To quote from the link above, "MathJax expects that you will use standard HTML tags to handle formatting the text of your page."


This approach is exactly what we need in the VR pane. The PR renders @mathjax nodes (nodes containing @language mathjax) in the VR pane. Changes in p.b render automatically (and quickly) in the VR pane.


mathjax has its quirks. There are subtle differences between mathjax and LaTeX. In particular, it appears best to use mathjax 2 rather than 3.


Summary


The general goal of eliminating friction produced a surprise. mathjax, not LaTeX, is a better fit for Leo. The PR creates a friction-free environment for doing mathematics in Leo.


Edward


P.S. Some details:


Problems using pdf files.


math.leo contains @button render-latex. This script assumes that c.p.b contains LaTeX code. The script:


- Converts c.p.b to a pdf file whose name is a sanitized version of c.p.h.

- Searches the outline for a corresponding @pdf node, creating the node if necessary.

  The body text of this node contains the full path to the generated pdf file.


The PR adds support for @pdf nodes. Selecting the node will render the named pdf file. 


Alas, @pdf doesn't work well enough. Qt does render the pdf file, but there is no way to set a default magnification of the rendered text! This defect quickly becomes intolerable. Furthermore, using two separate nodes is way too clumsy.


About the QWebEngineView class


The revised VR plugin uses the QWebEngineView class for all html-related nodes. This class is a wrapper around the Chromium open-source browser. This browser should have enough firepower.


Alas, Qt's support for pdf files uses a hidden pdf plugin. This plugin is inaccessible via any programming API. Hours of googling and experimentation failed to find any access points.


Interference from GE Force Experience


The VR3 plugin raises a bizarre warning on my machine. With the revised VR plugin, I got the same warning! This message originates from the GE Force Experience package, a part of the NVidia video driver.


Somehow, instantiating the QWebEngineView class triggered a notification from the NVidia package! Turning off the notification eliminated the message. The NVidia package is obsolete, so I removed it.


Hangs on Exit


Earlier versions of the PR could hang when Leo shut down. As a workaround, the PR deletes all known objects related to the QWebEngineView class.


Leo no longer hangs since I removed the GE Force experience package. In any case, the bug appears to have been caused outside of Leo and the VR plugin.


EKR

Thomas Passin

unread,
Dec 28, 2024, 5:57:03 PM12/28/24
to leo-editor

Interference from GE Force Experience


The VR3 plugin raises a bizarre warning on my machine. With the revised VR plugin, I got the same warning! This message originates from the GE Force Experience package, a part of the NVidia video driver.


Somehow, instantiating the QWebEngineView class triggered a notification from the NVidia package! Turning off the notification eliminated the message. The NVidia package is obsolete, so I removed it.


On my new Win11 machine, it turns out that I have geForce too but I have location services turned off.  GeForce needs to use that service and as a result it never turned itself on and I never even saw the User's agreement it would like me to sign. Now that I know what it wants to do to my computer, I won't ever be using it.

Thomas Passin

unread,
Dec 28, 2024, 7:20:34 PM12/28/24
to leo-editor


About the QWebEngineView class


The revised VR plugin uses the QWebEngineView class for all html-related nodes. This class is a wrapper around the Chromium open-source browser. This browser should have enough firepower.


Alas, Qt's support for pdf files uses a hidden pdf plugin. This plugin is inaccessible via any programming API. Hours of googling and experimentation failed to find any access points.



If you want to set the initial zoom factor, which is the main problem I've had to deal with, you can add a fragment identifier to the QUrl, like this:

        url = QUrl.fromLocalFile(pdf_path)
        url.setFragment("zoom=200")

There are a few other settings that can be set with a fragment identifier.  See Section 3 here.


Thomas Passin

unread,
Dec 28, 2024, 7:31:22 PM12/28/24
to leo-editor
I suppose you already found this, but in case you didn't, you can set up the QWebEngineView for the pdf viewer like this:

        settings = qwev.settings() # qwev = QWebEngineView instance
        settings.setAttribute(settings.WebAttribute.PluginsEnabled, True)
        settings.setAttribute(settings.WebAttribute.PdfViewerEnabled, True)

Thomas Passin

unread,
Dec 29, 2024, 12:50:30 AM12/29/24
to leo-editor
On Saturday, December 28, 2024 at 7:31:22 PM UTC-5 Thomas Passin wrote:
I suppose you already found this, but in case you didn't, you can set up the QWebEngineView for the pdf viewer like this:

        settings = qwev.settings() # qwev = QWebEngineView instance
        settings.setAttribute(settings.WebAttribute.PluginsEnabled, True)
        settings.setAttribute(settings.WebAttribute.PdfViewerEnabled, True)


Turns out that when plugins are enabled, then the PdfViewer plugin gets enabled by default so the last line isn't needed (verified by testing).

Edward K. Ream

unread,
Dec 29, 2024, 6:15:45 AM12/29/24
to leo-e...@googlegroups.com
On Sat, Dec 28, 2024 at 6:20 PM Thomas Passin wrote:

If you want to set the initial zoom factor, which is the main problem I've had to deal with, you can add a fragment identifier to the QUrl, like this:

        url = QUrl.fromLocalFile(pdf_path)
        url.setFragment("zoom=200")

There are a few other settings that can be set with a fragment identifier.  See Section 3 here.

That works!!! Many thanks for this tip.

Edward

Ray wang

unread,
Dec 29, 2024, 8:26:03 PM12/29/24
to leo-editor

Would you consider using Typst? https://typst.app/

HaveF HaveF

unread,
Dec 29, 2024, 9:09:43 PM12/29/24
to leo-editor


mathjax has its quirks. There are subtle differences between mathjax and LaTeX. In particular, it appears best to use mathjax 2 rather than 3.

Maybe you can consider https://katex.org/
 

Edward K. Ream

unread,
Dec 30, 2024, 6:51:56 AM12/30/24
to leo-e...@googlegroups.com
On Sun, Dec 29, 2024 at 7:26 PM Ray wang <wzy...@gmail.com> wrote:

Would you consider using Typst? https://typst.app/

Thanks for the link. VR's QWebEngineView widget should handle Typst, so @typst should be feasible. I'll investigate.

Edward

Edward K. Ream

unread,
Dec 30, 2024, 6:54:08 AM12/30/24
to leo-e...@googlegroups.com
On Sun, Dec 29, 2024 at 8:09 PM HaveF HaveF <iamap...@gmail.com> wrote:


mathjax has its quirks. There are subtle differences between mathjax and LaTeX. In particular, it appears best to use mathjax 2 rather than 3.

Maybe you can consider https://katex.org/

Will do!

Edward
Reply all
Reply to author
Forward
0 new messages