TransWikia.com

how can I get bold symbols using boldsymbol and tex4ht with MathML output?

TeX - LaTeX Asked by Matteo Ipri on March 8, 2021

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="expandaftercsname aa:CssFileendcsname" />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}{ifvmodeShowParfiHCode{<em>}}{HCode{</em>}}
Configure{textbf}{ifvmodeShowParfiHCode{<b>}}{HCode{</b>}}
Configure{textit}{ifvmodeShowParfiHCode{<i>}}{HCode{</i>}}
Configure{texttt}{ifvmodeShowParfiHCode{<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}
{EndPTg<p>}
{EndPTg<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?

2 Answers

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:

renewcommandboldsymbol[1]{HCode{<mi mathvariant="bold-italic">}PauseMathClass #1EndPauseMathClassHCode{</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

Correct answer by michal.h21 on March 8, 2021

@Matteo Ipri To workaround the unbalanced tags generated by tex4ht while super-scripting or sub-scripting the bold symbol, enclose boldsymbol{sigma} with braces. This will produce rightly balanced tags.

Instead of $boldsymbol{sigma}_{a}^{2}$ use ${boldsymbol{sigma}}_{a}^{2}$.

Answered by Niranjana_KM on March 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP