Skip to content

Commit 5d73fbf

Browse files
committed
optionally use KaTeX for math in html
- 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 - 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
1 parent 9d18c2d commit 5d73fbf

File tree

14 files changed

+407
-50
lines changed

14 files changed

+407
-50
lines changed

Makefile.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ bib =
1717
# define index if an index is to be generated
1818
# index=yes
1919

20+
# override katex variables here if desired
21+
2022
include $(STEXLIB)/Mf-stex
2123

2224
# define or override suffixes here

Mf-stex

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ else
2828
fixbibtex = $(STEXLIB)/$m/fixbibtex
2929
endif
3030

31+
ifeq ($(origin KATEX), undefined)
32+
KATEX:=$(if $(shell command -v katex),katex,)
33+
endif
34+
ifneq ($(strip $(KATEX)),)
35+
ifeq ($(strip $(KATEX_CSS)),)
36+
# try to infer KATEX_CSS
37+
katexrealpath:=$(realpath $(shell command -v $(KATEX)))
38+
katexrealdir:=$(dir $(katexrealpath))
39+
ifeq ($(katexrealpath),)
40+
# can't infer; complain below
41+
else ifneq ($(realpath $(katexrealdir)/dist/katex.css),)
42+
# NPM installation layout (also release tarballs)
43+
KATEX_CSS=$(katexrealdir)/dist/katex.css
44+
else ifneq ($(realpath $(katexrealdir)/katex.css),)
45+
# Debian installation layout
46+
KATEX_CSS=$(katexrealdir)/katex.css
47+
endif
48+
ifeq ($(strip $(KATEX_CSS)),)
49+
$(error cannot find KATEX_CSS for KATEX=$(KATEX))
50+
endif
51+
endif
52+
endif
53+
export KATEX KATEX_FLAGS
54+
3155
mathdir=math/$(x)
3256
mathfiles=$(mathdir)/mathfiles
3357

@@ -104,7 +128,30 @@ $(x).firstrun: $(texsrc)
104128

105129
all.tex: $(texsrc)
106130

131+
ifeq ($(strip $(KATEX)),)
107132
$(x).html: $(x).mathrun
133+
else
134+
$(x).hfourthrun: $(x).mathrun $(x).katex-css-link
135+
$(Hprep) --mathdir $(mathdir) --use-katex --katex-finished --katex-css-link-file $(x).katex-css-link $(x)
136+
chmod 444 *.html
137+
touch $(x).hfourthrun
138+
139+
# a project may supply one with content to override the default
140+
$(x).katex-css-link:
141+
touch $(x).katex-css-link
142+
143+
ifeq ($(strip $(KATEX_CSS)),no)
144+
$(x).html: $(x).hfourthrun
145+
else
146+
$(x).html: $(x).hfourthrun katex
147+
148+
katex:
149+
mkdir -p katex
150+
cp $(KATEX_CSS) katex/katex.css
151+
(cd katex; (cd $(dir $(KATEX_CSS)); tar -cf - --dereference fonts) | tar -xpf -)
152+
chmod u+w katex/katex.css katex/fonts
153+
endif
154+
endif
108155

109156
$(x).mathrun: gifs $(mathfiles)
110157
@(cd $(mathdir); make)
@@ -121,20 +168,22 @@ math:
121168
$(mathfiles): $(x).hthirdrun $(figps)
122169
echo -n gifs= > $(mathfiles)
123170
(cd $(mathdir); echo *.tex | sed -e "s/\.tex/.gif/g") >> $(mathfiles)
171+
echo -n htmls= >> $(mathfiles)
172+
(cd $(mathdir); echo *.katex | sed -e "s/\.katex/.html/g") >> $(mathfiles)
124173

125174
$(x).hthirdrun: $(x).hsecondrun
126-
$(Hprep) --mathdir $(mathdir) $(x)
175+
$(Hprep) --mathdir $(mathdir) $(if $(strip $(KATEX)),--use-katex,) $(x)
127176
chmod 444 *.html
128177
touch $(x).hthirdrun
129178

130179
$(x).hsecondrun: $(x).hfirstrun
131-
$(Hprep) --mathdir $(mathdir) $(x)
180+
$(Hprep) --mathdir $(mathdir) $(if $(strip $(KATEX)),--use-katex,) $(x)
132181
chmod 444 *.html
133182
touch $(x).hsecondrun
134183

135184
$(x).hfirstrun: math $(x).thirdrun
136-
(if [ ! -e $(mathdir) ] ; then mkdir -p -m u=rwx,g=srx,o=rx $(mathdir); ln -s ../Makefile ../mathmacros $(mathdir); fi)
137-
$(Hprep) --mathdir $(mathdir) $(x)
185+
(if [ ! -e $(mathdir) ] ; then mkdir -p -m u=rwx,g=srx,o=rx $(mathdir); ln -s ../Makefile ../mathmacros ../katexmacros $(mathdir); fi)
186+
$(Hprep) --mathdir $(mathdir) $(if $(strip $(KATEX)),--use-katex,) $(x)
138187
touch $(x).hfirstrun
139188

