Debian Codegraph @

texmf-dist/source/generic/tex4ht/tex4ht-mathjax.tex

raw
1 % $Id: tex4ht-mathjax.tex 1634 2025-01-26 15:11:47Z karl $
2 % compile: latex tex4ht-mathjax
3 %
4 % Copyright 2018-2025 TeX Users Group
5 % Released under LPPL 1.3c+.
6 % See tex4ht-cpright.tex for license text.
7
8 \ifx \HTML\UnDef
9 \def\HTML{mathjax-latex-4ht}
10 \def\CONFIG{\jobname}
11 \def\MAKETITLE{\author{Michal Hoftich}}
12 \def\next{\input mktex4ht.4ht \endinput}
13 \expandafter\next
14 \fi
15
16 \input{common-code}
17 \input{common}
18 \input{tex4ht-cpright}
19
20 \<mathjax-latex-4ht\><<<
21 % mathjax-latex-4ht.4ht (|version), generated from |jobname.tex
22 % Copyright 2018-2025 TeX Users Group
23 |<TeX4ht copywrite|>
24 |<required packages|>
25 |<defined commands|>
26 \AtBeginDocument{%
27 |<configurations|>
28 |<mhchem configurations|>
29 |<restore environments|>
30 }
31 \endinput
32 >>>
33
34 We don't require additional packages anymore, as everything necessary is included
35 in LaTeX kernel now.
36 % \RequirePackage{etoolbox,expl3,environ}
37 \<required packages\><<<
38 >>>
39
40 The \verb|\alteqtoks| command prints the used LaTeX math code to the output document in
41 verbatim.
42
43 We use \verb|\detokenize| to output verbatim LaTeX code to the HTML document.
44 The side effect of this is that detokenize inserts space
45 after each control sequence. This is completely valid TeX code, but earlier versions of
46 MathJax didn't like that, rendering resulted in error.
47
48 Fortunatelly, MathJax 3 supports these spaces, so we could remove regular expressions for space handling.
49 The original code was this:
50
51 % % convert \ { to \:{
52 % \regex_replace_all:nnN { \x{5C} \x{20} \x{7B} } { \x{5C} \x{3A} \x{7B} } \l_tmpa_tl
53 % % delete spaces before left brackets
54 % \regex_replace_all:nnN { \x{20} \x{7B} } { \x{7B} } \l_tmpa_tl
55 % % replace \\:{ back to \\ { -- this can be introduced by the previous regex
56 % \regex_replace_all:nnN { \x{5C} \x{5C} \x{3A} \x{7B} } { \x{5C} \x{5C} \x{20} \x{7B} } \l_tmpa_tl
57
58
59 In July 2024, when I researched another issue, I've found that it is actually possible to avoid these
60 extra spaces using LaTeX 3 commands. The inspiration comes from
61 \Link[https://tex.stackexchange.com/a/44444/2891]this Bruno Le Foch\EndLink.
62 One ongoing issue is that newlines are not presented. But they weren't preserved with \verb|\detokenize|
63 either, so it shouldn't be a problem.
64
65 In September 2024 I found that the LaTeX 3 method of preserving spaces didn't work correctly for
66 code like this: \verb|$\int A f(x)\;dx$| -- the space between \verb|\int| and \verb|A| was removed,
67 so it ended like command \verb|\intA|. So we are moving back to \verb|\detokenize|.
68
69 This is the code that was promising, but didn't work in the end:
70
71 \begin{verbatim}
72 % save tokens, but preserve spaces
73 % https://tex.stackexchange.com/a/44444/2891
74 \tl_set:Nn \l_tmpa_tl {#1}
75 \regex_replace_all:nnN { . } { \c{string} \0 } \l_tmpa_tl
76 \tl_set:Nx \l_tmpa_tl { \l_tmpa_tl }
77 \end{verbatim}
78
79
80 We still use regular expressions to escape invalid XML characters to entities, so it works only with LaTeX.
81 But regular expressions may be slow if there is a huge number of math, and they may fail if contents
82 of any math environment is larger than 64 KB.
83
84 So we provide a "fastmathjax" option, that doesn't do any post-processing of the math contents. It should
85 result in 60-70\% speed-up. The downside is that if math contains characters that are invalid in the
86 XML mode, such as ampersand, less-than and greater-than, make4ht XML DOM filters will fail. Fortunatelly,
87 make4ht will then switch to the HTML parser mode, so DOM filters will be still executed.
88
89
90 \<defined commands\><<<
91 \ExplSyntaxOn
92 \:CheckOption{fastmathjax}\if:Option
93 |<fastmathjax|>
94 \else
95 |<normalmathjax|>
96 \fi
97 \ExplSyntaxOff
98 >>>
99
100 Output verbatim math to HTML without any post-processing.
101 We still need to set the token list to prevent some issues.
102
103 For example, if the processed math contain paragraphs, we would
104 get an error because \''\HCode' doesn't like that.
105
106 \<fastmathjax\><<<
107 \DeclareRobustCommand\alteqtoks[1]{%
108 \tl_set:Ne \l_tmpa_tl {\detokenize{#1}}
109 \HCode{\l_tmpa_tl}
110 }
111 >>>
112
113 Escape illegal XML characters.
114
115 \<normalmathjax\><<<
116 \cs_new_protected:Npn \alteqtoks #1
117 {
118 \tl_set:Ne \l_tmpa_tl {\detokenize{#1}}
119 % % replace < > and & with xml entities
120 \regex_replace_all:nnN { \x{26} } { &amp; } \l_tmpa_tl
121 \regex_replace_all:nnN { \x{3C} } { &lt; } \l_tmpa_tl
122 \regex_replace_all:nnN { \x{3E} } { &gt; } \l_tmpa_tl
123 % replace \par command with blank lines
124 \regex_replace_all:nnN { \x{5C}par\b } {\x{A}\x{A}} \l_tmpa_tl
125 \tl_set:Ne \l_tmpb_tl{ \l_tmpa_tl }
126 \HCode{\l_tmpb_tl}
127 }
128 >>>
129
130
131 Provide configuration for MathJax
132
133 \<defined commands\><<<
134 \NewConfigure{MathJaxConfig}{1}
135 >>>
136
137 The MathJaxMacros configuration includes a file with TeX macro
138 definitions, and include them at the beginning of each HTML page.
139 MathJax then interprets them, so they are available in math.
140 This is an alternative to JavaScript method using MathJaxConfig.
141
142 The file cannot contain any other commands than newcommand,
143 all characters that could cause issues in the HTML parsing must
144 be escaped, so \verb|<| should became \verb|&lt;|, for example.
145
146 \<defined commands\><<<
147 \NewConfigure{MathJaxMacros}[1]{%
148 \Configure{@BODY}{\bgroup\NoFonts\ttfamily\detokenize{\(}%
149 \special{t4ht*<#1}%
150 \detokenize{\)}\EndNoFonts\egroup}%
151 }
152 >>>
153
154 The following commands are used for patching of the standard math commands and
155 environments. They will then keep the LaTeX code unchanged.
156
157 \<defined commands\><<<
158
159 \long\def\AltlMath#1\){\expandafter\alteqtoks{\(#1\)}\)}
160 \long\def\AltlDisplay#1\]{\alteqtoks{\[#1\]}\]}
161 \long\def\AltMathOne#1${\alteqtoks{\(#1\)}$}
162 % this seems a bit hacky -- we need to skip some code inserted at the
163 % beginning of each display math
164 \long\def\AltlDisplayDollars#1$${\alteqtoks{\[#1\]}$$}
165
166 \NewConfigure{MathjaxEnv}{2}
167
168 \newcommand\VerbMathToks[2]{%
169 \a:MathjaxEnv%
170 \HCode{\string\begin{#2}}%
171 \alteqtoks{#1}%
172 \HCode{\string\end{#2}}%
173 \b:MathjaxEnv%
174 }
175 >>>
176
177 In pictures, we want to use the original version of the math environments. Because we use
178 an agressive verbatim method for the environments, we cannot use the usual \verb'\HLet' method.
179 Instead, we save the original meaning, and user needs to restore it manually, using
180 the \verb'\RestoreMathJaxEnvironments' command.
181
182 \<defined commands\><<<
183 \ExplSyntaxOn
184 \seq_new:N\:savedmathjaxenvs
185
186 \newcommand\:savemathjaxenv[1]{%
187 \seq_gput_right:Nn\:savedmathjaxenvs{#1}
188 \expandafter\let\csname mathjax-#1\expandafter\endcsname\csname #1\endcsname%
189 \expandafter\let\csname mathjax-end#1\expandafter\endcsname\csname end#1\endcsname%
190 }
191
192 % we must not reintroduce the matrix environmeint in TikZ, because it interferes with the \matrix command
193 \newcommand\:ignoretikzmatrix{\seq_remove_all:Nn\:savedmathjaxenvs{matrix}}
194
195 \newcommand\RestoreMathJaxEnvironment[1]{%
196 \expandafter\let\csname #1\expandafter\endcsname\csname mathjax-#1\endcsname%
197 \expandafter\let\csname end#1\expandafter\endcsname\csname mathjax-end#1\endcsname%
198 }
199
200 \newcommand\RestoreMathJaxEnvironments{%
201 \seq_map_function:NN\:savedmathjaxenvs\RestoreMathJaxEnvironment%
202 }
203 \ExplSyntaxOff
204 >>>
205
206 The \verb|\VerbMath| command redefines environments to pass their content
207 verbatim to the HTML output.
208
209 The optional command can contain name of the counter that should be updated.
210 It will also use LaTeX 3 regular expressions to search for label commands.
211 Thanks to that, cross-referencing to the math environments should work.
212 It is a bit limited, for example subequations cannot work. There can be also
213 issues in books, where equation numbers are based on chapters, but MathJax
214 numbers them consecutively. In these cases, other mechanisms may be necessary.
215
216 \<defined commands\><<<
217 \ExplSyntaxOn
218 \cs_generate_variant:Nn \regex_extract_once:nnNTF {nV}
219 \newcommand\VerbMath[2][]{%
220 \cs_if_exist:cTF{#2}{
221 \:savemathjaxenv{#2}%
222 \RenewDocumentEnvironment{#2}{+!b}{%
223 \NoFonts\expandafter\VerbMathToks\expandafter{\detokenize{##1}}{#2}\EndNoFonts%
224 \ifx\relax#1\relax\else%
225 \refstepcounter{#1}%
226 \regex_extract_once:nVNTF { label\s* \x{7B}([^\x{7D}]*)\x{7D}} {\l_tmpb_tl} \l_tmp_seq {\label{\seq_item:Nn\l_tmp_seq{2}}} {}%
227 \fi
228 }{}
229 }{}%
230 }
231 \ExplSyntaxOff
232 >>>
233
234 The \verb|\fixmathjaxtoc| command is used for patching commands which should
235 keep their verbatim appearance in the TOC
236
237 \<defined commands\><<<
238 \def\fixmathjaxtoc#1{\Configure{writetoc}{\def#1{\detokenize{#1}}}}
239 >>>
240
241 This was meant to fix some commands in sectioning commands, but it is not
242 a good solution. Better is to define the problematic commands as robust.
243
244 \<not defined commands\><<<
245 \def\fixmathjaxsec#1{\def#1{\detokenize{#1}}}
246 \fixmathjaxsec\right
247 \fixmathjaxsec\left
248 >>>
249
250 Require verbatim math environments.
251
252 \<configurations\><<<
253 \VerbMath{subarray}
254 \VerbMath{smallmatrix}
255 \VerbMath{matrix}
256 \VerbMath{pmatrix}
257 \VerbMath{bmatrix}
258 \VerbMath{Bmatrix}
259 \VerbMath{vmatrix}
260 \VerbMath{Vmatrix}
261 \VerbMath{cases}
262 \VerbMath{subequations}
263 \VerbMath{aligned}
264 \VerbMath{alignedat}
265 \VerbMath{gathered}
266 \VerbMath[equation]{gather}
267 \VerbMath{gather*}
268 \VerbMath[equation]{alignat}
269 \VerbMath{alignat*}
270 \VerbMath{xalignat}
271 \VerbMath{xalignat*}
272 \VerbMath{xxalignat}
273 \VerbMath[equation]{align}
274 \VerbMath{align*}
275 \VerbMath{flalign[equation]}
276 \VerbMath{flalign*}
277 \VerbMath[equation]{split}
278 \VerbMath[equation]{multline}
279 \VerbMath{multline*}
280 \VerbMath[equation]{equation}
281 \VerbMath{equation*}
282 \VerbMath{math}
283 \VerbMath{displaymath}
284 \VerbMath{eqnarray}
285 \VerbMath{eqnarray*}
286 >>>
287
288 It is necessary to reset env configurations for multiline
289 environments, because default TeX4ht configurations turn
290 them into pictures.
291
292 \<configurations\><<<
293 \ConfigureEnv{gather}{}{}{}{}
294 \ConfigureEnv{gather*}{}{}{}{}
295 \ConfigureEnv{multline}{}{}{}{}
296 \ConfigureEnv{multline*}{}{}{}{}
297 >>>
298
299
300 Fixes for tables of contents.
301
302 \<configurations\><<<
303 \fixmathjaxtoc\int
304 \fixmathjaxtoc\,
305 \fixmathjaxtoc\sin
306 \fixmathjaxtoc\cos
307 \fixmathjaxtoc\tan
308 \fixmathjaxtoc\arcsin
309 \fixmathjaxtoc\arccos
310 \fixmathjaxtoc\arctan
311 \fixmathjaxtoc\csc
312 \fixmathjaxtoc\sec
313 \fixmathjaxtoc\cot
314 \fixmathjaxtoc\sinh
315 \fixmathjaxtoc\cosh
316 \fixmathjaxtoc\tanh
317 \fixmathjaxtoc\coth
318 \fixmathjaxtoc\log
319 \fixmathjaxtoc\ln
320 \fixmathjaxtoc\sum
321 \fixmathjaxtoc\(
322 \fixmathjaxtoc\)
323 \fixmathjaxtoc\begin
324 \fixmathjaxtoc\end
325 \fixmathjaxtoc\\
326 \fixmathjaxtoc\exp
327 \fixmathjaxtoc\left
328 \fixmathjaxtoc\right
329 >>>
330
331 \<mhchem configurations\><<<
332 \@ifpackageloaded{mhchem}{%
333 \def\:tempa#1{\texttt{\detokenize{\(\ce{#1}\)}}}
334 \HLet\ce\:tempa
335 }{}
336 >>>
337
338 Restore environments redefined to produce verbatim output to their
339 original meaning in the picture mode. Otherwise math cannot work
340 inside TikZ etc.
341
342 \<restore environments\><<<
343 \@ifpackageloaded{tikz}{%
344 %\tikzset{every picture/.append code={\:ignoretikzmatrix\RestoreMathJaxEnvironments}}
345 \pend:def\tikz@installcommands{\RestoreMathJaxEnvironments}
346 }{}
347 >>>
348
349 \endinput