Auch MathML benötigt LaTeX-Codierung in tex4ht

Oct 09 2020

Ich möchte die folgende Datei in ein MathMLFormat konvertieren und benötige auch die LaTeXCodierung im MathMLSemantic-Tag.

MWE:

\documentclass{article}
\usepackage[T1]{fontenc}

\begin{document}

\article{Article Title Here}
\author{Author Name Here}
\maketitle

\section{Introduction}

This is the sample paragraph.
 \begin{equation}\label{eq1-11}
T\,^{\prime}_{\mu \nu} = \left( \frac{\partial \xi^\alpha} {\partial\xi^{\prime\mu}}\right) \left( \frac{\partial \xi^\beta}{\partial \xi^{\prime\nu}} \right) T_{\alpha \beta}
\end{equation}

Please refer the equations \ref{eq1-11} for the further testing.
\end{document}

Antworten

1 michal.h21 Oct 11 2020 at 19:43

Es gibt verschiedene mögliche Ansätze, um dies zu erreichen:

  1. Konfigurieren Sie TeX4ht so, dass alle mathematischen Inhalte abgefangen und zweimal gesetzt werden - einmal mit MathML, zum zweiten Mal als wörtlicher Text.
  2. Analysieren Sie MathML-Inhalte und konvertieren Sie sie zurück in den LaTeX-Code
  3. Verarbeiten Sie die eingegebene TeX-Datei vor und ändern Sie sie so, dass die Arbeit mit ihr einfacher wird

Die erste Methode könnte den Code, den wir für die MathJax-Option in TeX4ht verwenden, wiederverwenden. Weitere mathjax-latex-4ht.4htInformationen finden Sie in der Datei .

Die zweite Methode erzeugt nicht den gleichen LaTeX-Code wie die ursprüngliche Eingabe. Es kann ein Problem für Sie sein. Für die Konvertierung kann LuaXML verwendet werden .

Ich werde die dritte Methode in meiner Antwort vorstellen. Es besteht aus zwei Komponenten - dem Eingabefilter, der die eingegebene LaTeX-Datei für den mathematischen Inhalt analysiert und mit einigen zusätzlichen Makros markiert, und dem make4ht-DOM-Filter, der die resultierende HTML-Datei ändert, um die richtige MathML-Struktur zu erzeugen.

Hier ist der Eingangsfilter. Es liest Eingaben von der Standardeingabe und druckt die geänderte Ausgabe.

Datei altmath.lua:

-- insert envrionmnets that should be handled by the script here
local math_environments = {
  equation = true,
  displaymath = true,
  ["equation*"] = true,

}

-- macros that will be inserted to the updated document
local macros = [[
\NewDocumentCommand\inlinemath {mv} {\HCode{<span class="inlinemath">}#1\HCode{<span class="alt">}\NoFonts #2\EndNoFonts\HCode{</span></span>}}
\NewDocumentEnvironment{altdisplaymath}{} {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="altmath">}} {\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}
]]

