Kindly suggest latex to xml cfg code to get the following:

(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>
  • If you use \verb| ... | in the LaTeX code then tex4ht will not change the contents in the HTML output. – musarithmia Jul 11 at 14:02

The following code is modified version of the mathjax-latex-4ht package. Save it as 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*}
}

The configurations which insert the required XML tags are saved in companion configuraton file, 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>

Important thing is that the \displayblockmath configuration inserts the type attribute, which can be then used for DOM manipulations. Because the LaTeX code is saved verbatim, it is not possible to add the <label> element from the TeX side, it must be done in the post-processing stage.

To require the myaltmath package, use the following configuration file:

\RequirePackage{myaltmath}
\Preamble{xhtml}
\begin{document}
\EndPreamble

The post-processing can be done using the LuaXML library in the make4ht build file (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)

Please note that I've found some bugs in LuaXML when I wrote this build file and it will take some time before the fixed version will be on CTAN. You can use the development version in the meantime.

To compile, use the following command

make4ht -uc myconfig.cfg -e mybuild.mk4 filename.tex

This is the result in 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>

As you can see, the white space is a bit messed up in the TeX code, this is a consequence of the \detokenize command. I am not sure that it can be fixed.

  • we are getting the following error message Output dir: Compiler: latex Latex options: -jobname=math tex4ht.sty : myconfig.cfg,,charset=utf-8 tex4ht -cmozhtf -utf8 build_file mybuild.mk4 Using build file mybuild.mk4 [string "local domfilter = require "make4ht-domfilter"..."]:1: module 'make4ht-domfilter' not found: no field package.preload['make4ht-domfilter'] [kpse lua searcher] file not found: 'make4ht-domfilter' [kpse C searcher] file not found: 'make4ht-domfilter' /usr/share/texlive/texmf-dist/scripts/make4ht/mkutils.lua:290: assertion failed! – Purushothaman Jul 13 at 5:07
  • @Purushothaman which TeX distribution do you use? It seems that it contains older version of make4ht. It is best to use TL 2018. You may also try to install the development version of make4ht. – michal.h21 Jul 13 at 7:48
  • We are using TeXLive 2015, ubuntu 16.04. We have updated the latest make4ht from the github. .../texmf/scripts/lua/make4ht/filters/make4ht-domfilter.lua:2: module 'luaxml-domobject' not found: no field package.preload['luaxml-domobject'] [kpse lua searcher] file not found: 'luaxml-domobject' [kpse C searcher] file not found: 'luaxml-domobject' /home/threetwo/texmf/scripts/lua/make4ht/mkutils.lua:413: assertion failed! – Purushothaman Jul 14 at 5:20
  • @Purushothaman TL 2015 is really old, many things will be broken. The particular error message you got means that you need to update also LuaXML. But as I said, you may have a better luck with updated TL. – michal.h21 Jul 14 at 7:41
  • DOM parsing of math.html failed: ...ive/2018/texmf-dist/tex/luatex/luaxml/luaxml-mod-xml.lua:174: Unbalanced Tag (/tex-math) [char=457] parse_lg process file: math.html outdir: parse_lg process file: math.css outdir: parse_lg process file: math.tmp tmp file – Purushothaman Jul 17 at 6:02

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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