140189
spell: $(spellobj)
@@ -145,7 +194,7 @@ $(x).spell: $(x).bbl $(x).tex
145194

146195
clean: $(x).clean
147196
-/bin/rm -f *.log *.dvi *.aux *.out *.toc *.tmp *.idx *.ilg *.ind *.blg *.bbl *.rfm *.sfm *.firstrun *.secondrun *.thirdrun
148-
-/bin/rm -f *.haux *.htoc *.hidx *.hfirstrun *.hsecondrun *.hthirdrun *.mathrun
197+
-/bin/rm -f *.haux *.htoc *.hidx *.hfirstrun *.hsecondrun *.hthirdrun *.mathrun *.hfourthrun
149198
-/bin/rm -f *.tex
150199

151200
reallyclean: clean $(x).reallyclean

doc/.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
gifs/
2+
math/
3+
*.tex
4+
*.log
5+
*[.h]aux
6+
*[.h]toc
7+
*[.h]firstrun
8+
*[.h]secondrun
9+
*[.h]thirdrun
10+
*.hfourthrun
11+
*.mathrun

doc/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ bib =
1717
# define index if an index is to be generated
1818
# index=yes
1919

20+
# override katex variables here if desired
21+
KATEX_CSS=no # see comment in stex.katex-css-link
22+
2023
include $(STEXLIB)/Mf-stex
2124

2225
# define or override suffixes here

doc/stex.html

Lines changed: 127 additions & 13 deletions
Large diffs are not rendered by default.

doc/stex.katex-css-link

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!--
2+
A built copy of stex.html is checked in to the Git repository, and it
3+
ought to be readable without having to install or use stex (since it tells
4+
you how to do that). However, checking in the fonts needed for KaTeX would
5+
roughly double the size of the stex repository, even though the fonts are
6+
not especially large in absolute terms.
7+
8+
Instead, this file overrides the default link to katex.css to load it, and
9+
the fonts it uses, externally. The `KATEX_CSS=no` line in the makefile
10+
accordingly instructs stex not to copy the CSS and fonts into the build.
11+
12+
Under other circumstances, the default behavior of stex is preferrable: in
13+
particular, it ensures that the version of katex.css matches the version
14+
of KaTeX used to typeset the document.
15+
16+
Another option would be to define `KATEX_FLAGS=--format mathml` and rely
17+
solely on the native support in recent browsers and operating systems.
18+
-->
19+
<link rel="stylesheet"
20+
href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.css"
21+
integrity="sha384-OH8qNTHoMMVNVcKdKewlipV4SErXqccxxlg6HC9Cwjr5oZu2AdBej1TndeCirael"
22+
crossorigin="anonymous" />

doc/stex.pdf

5.46 KB
Binary file not shown.

doc/stex.stex

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,29 @@ items, such as make files, make-file templates, class files, and style
4242
files.
4343
The two main programs are \textbf{scheme-prep} and \textbf{html-prep}.
4444
\textbf{scheme-prep} performs a conversion from ``stex''-formatted files
45-
into latex-formatted files, while \textbf{html-prep} converts (some)
46-
latex-formatted files into html-formatted files.
45+
into \LaTeX-formatted files, while \textbf{html-prep} converts (some)
46+
\LaTeX-formatted files into html-formatted files.
4747

