根据我的要求,我需要<span class="inlinemath">...</span>
所有内联数学的标签,因此我创建了如下 .cfg 文件:
\Preamble{xhtml,fn-in}
\RequirePackage{etoolbox}% for the \forsvlist command
\newtoks\eqtoks
\def\AltMath#1${\eqtoks{$#1$}%
\HCode{\the\eqtoks}$}
\Configure{$}{\HCode{<span class="inlinemath">}}{\HCode{</span>}}{}
\begin{document}
\EndPreamble
示例.tex
\documentclass{book}
\begin{document}
This is for test $a+b=c$ then complex equations $\frac{1}{2}+c=z$
\end{document}
转换:
htlatex test "test,xhtml" " -cunihft" "-cvalidate -p"
转换后的 HTML 文件:
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd-->
<html xmlns="http://www.w3.org/1999/xhtml"
>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)" />
<meta name="originator" content="TeX4ht (http://www.tug.org/tex4ht/)" />
<!-- xhtml,html,fn-in -->
<meta name="src" content="test.tex" />
<meta name="date" content="2017-08-16 14:36:00" />
<link rel="stylesheet" type="text/css" href="test.css" />
</head><body
>
<!--l. 5--><p class="noindent" >This is for test <span class="inlinemath"><span
class="cmmi-10">a </span>+ <span
class="cmmi-10">b </span>= <span
class="cmmi-10">c</span></span> then complex equations <span class="inlinemath"><img
src="test0x.png" alt="12" class="frac" align="middle" /> + <span
class="cmmi-10">c </span>= <span
class="cmmi-10">z</span></span>
</p>
</body></html>
这工作正常,但除了我的要求之外,我还需要将 HTML 标签更改<span class="inlinemath">
为<span class="inlinemathimage">
标签\frac, \sqrt, \sum,
出现在内联数学中。
我该如何实现这一点?请提出建议
答案1
看起来您想要检测包含图像的内联数学元素。我们可以使用make4ht
对文件执行的后处理过滤器轻松做到这一点HTML
。我们可以使用开发版本的LuaXML库,它支持使用查询选择 XML 元素CSS
。
保存以下代码为test.mk4
local filter = require "make4ht-filter"
local dom = require "luaxml-domobject"
local inlinemath = function(s)
local tree = dom.parse(s)
-- search for all elements with `inlinemath` class
for _,el in ipairs(tree:query_selector(".inlinemath")) do
local images = el:query_selector("img")
if #images > 0 then
el:set_attribute("class", "inlinemathimage")
end
end
return tree:serialize()
end
local process = filter {inlinemath}
Make:match(".html$", process)
有趣的是这个:
local tree = dom.parse(s)
-- search for all elements with `inlinemath` class
for _,el in ipairs(tree:query_selector(".inlinemath")) do
local images = el:query_selector("img")
if #images > 0 then
el:set_attribute("class", "inlinemathimage")
end
end
return tree:serialize()
它解析作为字符串传递给过滤器的 HTML 文档的内容,inlinemath
使用函数选择所有元素tree:query_selector(".inlinemath")
,该函数返回一个数组。使用函数循环数组ipairs
,每个选定的元素都可以通过el
变量访问。我们可以再次使用query_selector
,这次是为了选择img
元素。images
数组将包含超过零个对象,如果有任何元素,在这种情况下,我们可以将当前img
设置为。class
el
inlinemathimage
编译使用:
make4ht -uc config.cfg test.tex "" "" " -cvalidate -p"
以下测试文件:
\documentclass{article}
\begin{document}
Some text $a=b$, simple inline math, $\sqrt{c}$ -- square root, $\frac{a}{b}$ -- fraction.
\end{document}
产生以下 HTML 结果:
<?xml encoding='utf-8' version='1.0'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd -->
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<meta content='TeX4ht (http://www.tug.org/tex4ht/)' name='generator' />
<meta content='TeX4ht (http://www.tug.org/tex4ht/)' name='originator' />
<!-- charset=utf-8,html,xhtml,fn-in -->
<meta content='sample.tex' name='src' />
<link rel='stylesheet' type='text/css' href='sample.css' />
</head><body>
<!-- l. 6 --><p class='noindent'>Some text <span class='inlinemath'><span class='cmmi-10'>a </span>= <span class='cmmi-10'>b</span></span>, simple inline math, <span class='inlinemathimage'><img alt='√c-' src='sample0x.png' class='sqrt' /></span> – square root, <span class='inlinemathimage'><img align='middle' alt='a
b' class='frac' src='sample1x.png' /></span> – fraction.
</p>
</body></html>