-- we will insert macros before the second control sequence (we assume that first is \documentclass
local cs_counter = 0

-- we will hanlde inline and diplay math differently
local inline  = 1
local display = 2

local function handle_math(input, nexts, stop, buffer, mathtype)
  local content = input:sub(nexts, stop)
  local format = "\\inlinemath{%s}{%s}" -- format used to insert math content back to the doc
  -- set format for display math
  if mathtype == display then
    format = [[
\begin{altdisplaymath}
%s
\begin{verbatim}
%s
\end{verbatim}
\end{altdisplaymath}
]]
  end
  buffer[#buffer + 1] =  string.format(format, content, content )
end

local function find_next(input, start, buffer)
  -- find next cs or math start
  local nexts, stop = input:find("[$\\]", start) local mathtype if nexts then -- save current text chunk from the input buffer buffer[#buffer+1] = input:sub(start, nexts - 1) local kind, nextc = input:match("(.)(.)", nexts) if kind == "\\" then -- handle cs -- insert our custom TeX macros before second control sequence cs_counter = cs_counter + 1 if cs_counter == 2 then buffer[#buffer+1] = macros end if nextc == "(" then -- inline math _, stop = input:find("\\)", nexts) mathtype = inline elseif nextc == "[" then -- display math _, stop = input:find("\\]", nexts) mathtype = display else -- maybe environment? -- find environment name local env_name = input:match("^begin%s*{(.-)}", nexts+1) -- it must be enabled as math environment if env_name and math_environments[env_name] then _, stop = input:find("\\end%s*{" .. env_name .. "}", nexts) mathtype = display else -- not math environment buffer[#buffer+1] = "\\" -- save backspace that was eaten by the processor return stop + 1 -- return back to the main loop end end else -- handle $
      if nextc == "$" then -- display math _, stop = input:find("%$%$", nexts + 1) mathtype = display else -- inline math _, stop = input:find("%$", nexts + 1)
        mathtype = inline
      end
    end
    if not stop then -- something failed, move one char next
      return nexts + 1
    end
    -- save math  content to the buffer
    handle_math(input, nexts, stop, buffer, mathtype)
  else
    -- if we cannot find any more cs or math, we need to insert rest of the input 
    -- to the output buffer
    buffer[#buffer+1] = input:sub(start, string.len(input))
    return nil
  end
  return stop + 1
end

-- process the input buffer, detect inline and display math and also math environments
local function process(input)
  local buffer = {} -- buffer where text chunks are stored
  local start = 1
  start = find_next(input, start,buffer)
  while start do
    start = find_next(input, start, buffer)
  end
  return table.concat(buffer) -- convert output buffer to string
end


local content = io.read("*all")
print(process(content))

Sie können es mit dem folgenden Befehl testen:

texlua altmath.lua < sample.tex

Dies ist eine modifizierte Version Ihrer ursprünglichen TeX-Datei:

\documentclass{article}
\NewDocumentCommand\inlinemath {mv} {\HCode{<span class="inlinemath">}#1\HCode{<span class="alt">}\NoFonts #2\EndNoFonts\HCode{</span></span>}}
\NewDocumentEnvironment{altdisplaymath}{} {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="altmath">}} {\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}
\usepackage[T1]{fontenc}

\begin{document}

\title{Article Title Here}
\author{Author Name Here}
\maketitle

\section{Introduction}

This is the sample paragraph with \inlinemath{$a=b^2$}{$a=b^2$} inline math. Different \inlinemath{\(a=c^2\)}{\(a=c^2\)} type of math.
 \begin{altdisplaymath}
\begin{equation}\label{eq1-11}
T\,^{\prime}_{\mu \nu} = \left( \frac{\partial \xi^\alpha} {\partial\xi^{\prime\mu}}\right) \left( \frac{\partial \xi^\beta}{\partial \xi^{\prime\nu}} \right) T_{\alpha \beta}
\end{equation}
\begin{verbatim}
\begin{equation}\label{eq1-11}
T\,^{\prime}_{\mu \nu} = \left( \frac{\partial \xi^\alpha} {\partial\xi^{\prime\mu}}\right) \left( \frac{\partial \xi^\beta}{\partial \xi^{\prime\nu}} \right) T_{\alpha \beta}
\end{equation}
\end{verbatim}
\end{altdisplaymath}


Please refer the equations \ref{eq1-11} for the further testing.
\end{document}

Sie können sehen, dass nach dem \documentclassBefehl Makrodefinitionen eingefügt werden . Es definiert den \inlinemathBefehl und die altdisplaymathUmgebung. Die Definitionen enthalten Code, der HTML-Tags direkt in die konvertierte Datei einfügt. Sie sind nur für die Verwendung mit TeX4ht konzipiert.

Sie können Ihre Datei mit in HTML konvertieren

texlua altmath.lua < sample.tex | make4ht -j sample - "mathml"

Es wird folgender Code erzeugt:

<span class='inlinemath'><!-- l. 14 --><math xmlns='http://www.w3.org/1998/Math/MathML' display='inline'><mi>a</mi> <mo class='MathClass-rel'>=</mo> <msup><mrow><mi>b</mi></mrow><mrow><mn>2</mn></mrow></msup></math><span class='alt'>$a=b^2$</span></span> 

oder

<div class='altmath'> <!-- tex4ht:inline --><table class='equation'><tr><td>
<!-- l. 16 --><math xmlns='http://www.w3.org/1998/Math/MathML' display='block' class='equation'>
                       <mstyle class='label' id='x1-1001r1'></mstyle><!-- endlabel --><mi>T</mi><msubsup><mrow><mspace width='0.17em' class='thinspace'></mspace></mrow><mrow><mi mathvariant='italic'>μν</mi></mrow><mrow><mi>′</mi></mrow></msubsup> <mo class='MathClass-rel'>=</mo> <mrow><mo form='prefix' fence='true'> (</mo><mrow> <mfrac><mrow><mi>∂</mi><msup><mrow><mi>ξ</mi></mrow><mrow><mi>α</mi></mrow></msup></mrow>
<mrow><mi>∂</mi><msup><mrow><mi>ξ</mi></mrow><mrow><mi mathvariant='italic'>′μ</mi></mrow></msup></mrow></mfrac> </mrow><mo form='postfix' fence='true'>)</mo></mrow> <mrow><mo form='prefix' fence='true'> (</mo><mrow> <mfrac><mrow><mi>∂</mi><msup><mrow><mi>ξ</mi></mrow><mrow><mi>β</mi></mrow></msup></mrow>
<mrow><mi>∂</mi><msup><mrow><mi>ξ</mi></mrow><mrow><mi mathvariant='italic'>′ν</mi></mrow></msup></mrow></mfrac> </mrow><mo form='postfix' fence='true'>)</mo></mrow> <msub><mrow><mi>T</mi></mrow><mrow><mi mathvariant='italic'>αβ</mi></mrow></msub>
</math></td><td class='eq-no'>(1)</td></tr></table>
<!-- l. 18 --><p class='nopar'>

</p>
   <pre id='verbatim-1' class='verbatim'>
\begin{equation}\label{eq1-11}
T\,^{\prime}_{\mu \nu} = \left( \frac{\partial \xi^\alpha} {\partial\xi^{\prime\mu}}\right) \left( \frac{\partial \xi^\beta}{\partial \xi^{\prime\nu}} \right) T_{\alpha \beta}
\end{equation}
</pre>
<!-- l. 23 --><p class='nopar'> </p></div>

Wir müssen den make4ht DOM-Filter verwenden, um eine korrekte MathML-Struktur zu erstellen. Speichern Sie die folgende Datei als build.lua:

local domfilter = require "make4ht-domfilter"

-- find mathml and insert TeX as an alternative annotation
local function update_mathml(element, class)
  local alt_element_t = element:query_selector(class)
  if not alt_element_t and not alt_element_t[1] then return nil end
  -- save alt element contents and remove it from the document
  local alt_contents = alt_element_t[1]:get_children()
  alt_element_t[1]:remove_node()
  -- create a new structure of the mathml element ->
  -- mathml 
  --   semantics
  --     mrow -> math content
  --     annotation -> saved TeX
  local mathml = element:query_selector("math")[1]
  local mathml_contents = mathml:get_children()
  local semantics = mathml:create_element("semantics")
  local mrow = semantics:create_element("mrow")
  mrow._children = mathml_contents -- this trick places saved original mathml content into a new <mrow>
  semantics:add_child_node(mrow)
  local annotation = semantics:create_element("annotation", {encoding="application/x-tex"})
  annotation._children = alt_contents
  semantics:add_child_node(annotation)
  mathml._children = {semantics}
end

local process = domfilter {
  function(dom)
    for _, inline in ipairs(dom:query_selector(".inlinemath")) do
      update_mathml(inline, ".alt")
    end
    for _, display in ipairs(dom:query_selector(".altmath")) do
      update_mathml(display, ".verbatim")
    end
    return dom
  end
}

Es analysiert die HTML-Dateien für unsere benutzerdefinierten Elemente <span>und <div>Elemente, ruft den Alternativtext ab und fügt ihn als '`Element des MathML-Codes ein.

Das ist das Ergebnis:

   <h3 class='sectionHead'><span class='titlemark'>1   </span> <a id='x1-10001'></a>Introduction</h3>
<!-- l. 14 --><p class='noindent'>This  is  the  sample  paragraph  with
<span class='inlinemath'><!-- l. 14 --><math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><semantics><mrow><mi>a</mi> <mo class='MathClass-rel'>=</mo> <msup><mrow><mi>b</mi></mrow><mrow><mn>2</mn></mrow></msup></mrow><annotation encoding='application/x-tex'>$a=b^2$</annotation></semantics></math></span> inline math.
Different <span class='inlinemath'><!-- l. 14 --><math display='inline' xmlns='http://www.w3.org/1998/Math/MathML'><semantics><mrow><mrow><mi>a</mi> <mo class='MathClass-rel'>=</mo> <msup><mrow><mi>c</mi></mrow><mrow><mn>2</mn></mrow></msup></mrow></mrow><annotation encoding='application/x-tex'>\(a=c^2\)</annotation></semantics></math></span>
type of math. </p><div class='altmath'> <!-- tex4ht:inline --><table class='equation'><tr><td>
<!-- l. 16 --><math class='equation' xmlns='http://www.w3.org/1998/Math/MathML' display='block'><semantics><mrow>
                       <mstyle id='x1-1001r1' class='label'></mstyle><!-- endlabel --><mi>T</mi><msubsup><mrow><mspace width='0.17em' class='thinspace'></mspace></mrow><mrow><mi mathvariant='italic'>μν</mi></mrow><mrow><mi>′</mi></mrow></msubsup> <mo class='MathClass-rel'>=</mo> <mrow><mo fence='true' form='prefix'> (</mo><mrow> <mfrac><mrow><mi>∂</mi><msup><mrow><mi>ξ</mi></mrow><mrow><mi>α</mi></mrow></msup></mrow>
<mrow><mi>∂</mi><msup><mrow><mi>ξ</mi></mrow><mrow><mi mathvariant='italic'>′μ</mi></mrow></msup></mrow></mfrac> </mrow><mo fence='true' form='postfix'>)</mo></mrow> <mrow><mo fence='true' form='prefix'> (</mo><mrow> <mfrac><mrow><mi>∂</mi><msup><mrow><mi>ξ</mi></mrow><mrow><mi>β</mi></mrow></msup></mrow>
<mrow><mi>∂</mi><msup><mrow><mi>ξ</mi></mrow><mrow><mi mathvariant='italic'>′ν</mi></mrow></msup></mrow></mfrac> </mrow><mo fence='true' form='postfix'>)</mo></mrow> <msub><mrow><mi>T</mi></mrow><mrow><mi mathvariant='italic'>αβ</mi></mrow></msub>
</mrow><annotation encoding='application/x-tex'>
\begin{equation}\label{eq1-11}
T\,^{\prime}_{\mu \nu} = \left( \frac{\partial \xi^\alpha} {\partial\xi^{\prime\mu}}\right) \left( \frac{\partial \xi^\beta}{\partial \xi^{\prime\nu}} \right) T_{\alpha \beta}
\end{equation}
</annotation></semantics></math></td><td class='eq-no'>(1)</td></tr></table>
<!-- l. 18 --><p class='nopar'>

</p>
   
<!-- l. 23 --><p class='nopar'> </p></div>
MadyYuvi Oct 09 2020 at 15:37

Vorausgesetzt, MWEdass viele LaTeX-Codierungsfehler vorliegen, habe ich Folgendes behoben und die geänderten Tags sind:

\documentclass{article} 
\usepackage[T1]{fontenc}

\begin{document}

\title{Article Title Here}

\author{Author Name Here}

\maketitle

\section{Introduction}

This is the sample paragraph.
\begin{equation}\label{eq1-11}
T\,^{\prime}_{\mu \nu} = \left( \frac{\partial \xi^{\alpha}}
{\partial\xi^{\prime\mu}}\right) \left( \frac{\partial \xi^{\beta}}{\partial \xi^{\prime\nu}} \right) T_{\alpha \beta}
\end{equation}

Please refer the equations \ref{eq1-11} for the further testing.
\end{document}

Nachdem ich die Fehler behoben habe, habe ich den Befehl ausgeführt

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

Es konvertiert gut ...

BEARBEITEN

Wenn Sie die LaTeXTags in der konvertierten anzeigen möchten HTML, verwenden Sie die folgende .cfgDatei:

convert.cfg

\RequirePackage{verbatim,etoolbox}

\Preamble{xhtml}
\def\AltMathOne#1${\HCode{\detokenize{\(#1\)}}$}
\Configure{$}{}{}{\expandafter\AltMathOne} \def\AltlMath#1\){\HCode{\detokenize{\(#1\)}}\)} \Configure{()}{\AltlMath}{} \def\AltlDisplay#1\]{\HCode{\detokenize{\[#1\]}}\]} \Configure{[]}{\AltlDisplay}{} \def\AltDisplayOne#1#2$${#1\HCode{\detokenize{$$#2$$}}$$} \Configure{$$}{}{}{\AltDisplayOne}{}{}
\newcommand\VerbMath[1]{%
\ifcsdef{#1}{%
  \renewenvironment{#1}{%
    \NoFonts%
  \Configure{verbatim}{}{} % suppress <br /> tags
    \texttt{\string\begin\{#1\}}\HCode{\Hnewline}% we need to use \texttt to get all characters right
      \verbatim}{\endverbatim\texttt{\string\end\{#1\}}\EndNoFonts}%
}{}%
}
\VerbMath{align}
\VerbMath{equation}
\VerbMath{equation*}

\begin{document}

\EndPreamble

Dann der Befehl run:

htlatex sample "conversion" " " "-cvalidate -p"