48-
An stex file is really just a latex file extended with a handful of
48+
An stex file is really just a \LaTeX\ file extended with a handful of
4949
commands for including Scheme code (or pretty much any other kind of code,
5050
as long as you don't plan to use the Scheme-specific transcript support)
5151
in a document, plus a couple of additional features rather arbitrarily
5252
thrown in.
5353

54-
The subset of latex-formatted files \textbf{html-prep} is capable of
54+
The subset of \LaTeX-formatted files \textbf{html-prep} is capable of
5555
handling is rather small but has nevertheless been useful for our
5656
purposes, which include producing html versions of a couple of books
5757
(\emph{The Scheme Programming Language}, Editions 2--4 and the Chez Scheme
58-
User's Guides for Versions 6--9), the scheme.com web site, class websites,
58+
User's Guides for Versions 6--10), the scheme.com web site, class websites,
5959
class assignments, and various other documents.
6060

6161
\section{Installation}
6262

6363
A prerequisite to building and using stex is to have Chez Scheme
6464
installed on your system.
6565
You'll also need pdflatex, dvips, ghostscript, and netpbm.
66+
Additionally, you may want to have \KaTeX\ installed: see ``Math in HTML''
67+
below.
6668
We've run stex under Linux and OS X but have not tried to run it under
6769
Windows.
6870

@@ -618,4 +620,60 @@ a & b & c & d \\
618620
\end{tabular}
619621
\end{quotation}
620622

623+
\section{Math in HTML}
624+
625+
A few very simple uses of \LaTeX's math mode are converted to HTML directly by
626+
\textbf{html-prep}. For typesetting non-trivial math, there are two strategies
627+
available:
628+
629+
\begin{enumerate}
630+
\item Originally, stex used \LaTeX\ to render math to GIF images.
631+
\item Optionally, stex can use \KaTeX, which generates MathML and/or HTML.
632+
\end{enumerate}
633+
634+
Using \KaTeX\ makes the rendered math accessible to assistive technologies,
635+
CSS, and other browser features. It also tends to look better than math in
636+
GIFs. However, a few features that stex supports when rendering to GIFs,
637+
notably including \scheme{\epsfbox\schlbrace\schrbrace} and the
638+
\scheme{eqnarray*} environment, are not currently supported when rendering with
639+
\KaTeX.
640+
641+
By default, stex will use \KaTeX\ if the ``katex'' command is found; otherwise,
642+
it will automatically fall back to generating GIFs. To prevent stex from using
643+
\KaTeX\ even if it is available, use \scheme{make KATEX=} or define
644+
\scheme{KATEX} as empty in your makefile before including Mf-stex. The same
645+
mechanisms can be used to supply a non-default ``katex'' command or even to
646+
require that \KaTeX\ be present, e.g.\ by writing:
647+
648+
\begin{quotation}
649+
\begin{verbatim}
650+
KATEX:=$(if $(shell command -v katex),katex,$(error katex not found))
651+
\end{verbatim}
652+
\end{quotation}
653+
654+
The variable \scheme{KATEX_FLAGS} may be used to supply additional options for
655+
\KaTeX.
656+
657+
The output of \KaTeX\ requires supporting CSS and font files. (Notably, it does
658+
not require JavaScript. It is also possible to supply non-default options to
659+
create output that can work without CSS and fonts by relying solely on the
660+
native support for MathML in recent browsers and operating systems.) By
661+
default, stex will attempt to find these files relative to the ``katex''
662+
command, and it should be able to handle the installation layouts used by both
663+
Node.js and Debian. If the files cannot be found automatically, you will need
664+
to define \scheme{KATEX_CSS}, either at the command line or in your makefile
665+
(before including Mf-stex): likely values include
666+
``./node\_modules/katex/dist/katex.css'' and
667+
``/usr/share/javascript/katex/katex.css''.
668+
669+
By default, stex copies the needed CSS and font files into a directory named
670+
``katex'' alongside the generated HTML files: when using \KaTeX, you need to
671+
install this directory or upload it to your web server.
672+
673+
As a special case, defining \scheme{KATEX_CSS=no} instructs stex not to copy
674+
these files. If you use this option, you may also want to override the default
675+
HTML \scheme{link} element used to load the CSS by creating a non-empty file
676+
with the base name of your document and the extension ``.katex-css-link'': for
677+
an example, see the ``stex.katex-css-link'' file used for this document.
678+
621679
\end{document}

inputs/scheme.hsty

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
\def\schunderscore{\raw{_}}
3232
\def\becomes{$\rightarrow$}
3333
\def\is{$\Rightarrow$}
34-
\def\si{\raw{<img src="gifs/ghostRightarrow.gif">}}
34+
\def\si{\schemeghostRightarrow} % handled specially by html-prep
3535
\def\var#1{\emph{#1}}
3636

3737
% frame="border" rules="all" makes mozilla do it right
@@ -40,3 +40,10 @@
4040
\def\endrepl{\raw{</td>}}
4141
\def\startinteraction{\raw{<td align="right"><table rules="all" border="1"><tr><td>}}
4242
\def\endinteraction{\raw{</td></tr></table></td></tr></table>}}
43+
44+
% This seemingly circular definition works because the \KaTeX inside
45+
% math mode is expanded with a different definition: either the
46+
% primitive from KaTeX or, if rendering without KaTeX, the definition
47+
% from scheme.sty.
48+
\def\KaTeX{$\mbox{\KaTeX}$}
49+
\def\LaTeX{$\mbox{\LaTeX}$}

inputs/scheme.sty

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@
5454
\def\beforeschemedisplay{\penalty-100\vskip\parskip\vskip5pt}
5555
\def\afterschemedisplay{\penalty-200\vskip5pt}
5656

57+
% From test/screenshotter/test.tex in the KaTeX source code, which says:
58+
% > Based on LaTeX's definition of \LaTeX, with L changed to K and kerns
59+
% > changed as in src/macros.js
60+
\DeclareRobustCommand{\KaTeX}{\mbox{%
61+
K\kern-.17em%
62+
{\sbox0 T\vbox to\ht0{\hbox%
63+
{\fontsize{.75em}{1em}\selectfont A}%
64+
\vss}}%
65+
\kern-.15em%
66+
\TeX}}

0 commit comments

Comments
 (0)