我已经成功使用了优秀答案中描述的配置将方程式保存为乳胶,以便使用 MathJax。现在我还想使用 exsheets 包(用于创建带有解决方案的问题),它与 tex4ht 兼容除了显示的方程式在问题环境中不起作用(假设有人希望将方程式保留为乳胶,就像我一样)。
具体来说,tex 文件
\documentclass{article}
\usepackage[]{amsmath}
\usepackage{exsheets}
\usepackage[T1]{fontenc}
\begin{document}
This first question compiles fine:
\begin{question}
Here's an inline equation: \( a=0 \).
\end{question}
This next question produces desired output, but gives error ``Missing \} inserted'':
\begin{question}
Here's a displayed equation (using brackets)
\[ x=1 \]
\end{question}
This last question gives errors ''Argument of verbatim has an extra \}'' and ''Paragraph ended before verbatim was complete'':
\begin{question}
Here's a displayed equation (using equation*)
\begin{equation}
y=2
\end{equation}
\end{question}
\end{document}
使用配置文件
\usepackage{verbatim}
\Preamble{html}
\Configure{VERSION}{}
\Configure{DOCTYPE}{\HCode{<!DOCTYPE html>\Hnewline}}
\Configure{HTML}{\HCode{<html>\Hnewline}}{\HCode{\Hnewline</html>}}
%\Configure{@HEAD}{\HCode{<link href="basic.css" rel="stylesheet" type="text/css" />\Hnewline}}
\Configure{@HEAD}{\HCode{
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
extensions: ["color.js"],
equationNumbers: { autoNumber: "AMS" }
},
extensions: ["tex2jax.js"],
tex2jax: {
\unexpanded{
inlineMath: [ ['\$','\$'], ["\\\(","\\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],}
processEscapes: true,
processEnvironments: true
}
});
</script>\Hnewline
}}
\Configure{@HEAD}{\HCode{ <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> \Hnewline}}
\newtoks\eqtoks
\def\AltMathOne#1${\eqtoks{$#1$}%
\HCode{\the\eqtoks}$}
\Configure{$}{}{}{\expandafter\AltMathOne}
\def\AltlMath#1\){\eqtoks{\(#1\)}%
\HCode{\the\eqtoks}}
\Configure{()}{\AltlMath}{}
\def\AltlDisplay#1\]{\eqtoks{\[#1\]}%
\HCode{\the\eqtoks}}
\Configure{[]}{\AltlDisplay}{}
\begin{document}
\newcommand\VerbMath[1]{%
\renewenvironment{#1}{%
\NoFonts%
\string\begin\{#1\}%
\verbatim}{\endverbatim\string\end\{#1\}\EndNoFonts}%
}
\VerbMath{align}
\VerbMath{equation}
\VerbMath{equation*}
\EndPreamble
产生输出
在问题环境中,内联方程式可以很好地编译,而带括号的显示方程式输出正常但编译时出现错误,并且使用显示的方程式equation
不会产生所需的输出。
更新:我现在意识到这个问题是由于 exsheets 环境中不允许使用逐字材料造成的;请参阅这个问题。因此我想知道是否可以在不使用 verbatim 包的情况下定义类似于 VerbMath 的命令,即将 \begin{equation} EQUATION HERE \end{equation} 整个内容在 html 输出中保持不变的命令,以便由 MathJax 处理。
答案1
我稍微修正了配置,\detokenize
现在使用 token 寄存器代替。我已将所有配置从文件移至.cfg
独立包,该包现在是helpers4ht捆绑包。它被命名为mathjax-latex-4ht.sty
:
\RequirePackage{verbatim}
\AtBeginDocument{%
\Configure{VERSION}{}
\Configure{DOCTYPE}{\HCode{<!DOCTYPE html>\Hnewline}}
\Configure{HTML}{\HCode{<html>\Hnewline}}{\HCode{\Hnewline</html>}}
%\Configure{@HEAD}{\HCode{<link href="basic.css" rel="stylesheet" type="text/css" />\Hnewline}}
\Configure{@HEAD}{\HCode{
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
extensions: ["color.js"],
equationNumbers: { autoNumber: "AMS" }
},
extensions: ["tex2jax.js"],
tex2jax: {
\unexpanded{
inlineMath: [ ['\$','\$'], ["\\\(","\\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],}
processEscapes: true,
processEnvironments: true
}
});
</script>\Hnewline
}}
\Configure{@HEAD}{\HCode{ <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> \Hnewline}}
\newtoks\eqtoks
\def\AltMathOne#1${\HCode{\detokenize{$#1$}}$}
\Configure{$}{}{}{\expandafter\AltMathOne}
\def\AltlMath#1\){\HCode{\detokenize{\(#1\)}}\)}
\Configure{()}{\AltlMath}{}
\def\AltlDisplay#1\]{\HCode{\detokenize{\[#1\]}}\]}
\Configure{[]}{\AltlDisplay}{}
}
%
\newcommand\VerbMath[1]{%
\renewenvironment{#1}{%
\NoFonts%
\string\begin\{#1\}%
\verbatim}{\endverbatim\string\end\{#1\}\EndNoFonts}%
}
\VerbMath{align}
\VerbMath{equation}
\VerbMath{equation*}
改变的是数学抓取命令的声明:
\def\AltMathOne#1${\HCode{\detokenize{$#1$}}$}
\def\AltlMath#1\){\HCode{\detokenize{\(#1\)}}\)}
\def\AltlDisplay#1\]{\HCode{\detokenize{\[#1\]}}\]}
您现在可以简化该.cfg
文件:
\RequirePackage{mathjax-latex-4ht}
\Preamble{xhtml}
\begin{document}
\EndPreamble
您仍然需要它来加载mathjax-latex-4ht
包,但您不需要任何自定义配置。
逐字的问题更为严重,不幸的是,我能想到的最简单的解决方案是修改文档并使用命令,该命令在使用时将输出环境latex
并使用逐字代码进行编译tex4ht
。
我们需要为这个命令提供一个包verbtexmath.sty
:
\ProvidesPackage{verbtexmath}
\newcommand\myverbenv[2]{%
\begin{#1}%
#2%
\end{#1}%
}
\endinput
和tex4ht
配置,verbtexmath.4ht
:
\renewcommand\myverbenv[2]{%
\string\begin\{#1\}%
\HCode{\Hnewline\detokenize{#2}\Hnewline}
\string\end\{#1\}
}
正如我所说的,你必须稍微修改一下文档:
\documentclass{article}
\usepackage[]{amsmath}
\usepackage{exsheets}
\usepackage[T1]{fontenc}
\usepackage{verbtexmath}
\begin{document}
This first question compiles fine:
\begin{question}
Here's an inline equation:\( a=0 \)
and $b = 1$
\end{question}
Outside question \(a = 0\) and $b=1$
\[a=3\]
This next question produces desired output, but gives error ``Missing \} inserted'':
\begin{question}
Here's a displayed equation (using brackets)
\[c=4\]
\end{question}
This last question gives errors ''Argument of verbatim has an extra \}'' and ''Paragraph ended before verbatim was complete'':
\begin{question}
Here's a displayed equation (using equation*)
\myverbenv{equation}{y=2}
\end{question}
\end{document}
重要的一行是:
\myverbenv{equation}{y=2}
这将创建equation
有内容的环境y=2
。
生成的文档: