htlatex filename.tex “xhtml,mathml”

I want to generate XHTML/MathML along with all equations as image. If I run the below command, I get html and equation-images

htlatex filename.tex


If I run the below command, I get html, and Mathml but not equation-images.

htlatex filename.tex "xhtml,mathml"


Please advise how to get HTML, MathML along with images for all mathml/equations.

• You can get either mathml or image math by default. These modes are incompatible, so it is impossible to get them in one compilation. Only hacky solutions can be provided for this. What is the use case? – michal.h21 Sep 8 '15 at 19:14
• Hi Michal, My client wants to have both MathML and Images as @alt in the html we produce. Even though MathML is more than enough, some of the platform requires both of them for fallback. – user3318250 Sep 9 '15 at 13:00
• Ok, some really hacky solution would be to produce images first, then create html with mathml and postprocess the html with some script and include the images. This is of course really fragile solution – michal.h21 Sep 9 '15 at 13:12
• @michal.h21 The right thing to do is to control what gets emitted using Javascript. I don't see why this would need to be done at a post-processing stage. – Charles Stewart Sep 10 '15 at 10:23
• @CharlesStewart I don't understand what you mean. There is no Javascript involved in LatTeX to HTML conversion by tex4ht by default – michal.h21 Sep 10 '15 at 10:26

This is impossible by default, some macros are redefined with mathml output and image output would be wrong. Some possible solution could be to produce document with images first and then reuse images with mathml. The issue is that the images may get out of sync with mathml and you would need to manually correct it.

IMHO, easiest way is to use some external scripting. Extracting generated mathml and converting it to images seems to be a best solution. For this task, we can use slimer.js, command line scriptable browser.

We can script it to save the images - it is based on Firefox's Gecko engine which supports mathml.

For mathml extraction, we can use make4ht filter, mathml-alt.mk4:

local filter = require "make4ht-filter"
local i = 0
local process = filter{function(s)
local t = {}
local par = Make.params
local s =  s:gsub("(<math.-[/itex])",function(a)
i = i + 1
local fn = string.format("%s-%d.%s", par.input, i, "png")
local img = string.format("<img src='%s' />", fn)
table.insert(t, {mathml=a, file = fn})
return a .. img
end)
local xml =io.open(par.input .. "-mathml.xml", "w")
xml:write("<mathbundle>\n")
for _,v in ipairs(t) do
xml:write(string.format("<mathitem filename='%s'>\n", v.file))
xml:write(v.mathml)
xml:write("</mathitem>\n")
end
xml:write("</mathbundle>")
xml:close()
return s
end}

Make:htlatex {}
Make:match("html$", process)  compile the TeX file with make4ht -e mathml-alt.mk4 filename mathml  this build file uses filter which process the html file and save all mathml elements to a xml file, filename-mathml.xml. It also inserts <img> elements pointing to not yet existing images directly after [/itex] tags. You may adapt the behavior to suit your needs, this version doesn't look nice. Now we need to create some simple script for slimer.js to save web pages as image. scripts are created in JavaScript. we can name it saveimage.js: var page = require('webpage').create(); var input = phantom.args[0]; var output = phantom.args[1]; page.open(input, function (status) { page.render(output); slimer.exit(); });  it takes two arguments, first is path to html page, the second name of the image. now we need to process the xml file with saved mathml and convert it to images. processmathml.lua: local file = io.open(arg[1],"r") local s = file:read("*all") local dir = lfs.currentdir() file:close() local tpl = [[ <DOCTYPE html> <html> <head> <meta charset="utf-8" /> <style type="text/css"> width:auto; height:auto; </style> </head> <body> %s </body> </html> ]] for filename, mathml in s:gmatch("<mathitem filename='(.-)'>%s*(.-)</mathitem>") do local htmlname = filename:gsub("%.[^%.]+$",".html")
print(htmlname)
local f = io.open(htmlname,"w")
f:write(string.format(tpl, mathml))
f:close()
local fn = "file://"..dir .. "/" .. htmlname
os.execute("slimerjs saveimage.js ".. fn .. " " .. filename)
os.execute("convert -trim ".. filename .. " ".. filename)
-- os.execute("./autotrim ".. filename .. " ".. filename)
-- print(filename, mathml)
end


