Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally use KaTeX for math in HTML #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

LiberalArtist
Copy link
Contributor

@LiberalArtist LiberalArtist commented Nov 19, 2023

This patch series adds optional support for using KaTeX, which generates MathML and/or HTML from LaTeX math, as an alternative to rendering math to GIFs. The most important benefit of using KaTeX is that its output is accessible to screenreaders and other assistive technologies, rather than appearing as <graphic>. The output also integrates with CSS, can be copied and pasted as text, can reflow across line breaks as needed, and doesn't suffer from pixelation.

The support added to stex is sufficient to handle all uses math mode in stex.stex and the latest versions of the Chez Scheme User's Guide and release notes without requiring any changes to the stex documents themselves. (Chez Scheme does need a few minor changes to the makefiles related to installation and cross-references to The Scheme Programming Language.) The only other public stex document I know of, Nanopass's user-guide.stex, does not render to HTML at all (it uses \documentclass{book}, but there is no book.hcls).

As an example, here is a comparison of the rendering of part of the documentation for collect:

Before:

Screenshot 2023-11-19 at 18-17-10 Storage Management

After:

Screenshot 2023-11-19 at 18-19-41 Storage Management

This series also subsumes my earlier #6, as I encountered related issues while working on these patches.

An installed copy of stex might have all write permission bits removed,
even for the owner of the installed files: this is the case with the
Guix package of stex, for example. If such an installation is used to
initialize the gifs or math directories (e.g. for an out-of-source
build), tar likewise creates them without write permissions, preventing
subdirectories from being created later. Set the user write bit
explicitly to avoid this problem.
Furthermore, tar by default would copy symbolic links in the installed
copy as links, causing chmod to fail or, if permitted, writes to an
improper location. Avoid this using the --dereference option, which is
portable to at least GNU and FreeBSD tar (but only in long form: GNU
has -h, but FreeBSD has -L).
  Mf-stex

Related to racket/racket#4203
Closes dybvig#6
Emit the &ndash; and &mdash; entites, which are now well supported.
Emit <td style="white-space:nowrap;"> instead of the deprecated
<td nowrap>.
  src/html-prep.ss
LiberalArtist added a commit to LiberalArtist/ChezScheme that referenced this pull request Nov 19, 2023
KaTeX renders LaTeX math ahead-of-time to MathML and/or HTML.
Its output is accessible to screen readers and other tools
(instead of appearing as "<graphic>"), and it also looks less
pixelated than math rendered to GIFs.

Depends on dybvig/stex#7
LiberalArtist added a commit to LiberalArtist/ChezScheme that referenced this pull request Nov 19, 2023
KaTeX renders LaTeX math ahead-of-time to MathML and/or HTML.
Its output is accessible to screen readers and other tools
(instead of appearing as "<graphic>"), and it also does not
suffer from the pixelation of math rendered to GIFs.

Depends on dybvig/stex#7
@owaddell
Copy link
Collaborator

Having an alternative GIFs for math would be nice.

I haven't looked at the changes yet, but noticed a difference in \schemedisplay layout in the generated stex.html that seems to be a result of changing to <!DOCTYPE html>.

Original PR
original PR

It looks like we can restore the original layout via:

diff --git a/doc/stex.css b/doc/stex.css
index 8950ff0..b502138 100644
--- a/doc/stex.css
+++ b/doc/stex.css
@@ -14,6 +14,8 @@ a.image:link, a.image:active, a.image:visited, a.image:hover {
   background: #FFFFFF;
 }
 
+.schemedisplay { line-height: 1.0625; }
+
 ul.tocchapter { list-style: none; }
 ul.tocsection { list-style: circle; color: #923a3a }
 
diff --git a/inputs/scheme.hsty b/inputs/scheme.hsty
index 18fe082..bbbd3ee 100644
--- a/inputs/scheme.hsty
+++ b/inputs/scheme.hsty
@@ -18,7 +18,8 @@
 \def\scheme#1{{\tt #1}}
 \def\longcode\schemedisplay{\schemedisplay}
 \def\noskip\schemedisplay{\schemedisplay}
-\def\schemedisplay{\par\begingroup\tt\hardspaces}
+\def\schemedisplay{\begingroup\renewcommand{\par}{\raw{
+<p class="schemedisplay">}}\par\endgroup\begingroup\tt\hardspaces}
 \def\endschemedisplay{\endgroup\par}
 \def\schemeindent{}
 \def\schatsign{\raw{@}}

@LiberalArtist
Copy link
Contributor Author

Good catch! I will push a revised series with that fix, though probably not today.

For context, the KaTeX documentation says that its output generally requires <!DOCTYPE html>: IIRC, support for MathML was new in HTML 5. I considered only changing the doctype when using KaTeX, but overall it seemed better to reduce the number of branches in stex's output.

LiberalArtist added a commit to LiberalArtist/stex that referenced this pull request Nov 22, 2023
- Adjust \schemedisplay{} to preserve the existing layout with the
  switch to <!DOCTYPE html>.
   inputs/scheme.hsty, doc/stex.css
@LiberalArtist
Copy link
Contributor Author

I've pushed a revised series with the fix for \schemedisplay. I'll also update the draft cisco/ChezScheme#761 to refer to it.

LiberalArtist added a commit to LiberalArtist/ChezScheme that referenced this pull request Nov 22, 2023
LiberalArtist added a commit to LiberalArtist/ChezScheme that referenced this pull request Dec 1, 2023
KaTeX renders LaTeX math ahead-of-time to MathML and/or HTML.
Its output is accessible to screen readers and other tools
(instead of appearing as "<graphic>"), and it also does not
suffer from the pixelation of math rendered to GIFs.

Also, use scheme.sty and scheme.hsty from stex instead of
stale copies with ad-hoc changes.

Depends on dybvig/stex#7
- Detect when KaTeX is available and use it. Add the variables KATEX,
  KATEX_FLAGS, and KATEX_CSS for configuration. Add KaTeX definitions
  for macros needed for compatibility.
    Mf-stex, math/Makefile, math/katexmacros
- Add --use-katex, --katex-css-link-file, and --katex-finished to
  html-prep. Use <!DOCTYPE html>. Emit a link to katex.css when
  applicable. Adjust emit-math and punt-to-latex for KaTeX backend.
  Report lack of support for \epsfbox{} and the eqnarray* environment
  when using KaTeX. Add special handling for the "ghost" \Rightarrow
  so that it will match the selected backend's \Rightarrow.
    src/html-prep.ss, inputs/scheme.hsty
- Adjust \schemedisplay{} to preserve the existing layout with the
  switch to <!DOCTYPE html>.
   inputs/scheme.hsty, doc/stex.css
- Support the macros \LaTeX and \KaTeX.
    inputs/scheme.sty, inputs/scheme.hsty
- Update documentation and build with KaTeX. Add .gitignore to avoid
  committing generated intermediate files.
    Makefile.template, doc/Makefile, doc/stex.stex, doc/stex.pdf,
    doc/stex.html, doc/stex.katex-css-link, doc/.gitignore
- Increment version to 1.3.
    src/VERSION
@LiberalArtist
Copy link
Contributor Author

I've pushed a slight change that uses:

.schemedisplay { line-height: 1.0625; }
.schemedisplay .katex { font-size: 1em; }

to prevent math inside \schemedisplay, especially the right arrows and ghost versions, from adding extra line spacing. I did likewise in my latest commit to cisco/ChezScheme#761: the usage in the release notes (e.g. under "2.126. New time-object manipulation procedures (8.9.5)") motivated the change.

I don't know of anything else that needs to be done before this can be merged, though more review is always welcome!

LiberalArtist added a commit to LiberalArtist/ChezScheme that referenced this pull request Feb 25, 2024
KaTeX renders LaTeX math ahead-of-time to MathML and/or HTML.
Its output is accessible to screen readers and other tools
(instead of appearing as "<graphic>"), and it also does not
suffer from the pixelation of math rendered to GIFs.

Also, use scheme.sty and scheme.hsty from stex instead of
stale copies with ad-hoc changes.

Depends on dybvig/stex#7
Copy link
Collaborator

@owaddell owaddell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating this. I finally got back to looking at it and had just a couple suggestions. I'd like to get these changed merged.

I'm still wading through the changes over in the Chez Scheme repository. Not sure what prompted the increase in \hfuzz over in csug.stex, for example.

(input-error "environment eqnarray* not supported by KaTeX"))
;; Might be viable to translate to \begin{align*} ... \end{align*} syntax,
;; which KaTeX does support, or to add support for align* and encourage
;; users to port there documents. See discussion in:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"there" => "their"

(fprintf op "<!-- DO NOT EDIT THIS FILE-->~%")
(fprintf op "<!-- Edit the .tex version instead-->~%~%")
(fprintf op "<!-- Edit the .stex version instead-->~%~%")
(fprintf op "<html>~%")
(fprintf op "<head>~%<title>~a</title>~%" title)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add:

      (fprintf op "<meta charset=\"utf-8\">~%")

inside the header so that we don't get rendering like the following in some browsers:
another-example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants