4

Here is a sample from a LaTeX document I want to convert to HTML.

\documentclass[11pt]{book}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{amscd}

\begin{document}

 Some math...\newline
 Scalars: regular math font $a, x, X, \alpha, \beta, \Theta, \ldots$ \newline
 Vectors: bold math font $\boldsymbol{a}, \boldsymbol{x}, \boldsymbol{X}, \boldsymbol{\alpha}, \boldsymbol{\beta}, \boldsymbol{\Theta}, \ldots$ \newline

\end{document}

This is the PDF output. enter image description here

This is the .cfg file I'm using to convert.

% bold-math.cfg

\usepackage[T1]{fontenc}

% We are generating HTML + MathML code
\Preamble{xhtml,mathml,3,charset=utf-8,notoc*,nominitoc,fonts}

% Don't output xml version tag
\Configure{VERSION}{}

% Output HTML5 doctype instead of the default for HTML4
\Configure{DOCTYPE}{\HCode{<!doctype html>\Hnewline}}

% Custom page opening
\Configure{HTML}{\HCode{<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\Hnewline}}{\HCode{\Hnewline</html>}}

% Reset <head>, aka delete all default boilerplate
\Configure{@HEAD}{}

% Setup custom <head> content
\Configure{@HEAD}{\HCode{<meta charset="utf-8" />\Hnewline}}
\Configure{@HEAD}{\HCode{<meta name="generator" content="TeX4ht (http://www.cse.ohio-state.edu/\string~gurari/TeX4ht/)" />\Hnewline}}

% Add css files
\Configure{@HEAD}{\HCode{<link rel="stylesheet" type="text/css" href="\expandafter\csname aa:CssFile\endcsname" />\Hnewline}}
\Configure{@HEAD}{\HCode{<link rel="stylesheet" href="../Serif/cmun-serif.css" type="text/css" />\Hnewline}}
\Configure{@HEAD}{\HCode{<link rel="stylesheet" href="bold-math-mi.css" type="text/css" />\Hnewline}}

