请建议将 latex 转换为 xml cfg 代码以获得以下内容:
(1)LaTeX:
$D$
XML:
<inline-formula>
<tex-math><![CDATA[
$D$
]]>
</tex-math>
</inline-formula>
(2)LaTeX:
\begin{equation}
\label{eq7}
\Lambda \left(x\left| {y_{1},\ldots,y_{p}} \right.\right)=\frac{\Lambda
(y_{1},\ldots,y_{p},x)} {\Lambda (y_{1},\ldots,y_{p} )},
\end{equation}
XML:
<disp-formula id="s1234-E7">
<label alt="equation 7">(7)</label>
<tex-math><![CDATA[
\begin{equation}
\label{eq7}
\Lambda \left(x\left| {y_{1},\ldots,y_{p}} \right.\right)=\frac{\Lambda
(y_{1},\ldots,y_{p},x)} {\Lambda (y_{1},\ldots,y_{p} )},
\end{equation}
]]>
</tex-math>
</disp-formula>
答案1
以下代码是修改后的版本mathjax-latex-4ht包。另存为myaltmath.sty
:
\RequirePackage{etoolbox,expl3,environ}
\ExplSyntaxOn
\cs_new_protected:Npn \alteqtoks #1
{
\tl_set:Nx \l_tmpa_tl {\detokenize{#1}}
% delete spaces before left brackets
\regex_replace_all:nnN { \x{20} \x{7B} } { \x{7B} } \l_tmpa_tl
\tl_set:Nx \l_tmpb_tl{ \l_tmpa_tl }
\HCode{\l_tmpb_tl}
}
\newcommand\inlinemath[1]{#1}
% #1 math type, #2 math contents
\newcommand\displayblockmath[2]{#2}
\ExplSyntaxOff
\AtBeginDocument{%
\def\AltMathOne#1${\inlinemath{\alteqtoks{#1}}$}
\Configure{$}{}{}{\expandafter\AltMathOne}
\def\AltlMath#1\){\inlinemath{\alteqtoks{#1}}\)}
\Configure{()}{\AltlMath}{}
\def\AltlDisplay#1\]{\displayblockmath{math}{\alteqtoks{\[#1\]}\]}}
\Configure{[]}{\AltlDisplay}{}
\newcommand\VerbMathToks[2]{%
\displayblockmath{#2}{%
\alteqtoks{\begin{#2}
#1
\end{#2}}}%
}
\newcommand\VerbMath[1]{%
\ifcsdef{#1}{%
\RenewEnviron{#1}{%
\NoFonts\expandafter\VerbMathToks\expandafter{\BODY}{#1}\EndNoFonts%
}
}{}%
}
\VerbMath{align}
\VerbMath{equation}
\VerbMath{equation*}
\VerbMath{align*}
\VerbMath{cases}
\VerbMath{array}
\VerbMath{matrix}
\VerbMath{pmatrix}
\VerbMath{eqnarray}
\VerbMath{eqnarray*}
}
插入所需 XML 标签的配置保存在配套配置文件中myaltmath.4ht
:
\NewConfigure{InlineMath}{2}
\pend:defI\inlinemath{\a:InlineMath}
\append:defI\inlinemath{\b:InlineMath}
\Configure{InlineMath}{\HCode{<inline-formula>\Hnewline
<tex-math><![CDATA[\Hnewline$}}{\HCode{$\Hnewline]]>\Hnewline
</tex-math>\Hnewline
</inline-formula>\Hnewline}}
% configure display math
% we want to save the math type info to an XML attribute, so we need to use three hooks
\NewConfigure{DisplayMath}{3}
\renewcommand\displayblockmath[2]{\a:DisplayMath#1\b:DisplayMath#2\c:DisplayMath}
\newcounter{DisplayMathCount}
\Configure{DisplayMath}{\stepcounter{DisplayMathCount}\HCode{<disp-formula id="s1234-E\arabic{DisplayMathCount}" type="}}{\HCode{">\Hnewline
<tex-math><![CDATA[\Hnewline}}
{\HCode{\Hnewline]]>\Hnewline
</tex-math>\Hnewline
</disp-formula>\Hnewline}}
%<label alt="equation 7">(7)</label>
重要的是,\displayblockmath
配置会插入type
属性,然后可用于 DOM 操作。由于 LaTeX 代码是逐字保存的,因此无法<label>
从 TeX 端添加元素,必须在后处理阶段完成。
要需要该myaltmath
包,请使用以下配置文件:
\RequirePackage{myaltmath}
\Preamble{xhtml}
\begin{document}
\EndPreamble
后处理可以使用LuaXML构建文件中的库make4ht
(mybuild.mk4
):
local domfilter = require "make4ht-domfilter"
local counters = {}
local function stepcounter(countertype)
-- retrieve counter type value, or initialize a new one
local currentval = counters[countertype] or 0
local newval = currentval + 1
counters[countertype] = newval
return newval
end
local process = domfilter {
function(dom)
-- process all equations and make label with their equation number
for _, equation in ipairs(dom:query_selector("disp-formula")) do
if equation:get_attribute("type") == "equation" then
local count = stepcounter("equation")
local label = dom:create_element("label", {alt = "equation " .. count})
label:add_child_node(label:create_text_node(string.format("(%i)", count)))
equation:add_child_node(label, 1)
end
end
return dom
end
}
-- replace the html$ with extension which you actually use, probably xml$
Make:match("html$", process)
请注意,我在编写此构建文件时发现了 LuaXML 中的一些错误,修复版本需要一段时间才能在 CTAN 上发布。在此期间,您可以使用开发版本。
要编译,请使用以下命令
make4ht -uc myconfig.cfg -e mybuild.mk4 filename.tex
HTML 中的结果如下:
<!-- l. 6 --><p class='noindent'>Some text <inline-formula>
<tex-math><![CDATA[
D
]]>
</tex-math>
</inline-formula>
</p><!-- l. 12 --><p class='indent'> <disp-formula id='s1234-E1' type='equation'><label alt='equation 1'>(1)</label>
<tex-math><![CDATA[
\begin{equation} \label{eq7} \Lambda \left (x\left |{y_{1},\ldots ,y_{p}} \right .\right )=\frac{\Lambda (y_{1},\ldots ,y_{p},x)}{\Lambda (y_{1},\ldots ,y_{p} )}, \end{equation}
]]>
</tex-math>
</disp-formula>
</p>
如您所见,TeX 代码中的空白有点混乱,这是命令的结果\detokenize
。我不确定是否可以修复它。