1

As per my requirement, I need the tag <span class="inlinemath">...</span> for all inline math, so I've created the .cfg file like:

\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

Sample.tex

\documentclass{book}

\begin{document}

This is for test $a+b=c$ then complex equations $\frac{1}{2}+c=z$

\end{document}

Conversion:

htlatex test "test,xhtml" " -cunihft" "-cvalidate -p"

Converted HTML file:

<?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> 

This works fine, but in addition to my requirement, I need to change the HTML tag <span class="inlinemath"> to <span class="inlinemathimage"> when the tags \frac, \sqrt, \sum, comes in an inline math.

How can I achieve this? Please suggest

  • 1
    I will be offline this week, so I can't test anything, but I guess that make4ht filter on html file is an only option – michal.h21 Aug 7 '17 at 16:29
  • Expecting your valuable suggestions, please.... – MadyYuvi Aug 8 '17 at 6:23
  • the math comes as images with your configuration. is that intentional? Don't you want to keep the macros preserved? – michal.h21 Aug 15 '17 at 11:55
  • @michal.h21 I have edited my question, and added live sample for your reference... – MadyYuvi Aug 16 '17 at 9:12
  • Maybe you want to change the class every time image is used in the inline math? Not just in the case of \frac, \sqrt and \sum? – michal.h21 Aug 16 '17 at 9:28
2

It seems that you want to detect inline math elements which contain images. We can do that easily using make4ht post-processing filter executed on the HTML file. We may use the development version of LuaXML library, it supports selection of XML elements using CSS queries.

Save the following code as 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)

The interesting part is this one:

  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()

It parses the contents of the HTML document which is passed as string to the filter, all inlinemath elements are selected using tree:query_selector(".inlinemath") function, which returns an array. The array is looped using ipairs function, every selected element is accessible using el variable. We can use the query_selector again, this time to select img elements. The images array will contain more than zero objects, if there are any img elements, in this case we can set class of the current el to inlinemathimage.

Compile using:

 make4ht -uc config.cfg test.tex "" "" " -cvalidate -p"

The following test file:

\documentclass{article}

\begin{document}


Some text $a=b$, simple inline math, $\sqrt{c}$ -- square root, $\frac{a}{b}$ -- fraction.

\end{document}

Produces a following HTML result:

<?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>
  • Given suggestion works only in LuaTeX? But I am using MikTeX Version 2.9, please confirm ... – MadyYuvi Aug 16 '17 at 11:34
  • @MadyYuvi no, it only uses Lua script to postprocess the HTML file. But you will need to install LuaXML from github – michal.h21 Aug 16 '17 at 11:36
  • Sorry to disturb you again & again, can you provide the link for LuaXML from github, please... – MadyYuvi Aug 16 '17 at 11:37
  • @MadyYuvi sure github.com/michal-h21/LuaXML – michal.h21 Aug 16 '17 at 11:51
  • Sorry sir, I have downloaded successfully, but can you suggest how to install it? :-( – MadyYuvi Aug 16 '17 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.