% Setup MathJax
\Configure{@HEAD}{\HCode{<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=MML_CHTML"></script>\Hnewline}}
%\Configure{@HEAD}{\HCode{<script type="text/javascript" src="../MathJax/MathJax.js?config=MML_CHTML"></script>\Hnewline}}

% Translate \textbf, \textit and \texttt directives into <b>, <em> and <code>
\Configure{emph}{\ifvmode\ShowPar\fi\HCode{<em>}}{\HCode{</em>}}
\Configure{textbf}{\ifvmode\ShowPar\fi\HCode{<b>}}{\HCode{</b>}}
\Configure{textit}{\ifvmode\ShowPar\fi\HCode{<i>}}{\HCode{</i>}}
\Configure{texttt}{\ifvmode\ShowPar\fi\HCode{<code>}}{\HCode{</code>}}

% Translate verbatim and lstlisting blocks into <pre> elements
\ConfigureEnv{verbatim}{\HCode{<pre>}}{\HCode{</pre>}}{}{}
\ConfigureEnv{lstlisting}{\HCode{<pre>}}{\HCode{</pre>}}{}{}

% Do not set `indent`/`noindent` classes on paragraphs
\Configure{HtmlPar}
{\EndP\Tg<p>}
{\EndP\Tg<p>}
{\HCode{</p>\Hnewline}}
{\HCode{</p>\Hnewline}}

\begin{document}

\EndPreamble

This is the command I use to produce the HTML file.

htlatex bold-math.tex "bold-math" " -cunihtf -utf8" " -cvalidate"

This is the HTML I get. enter image description here

As you can see, the problem is that the \boldsymbol is ignored in the output and that the capitol \Theta is in italics, rather the in normal font. After some tweaking to the MathML code in the HTML output, I can get the following. enter image description here

These are a couple of tweaks I made, for reference.
From:

<mi>a</mi>
<mi>Θ</mi>

To:

<mi mathvariant="bold-italic">a</mi>
<mi mathvariant="bold">Θ</mi>

This shows that MathJax and MathML are capable to show what I want. I assume the problem resides in tex4ht.

Does anyone know how to fix these issues?

  • 1
    \bm (needs \usepackage{bm}) is recommended as a better alternative to \boldsymbol. does that work? – barbara beeton Apr 6 '16 at 16:25
  • I've read about it, but the source latex code is very messy and adding that package (that redefines the \boldsymbol command) breaks the compilation, both PDF and HTML. The source is written by many people and most of them use a GUI latex editor that inserts bad things like many nested boldsymbol, e.g. \boldsymbol{\boldsymbol{\boldsymbol{a}}}. These nested things make bm infinitely loop until the max memory is reached. But bm doesn't complain about things like \boldsymbol{a_{\boldsymbol{b}}. More over, bm doesn't work well with simple \boldsymbol{a}^{2} for the HTML conversion. – Matteo Ipri Apr 8 '16 at 15:01
7

Most straightforward way to get the bold symbols is to redefine \boldsymbols command to include tags which you want. Put this command to your .cfg file:

\renewcommand\boldsymbol[1]{\HCode{<mi mathvariant="bold-italic">}\PauseMathClass #1\EndPauseMathClass\HCode{</mi>}}

\PauseMathClass will prevent tex4ht from including tags based on font, you would get

<mi mathvariant="bold-italic"><mi 
>x</mi></mi>

otherwise. The resulting mathml:

<mi mathvariant="bold-italic">a</mi><mo 
class="MathClass-punc">,</mo><mi mathvariant="bold-italic">x</mi><mo 
class="MathClass-punc">,</mo><mi mathvariant="bold-italic">X</mi><mo 
class="MathClass-punc">,</mo><mi mathvariant="bold-italic">α</mi><mo 
class="MathClass-punc">,</mo><mi mathvariant="bold-italic">β</mi><mo 
class="MathClass-punc">,</mo><mi mathvariant="bold-italic">Θ</mi><mo 
class="MathClass-punc">,</mo><mo 
class="MathClass-op">…</mo>

The other way is to use bm package, as Barbara suggested. It seems that it also redefines \boldsymbol and it is supported by tex4ht, with one caveat:

enter image description here

<mstyle mathvariant="bold"><mi 
>a</mi></mstyle><mo 
class="MathClass-punc">,</mo><mstyle mathvariant="bold"><mi 
>x</mi></mstyle><mo 
class="MathClass-punc">,</mo><mstyle mathvariant="bold"><mi 
>X</mi></mstyle><mo 
class="MathClass-punc">,</mo><mstyle mathvariant="bold"><mi 
>α</mi></mstyle><mo 
class="MathClass-punc">,</mo><mstyle mathvariant="bold"><mi 
>β</mi></mstyle><mo 
class="MathClass-punc">,</mo><mstyle mathvariant="bold"><mi 
>Θ</mi></mstyle><mo 
class="MathClass-punc">,</mo><mo 
class="MathClass-op">…</mo></math>

the problem is that it seems that only bold is selected, not italic style, although there is <mi> element as <mstyle> child. I am not sure whether it is only bug in Firefox rendering, but you can easily replace these elements with your desired output using some simple regular expresssion in make4ht build file:

-- sample.mk4
local filter = require "make4ht-filter"

local htmlmatch = filter {
  function(text)
    return text:gsub('<mstyle mathvariant="bold">%s*<mi%s*>([^%<]+)</mi></mstyle>', function(symbol)
      return string.format('<mi mathvariant="bold-italic">%s</mi>', symbol)
    end)
  end
}
if mode=="draft" then
  Make:htlatex {}
else
  Make:htlatex {}
  Make:htlatex {}
  Make:htlatex {}
end
Make:match("html$", htmlmatch)

The regular expression is included in htmlmatch function. Compile with:

make4ht -uc bold-math.cfg -e sample.mk4 bold-math.tex

the resulting mathml:

<mi mathvariant="bold-italic">a</mi><mo 
class="MathClass-punc">,</mo><mi mathvariant="bold-italic">x</mi><mo 
class="MathClass-punc">,</mo><mi mathvariant="bold-italic">X</mi><mo 
class="MathClass-punc">,</mo><mi mathvariant="bold-italic">α</mi><mo 
class="MathClass-punc">,</mo><mi mathvariant="bold-italic">β</mi><mo 
class="MathClass-punc">,</mo><mi mathvariant="bold-italic">Θ</mi><mo 
class="MathClass-punc">,</mo><mo 
class="MathClass-op">…</mo>

and Firefox rendering:

enter image description here

| improve this answer | |
  • Thanks! I tried the first solution, to redefine the \boldsymbol command, and it works. I still have the problems with superscripts and subscripts. But I believe the problem is inside tex4ht and not inside the bm package or your solutions. For example $\boldsymbol{\sigma}_{a}^{2} gets converted to <mi mathvariant="bold-italic"><msubsup><mrow>σ</mi></mrow><mrow><mi>a</mi></mrow><mrow><mn>2</mn></mrow></msubsup> where you can see that the mi element is open too early, before msubsup and mrow. In fact, placing the opening mi element right before the sigma, fixes the MathJax display – Matteo Ipri Apr 8 '16 at 15:38
  • One of a great explanation, thanks to both... – MadyYuvi Jan 3 at 10:25
  • @MadyYuvi I totally forgot about this answer. I've recently added a support for \boldsymbol to TeX4ht, so the solution from this answer will be unnecessary sooon – michal.h21 Jan 3 at 10:34
  • @michal.h21 Thanks for the update.... – MadyYuvi Jan 3 at 10:35

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.