run it as

texlua processmathml.lua filename-mathml.xml


it processes all saved mathml, saves it to an empty html page, execute slimer script and then trim spurious whitespace with imagemagick.

the result:

as you can see the rendering looks same, as I use Firefox, the only difference is that imline math images have wrong baseline, which is common issue for image math. And of course, it doesn't as good as images generated by LaTeX.

Tex file used for examples:

\documentclass[12pt]{article}
\usepackage{amssymb,amsmath,latexsym}

% Page length commands go here in the preamble
\setlength{\oddsidemargin}{-0.25in} % Left margin of 1 in + 0 in = 1 in
\setlength{\textwidth}{7in}   % Right margin of 8.5 in - 1 in - 6.5 in = 1 in
\setlength{\topmargin}{-.75in}  % Top margin of 2 in -0.75 in = 1 in
\setlength{\textheight}{9.2in}  % Lower margin of 11 in - 9 in - 1 in = 1 in

\newtheorem{theorem}{Theorem}
\newtheorem{definition}{Definition}

\renewcommand{\baselinestretch}{1.5} % 1.5 denotes double spacing. Changing it will change the spacing

\setlength{\parindent}{0in}
\begin{document}
\title{A Sample \LaTeX \;Article}
\author{John Doe}
\date{\today}
\maketitle
\abstract{This a sample \LaTeX document that explains some of the \LaTeX commands}

\section{Introduction}
\LaTeX \; is a markup language designed and implemented by \textbf{Leslie Lamport}, based on \textbf{Donald E. Knuth}'s typesetting language \TeX.  The markup in the source file of a \LaTeX \; document my appear somewhat challenging, but the compiled result of the document is certainly a pleasing rendering of the mark-up material.\\

\LaTeX \; was built on \TeX 's foundation.  An article is divided into \emph{logical units}, including an abstract, various sections and subsections, theorems, and a bibliography.  The logical units are typed independently of one another.  Once all the units have been typed, \LaTeX \, controls the \emph{placement} and \emph{formating} of these elements. \LaTeX \; automatically numbers the sections, theorems, and equations in your article, and builds the cross-references.  If any changes is made to the article, it automatically renumbers its various parts and rebuilds the cross-references.\\

\emph{Packages} are extensions of \LaTeX.  \LaTeX \; commands, as a rule, start with a backslash (\textbackslash) and tells \LaTeX  to do something special. For example, in the instruction\\
\verb+\emph{instructions to \LaTeX} +, \verb+\emph+ is a \LaTeX \; command. Another kind of instruction is called an \emph{environment}. For example, the commands \verb+\begin{flushright}+ and \verb+\end{flushright}+ enclose a \verb+flushright+ environment---texts that are typed inside this environment are right justified (lined up against the right margin) when typeset.

\section{Typing Text}
The following keys are used to type text in a \LaTeX \; source file:
\begin{center}
\begin{verbatim}
a-z  A-Z  0-9
+  =  *  /  ( )  [ ]
\end{verbatim}
\end{center}
You may also use the following punctuation marks:
\begin{center}
\begin{verbatim}
,  ;  .  ?  !  :    '  -
\end{verbatim}
\end{center}
and the spacebar, and the Return (or Enter) key.\\

