# Typesetting issues with make4ht and tex4ht--kerning of math characters seems too close

I am really loving make4ht and tex4ht, as they finally allow me to keep all of my tooling while generating web pages. One minor issue I am running into has to do with the kerning of some characters while in math mode. So if I have a tikz image, I am getting things that look like this.

In the image below, you can see that the sigma character is too close to the frame of the vector, and that the underbrace is a little off from the array as well.

In the corresponding pdf output, these issues don't exist. Now I could just add some spaces or something as a workaround, but wanted to check and see if there is anything that I might be doing wrong or any simple fixes. Could this be some sort of CSS issue?

Note, I can supply additional examples of this if it would be helpful.

UPDATED

I am including a full example. This was the code that regenerates the issue.

First, here isthe command string that I used:

make4ht -d html -f html5+latexmk_build -s mydevfile.tex


Here is the myconfig.cfg

\Preamble{xhtml,mathjax}
\Configure{@BODY}{\IgnorePar
\HCode{\detokenize{$$}} \special{t4ht*<mymacros.tex} \HCode{\detokenize{$$}}
\par
}
\begin{document}
\EndPreamble


The mymacros.tex file:

\def\RRRR{{\bf R}}
\newcommand\bface[1]{{\mathbf #1}}
\newcommand\grid[3]{
\begin{tikzpicture}[baseline=(current bounding box)]
\foreach \x in {1,...,#2} {
\foreach \y in {1,...,#1} {
\draw[fill=#3, color=#3] ($(0.4*\x, 0.4*\y)$) circle[radius=0.15cm];
}
}
\draw (0.2, 0.2) rectangle ($(0.4*#2, 0.4*#1) + (0.2, 0.2)$);
\end{tikzpicture}
}
\renewcommand{\vec}[1]{\mathbf{#1}}


The mymacros.sty file:

\ProvidesPackage{mymacros}
\input{mymacros.tex}
\endinput


Finally, here is a minimal latex document, mydevfile.tex:

\documentclass[12pt, a4paper, oneside, headinclude, footinclude]{article}

\ifdefined\HCode
\def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi

\usepackage{tikz}
\usetikzlibrary{shapes.multipart, positioning, decorations.markings,
arrows.meta, calc, fit}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{mymacros}

\title{\normalfont\spacedallcaps{My doc}}
\author{\spacedlowsmallcaps{kb}}
\date{\today\\version 0.1}
\begin{document}

\maketitle
\setcounter{tocdepth}{2}
\tableofcontents

\section*{First Section}

$\RRRR = \bface{x}$
Some simple text.

$\underbrace{\grid{10}{1}{red}}_{\mathbf{\hat{x}}} = \sigma \left( \underbrace{\grid{10}{5}{blue}}_{\mathbf{u}^\top} \sigma \left( \underbrace{\grid{5}{2}{blue}}_{\mathbf{w}^\top} \sigma \left(\underbrace{\grid{2}{5}{blue}}_{\mathbf{U}} \sigma\left( \underbrace{\grid{5}{10}{blue}}_{\mathbf{V}} \underbrace{\grid{10}{1}{red}}_{x}\right)\right) \right) \right)$
\end{document}


Now the picture looks like:

• can you please post a full MWE, so we can test it? Mar 27 at 22:36
• @michal.h21 yep, I was able to regenerate the exact issue. I am still getting these weird kerning errors. Hopefully you can reproduce them. Let me know if anything is ambiguous about the code. Mar 28 at 5:23

It seems that you want to combine two methods for displaying math: I suppose that you want to use MathJax for inline math, and SVG pictures for display math. The problem is that most math environments and commands are redefined to output the literal LaTeX code that is passed to them in the MathJax mode. This means that your \grid command which uses TikZ cannot work, as MathJax doesn't support TikZ.

To configure $...$ to produce pictures, you can try the following configuration file:

\Preamble{xhtml,mathjax}
% don't redefine \left and \right
\def\fixmathjaxsec#1{}
\Configure{[]}
{\bgroup%
\protect\csname nested:math\endcsname%
\PicDisplay%
$$\everymath{}\everydisplay{}} {$$\EndPicDisplay \egroup \ShowPar \par{\HCondtrue\noindent}%
}

\begin{document}
\EndPreamble


I've used the \Configure{[]} command from the default HTML configuration. The important commands are \PicDisplay and \EndPicDisplay, which convert the contained content to images. Rest of the commands handle paragraphs and math nesting. One important thing is \def\fixmathjaxsec#1{}. The original version of this command redefines command that is passed to it to produce literal output. It is used for \left and \right commands, because they caused some issues when used in sections. We need to prevent that. Just remember that you shouldn't use \left and \right in sections, because you will get an error otherwise.

This is the result:

• Thanks so much @Michal.h21. This code works. I still get some overlap between the sigma and the brackets, but I can perhaps just add some spaces. In terms of your comment about SVG for display math and Mathjax for inline math, I was just wondering if I am doing it wrong, or if I not following what most other folks do? I was literally just hoping to include the Tikz and Pgf diagrams in the latex export to html. So whatever way works best for this, I am happy to follow. I was not tryign to do anything non-standard. Mar 28 at 15:09
• @krishnab no you are not doing anything wrong, there is just the issue that mathjax doesn't support TikZ, so you have to use some workarounds, and it is possible that you will experience some strange bugs. Mar 28 at 15:27
• Okay that is reassuring :). Yes, I get what you mean now. I mean, I am impressed at how well make4ht, tex4ht, and pandoc all work, given the different processing engines, etc. So I really appreciate all of the efforts that you and everyone invests in making this ecosystem run. I guess once I get more of a feel for the html stuff, I will figure out the workarounds. Thanks again for all of your help. Mar 28 at 15:31