summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/ximera/src/jax.dtx
blob: 5441a139e13db0b8ab02d9da2a1d21008851077a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
% \subsubsection{MathJax support}
% When using mathjax, dump all the |\newcommand|s to a |.jax| file.
%
% First, create the |.jax| file.
%    \begin{macrocode}
%<*classXimera>
\ifdefined\HCode
  \else
    \newwrite\myfile
    \immediate\openout\myfile=\jobname.jax
\fi    
%</classXimera>
%    \end{macrocode}
% From |only.dtx| we must also create |prompt| on the MathJax side.
%    \begin{macrocode}
%<*classXimera>
\ifdefined\HCode
  \else
    \immediate\write\myfile{\unexpanded{\newenvironment}{prompt}{}{}}
\fi
%</classXimera>
%    \end{macrocode}
% Redefine newcommand appropriately.
%    \begin{macrocode}
%<*classXimera>
\ifdefined\HCode
  \else
\let\@oldargdef\@argdef
\long\def\@argdef#1[#2]#3{%
\immediate\write\myfile{\unexpanded{\newcommand}{\unexpanded{#1}}[\unexpanded{#2}]{\unexpanded{#3}}}%
\@oldargdef#1[#2]{#3}%
}

\let\@OldDeclareMathOperator\DeclareMathOperator
\renewcommand{\DeclareMathOperator}[2]{\@OldDeclareMathOperator{#1}{#2}\immediate\write\myfile{\unexpanded{\DeclareMathOperator}{\unexpanded{#1}}{\unexpanded{#2}}}}
    
\fi
%</classXimera>
%    \end{macrocode}
% Include the jax'ed newcommands
%    \begin{macrocode}
%<*cfgXimera>
% Remove commands that use @
\immediate\write18{sed -i "/@/d" \jobname.jax}
% Replace ##1 with #1 and so forth
\immediate\write18{sed -i "s/\string#\string#\string\\([0-9]\string\\)/\string#\string\\1/g" \jobname.jax}

\Configure{BVerbatimInput}{}{}{}{}

\Configure{verbatiminput}{}{}{}{}

% Instead of a nonbreaking space, use a standard space
\makeatletter
\def\FV@Space{\space}
\makeatother

% Include the mathjax newcommands in a math/tex script right at the beginning of the body
\Configure{BODY}{%
\HCode{<body>\Hnewline}%
\Tg<div class="preamble">%
\Tg<script type="math/tex">%
\BVerbatimInput{\jobname.jax}%
\Tg</script>%
\IfFileExists{\jobname.ids}{\HCode{<script type="text/javascript">\Hnewline}%
\BVerbatimInput{\jobname.ids}%
\HCode{</script>\Hnewline}%
\Tg</div>%
}{}
}{%
\HCode{</body>\Hnewline}%
}
%    \end{macrocode}
% Now I just need to add a newcommand command which outputs the
% appropriate newcommands to MathJax; then this should be "good
% enough" for our purposes.
%    \begin{macrocode}
\newtoks\eqtoks
\def\AltMath#1${\eqtoks{#1}%
        \HCode{<script type="math/tex">\the\eqtoks</script>}$}
\Configure{$}{}{}{\expandafter\AltMath}

\def\AltlMathI#1\){\eqtoks{#1}%
        \HCode{<script type="math/tex">\the\eqtoks</script>}\)}
\Configure{()}{\AltlMathI}{}

\def\AltlDisplay#1\]{\eqtoks{#1}%
        \HCode{<script type="math/tex; mode=display">\the\eqtoks</script>}\]}
\Configure{[]}{\AltlDisplay}{}

\def\AltlDisplayI#1$${\eqtoks{#1}%
       \HCode{<script type="math/tex; mode=display">\the\eqtoks</script>}$$}
\Configure{$$}{}{}{\expandafter\AltlDisplayI}  
%    \end{macrocode}
% Need to turn off htmlpar too, as expained in http://tex.stackexchange.com/questions/204930/vertical-spaces-in-htlatex-scriptenv
%    \begin{macrocode}
\newcommand\VerbMath[1]{%
\renewenvironment{#1}{\NoFonts}{\EndNoFonts}
\ScriptEnv{#1}{\ifvmode \IgnorePar\fi \EndP\HCode{<script type="math/tex; mode=display"> \string\begin{#1}\Hnewline}\HtmlParOff}{\HtmlParOn\HCode{\string\end{#1}</script>}}
}
%    \end{macrocode}
% This is a fix for the LAODE book, which uses matlabEquation as if it were an equation
%    \begin{macrocode}
\ScriptEnv{matlabEquation}{\ifvmode \IgnorePar\fi \EndP\HCode{<script type="math/tex; mode=display"> \string\begin{equation}\Hnewline}\HtmlParOff}{\HtmlParOn\HCode{\string\end{equation}</script>}}

\VerbMath{equation}
\VerbMath{equation*}
\VerbMath{align}
\VerbMath{align*}
\VerbMath{alignat}
\VerbMath{alignat*}
\VerbMath{eqnarray}
\VerbMath{eqnarray*}

%</cfgXimera>
%    \end{macrocode}