There are thirteen special keys that are mostly used in \LaTeX \; instructions:
\begin{center}
\begin{verbatim}
#  $% & ~ _ ^ \ { } @ " | \end{verbatim} \end{center} If you need to use them in your document, there are commands available for typesetting these special characters. For example, \$ is typed as \verb+\$+, the underscore (\_) is typed as \verb+\_+, and \% is typed as \verb+\%+, whereas \"{a} is typed as \verb+\"{a}+, and @ is simply typed \verb+@+.\\ In a \LaTeX \; source file, each \emph{comment} line begins with \%. \LaTeX \; will ignore everything on the line after the \% character. \\ The \emph{document class}, declared by the command \verb+\documentclass{..}+, in a \LaTeX \; source file controls how the document will be formatted. \LaTeX, by default, fully justifies the text by placing a certain size space between words---the \emph{interword space}---and a somewhat larger space between sentences--the \emph{intersentence space}. To force an interword space, you can use the \verb+\+$_{\sqcup}$command (the$_{\sqcup}$symbol indicates a blank space). The \~ \, (tilde) command also forces an interword space, but with a difference: it keeps words together on the same line. It is called a tie'' or non-breakable space.''\\ When \LaTeX \; encounters a period, it must decide whether or not it indicates the end of a sentence. It uses the following rule: A period following a capital letter (e.g., A.) is interpreted as being part of an abbreviation or an initial and will be followed by an interword space; otherwise, it signifies the end of a sentence and will be followed by an intersentence space. If this rule causes problems in your document, you can follow the period with \verb+\+$_{\sqcup}$to force an interword space, or precede the period with \verb+\@+ to force an intersetence space.\\ In a \LaTeX \; document source file, left double quotes are typed a \verb+ + (two left single quotes) and right double quotes are type as \verb+' '+ (two right single quotes). The left single quote key is usually in the upper-left or upper-right corner of the keyboard, and shares a key with the tilde (\verb+~+) key.\\ In a \LaTeX \; command that requires an argument, the argument follows the name of the command and is placed between \{ and \}. Command names are \emph{case sensitive}. The command \verb+\\+ (\verb+\newline+ is another form) breaks a line. You can use the \verb+\\+ command and specify an appropriate amount of vertical space, for example \verb+\$1in]+. Note that this command uses \emph{square brackets} rather than braces because the argument is \emph{optional}. The distance/spacing may be given in points(pt), centimenters(cm), or inches(in). To force a page break, use \verb+\newpage+. \section{Typing Math} In addition to the keys listed above, you need the keys \verb+|, <+, and \verb+>+ to type mathematical formulas. (\verb+|+ is the shifted \verb+\+ key on many keyboards). \\ There are two kinds of math formulas and environments: \begin{enumerate} \item \emph{Inline math environments} open and close with \ or open with \verb+$$+ and close with \verb+$$+. \item \emph{Displayed math environments} open with \verb+\[+ and close with \verb+$+. Other forms of the displayed environment are \verb+\begin{equation*}$$...$$\end{equation*}+ and\\ \verb+$$...$$+. \end{enumerate} Within the math environment, \LaTeX uses its own spacing rules and completely ignores the number of white spaces typed with two exceptions: \begin{enumerate} \item Spaces that delimit commands (e.g., in \verb+$\infty a$+, the space is not ignored; in fact, \verb+\inftya$+ is
an error)
\item Spaces in the arguments of commands that temporarily revert to text mode (\verb+\mbox+ and \verb+\text+ are such commands).
\end{enumerate}
In text mode, many spaces equal one space; whereas, in math mode, spaces are ignored (unless they terminate a command). To asjust the spacing in a typeset document, use a spacing command. The same formula may be typeset differently depending on whether it is inline or display. For example, $\sum_{i=1}^{n} i^{2}$ is inline math.  The following is the same expression as displayed math
$\sum_{i=1}^{n} i^{2}.$
Math symbols are invoked by commands inside a math formula or environment. The math symbols are organized into tables in Appendix A of textbook. Some commands (e.g. \verb+\sqrt+) need arguments enclosed in braces (\{ and \}).  For example, to typeset $\sqrt{x^{2} y^{2}}$, type \verb+$\sqrt{x^{2} y^{2}}$+. To typeset $\sqrt[n]{x^{2} y^{2}}$, type \verb+$\sqrt[n]{x^{2} y^{2}}$+. Some commends need more than one arguments.  For example to typeset
$\frac{\sin x}{\cos^{2} x + \tan x}$
type
\begin{verbatim}
$\frac{\sin x}{\cos^{2} x + \tan x}$
\end{verbatim}
\verb+\frac+ is the command; $\sin x$ and $\cos^{2} x + \tan x$ are the arguments.\\

\begin{theorem}
This is the Pythagorean Theorem. It says
$x^{2}+y^{2}=z^{2}.$

\end{theorem}
\begin{definition}
Earth is where life is possible.
\end{definition}

\section{References}
Michael Downes \emph{Short Math Guide for \LaTeX}, AMS, 2002\\[0.2in]
George Gratzer, \emph{First Steps in \LaTeX}, Springer-Verlag, New York, 1999\\[0.2in]

\end{document}
`