1

When using Make4ht how to get MathML and LaTeX code in HTML?

My MWE is:

%!TEX TS-program = lualatex
%!TEX encoding = UTF-8 Unicode

\documentclass{elife}

\title{Grasp movement initiation and representation}

\begin{document}

We recorded single neurons in F1 and hold actions, and investigated the population-level differences could explain, please refer $t_{1,92} = -0.55$ plotted $\mathbf{V_{Obs}}$ at x10 gain)  how condition.


\end{document}
3
  • Do I understand correctly that you want to output both LaTeX math and MathML to the generated HTML? Should it support only inline math, or every possible math environment?
    – michal.h21
    Jan 29 at 20:36
  • @michal.h21: Yes, I have need both LaTeX math and MathML in the generated HTML and need all the math(inline and display)
    – Balaji
    Jan 30 at 2:14
  • @michal.h21: I have updated LaTeX file. $\mathbf{V_{Obs}}$ is also not properly converted.
    – Balaji
    Jan 30 at 12:13

1 Answer 1

1

It is possible to do that, but it is not that easy. Here is a MWE:

\documentclass{article}
\newcommand\eqannotate[1]{#1}

\title{Grasp movement initiation and representation}

\begin{document}

We recorded single neurons in F1 and hold actions, and investigated the population-level differences could explain, please refer $t_{1,92} = -0.55$ plotted $\mathbf{V_{Obs}}$ at x10 gain)  how condition.

\[
  c^2 = \sqrt{a^2+b^2}  
\]


\begin{equation}
  \eqannotate{c^2 = \sqrt{a^2+b^2}}
\end{equation}
\end{document}

As you can see, I introduced the \eqannotate command, which is used in the equation environment. I am afraid that it is not easily possible to catch contents of environments, so they need to be marked by hand (or using pre-processing script).

Here is a configuration file:

\Preamble{xhtml}
\newtoks\eqtoks 


\def\AltMath#1${\eqtoks{#1}% 
   #1\HCode{</mrow><annotation encoding="application/x-tex">\the\eqtoks</annotation>}$} 
\Configure{$}{\Configure{@math}{display="inline"}\DviMath\HCode{<semantics><mrow>}}{\HCode{</semantics>}\EndDviMath}{\expandafter\AltMath} 

\long\def\AltDisplay#1\]{\eqtoks{#1}#1\HCode{</mrow><annotation encoding="application/x-tex">\the\eqtoks</annotation></semantics>}\]}
\Configure{[]}{\Configure{@math}{display="block"}\DviMath$$\DisplayMathtrue\HCode{<semantics><mrow>}\AltDisplay}{$$\EndDviMath}

\renewcommand\eqannotate[1]{\eqtoks{#1}\HCode{<semantics><mrow>}#1\HCode{</mrow><annotation encoding="application/x-tex">\the\eqtoks</annotation></semantics>}}


\begin{document}
\EndPreamble

MathML has the <annotation> element, it can be used to insert raw LaTeX code to the HTML as an annotation.

\Configure{$} and \Configure{[]} configure what HTML code will be inserted for $ ... $ and \[ ... \]. It also calls command that saves the original LaTeX code in a token list, typesets MathML and then prints the saved tokens inside <annotation>. Most of this code is copied from mathml.4ht, it is only a bit simplified.

Regarding wrong formatting of $\mathbf{V_{Obs}}$, this can be fixed using the following DOM filter:

local domfilter = require "make4ht-domfilter"


local function find_mstyle(x)
  -- find if element has <mstyle> parent, and its value of mathvariant
  if not x:is_element() then
    return nil
  elseif x:get_element_name() == "mstyle" then
    return x:get_attribute("mathvariant")
  else
    return find_mstyle(x:get_parent())
  end
end


local process = domfilter {
  function(dom)
    -- fix bold subscript
    for _, el in ipairs(dom:query_selector "mstyle mi") do
      local mathvariant = find_mstyle(el:get_parent())
      if mathvariant and el:get_attribute("mathvariant") then
        el:set_attribute("mathvariant", mathvariant)
      end
    end
    return dom
  end 
}

Make:match("html$", process)

Compile using:

make4ht -e build.lua -c config.cfg sample.tex "mathml,mathjax"

This is the resulting HTML:

<p class='noindent'>We recorded single neurons in F1 and hold actions, and investigated
the population-level differences could explain, please refer
<!--  l. 9  --><math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><semantics><mrow><msub><mrow><mi>t</mi></mrow><mrow><mn>1</mn><mo class='MathClass-punc'>,</mo><mn>92</mn></mrow></msub> <mo class='MathClass-rel'>=</mo> <mo class='MathClass-bin'>−</mo><mn>0.55</mn></mrow><annotation encoding='application/x-tex'>t_{1,92} = -0.55</annotation></semantics></math> plotted
<!--  l. 9  --><math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><semantics><mrow><mstyle mathvariant='bold'><msub><mrow><mi>V</mi></mrow><mrow><mi mathvariant='bold'>Obs</mi></mrow></msub></mstyle></mrow><annotation encoding='application/x-tex'>\mathbf {V_{Obs}}</annotation></semantics></math> at
x10 gain) how condition.
</p><!--  l. 11  --><p class='indent'>   <!--  l. 11  --><math display='block' xmlns='http://www.w3.org/1998/Math/MathML'>
                                          <semantics><mrow><msup><mrow><mi>c</mi></mrow><mrow><mn>2</mn></mrow></msup> <mo class='MathClass-rel'>=</mo> <msqrt><mrow><msup><mrow><mi>a</mi></mrow><mrow><mn>2</mn> </mrow> </msup> <mo class='MathClass-bin'>+</mo> <msup><mrow><mi>b</mi></mrow><mrow><mn>2</mn></mrow></msup></mrow></msqrt></mrow><annotation encoding='application/x-tex'> c^2 = \sqrt {a^2+b^2} </annotation></semantics>
</math>
</p>
   <table class='equation'><tr><td>
<!--  l. 16  --><math class='equation' display='block' xmlns='http://www.w3.org/1998/Math/MathML'>
                           <mstyle class='label' id='x1-2r1'></mstyle><!--  endlabel  --><semantics><msup><mrow><mi>c</mi></mrow><mrow><mn>2</mn></mrow></msup> <mo class='MathClass-rel'>=</mo> <msqrt><mrow><msup><mrow><mi>a</mi></mrow><mrow><mn>2</mn> </mrow> </msup> <mo class='MathClass-bin'>+</mo> <msup><mrow><mi>b</mi></mrow><mrow><mn>2</mn></mrow></msup></mrow></msqrt><annotation encoding='application/x-tex'>c^2 = \sqrt {a^2+b^2}</annotation></semantics>
</math></td><td class='eq-no'>(1)</td></tr></table>

And rendered page:

enter image description here

9
  • Thanks for your effort. But I have getting --- error --- Illegal storage address [FATAL] make4ht-lib: Fatal error. Command tex4ht returned exit code 1 E:\texlive\2021\bin\win32\runscript.tlu:915: command failed with exit code 1: texlua e:\texlive\2021\texmf-dist\scripts\make4ht\make4ht -e build.lua -c config.cfg sample.tex "mathml,mathjax"
    – Balaji
    Jan 31 at 0:11
  • I have using Windows 10 and TeXLive 2021
    – Balaji
    Jan 31 at 0:12
  • @Balaji and normally make4ht works?
    – michal.h21
    Jan 31 at 9:41
  • Yes. Previously also works well except above.
    – Balaji
    Jan 31 at 10:45
  • @Balaji can you please update your question with full log of make4ht -e build.lua -c config.cfg sample.tex "mathml,mathjax"?
    – michal.h21
    Jan 31 at 11:54

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.