# how to improve SVG for math resolution in tex4ht?

make4ht now supports both mathjax and svg for math.

I noticed that the math displayed in the browser when using mathjax mode looks sharper/crisper than when using svg for math.

So this question is asking how to improve the svg math resolution, and what configuration if any are needed. I'd like to use SVG for math, but this difference is one of the reasons I am compiling to mathjax with tex4ht.

To show this difference, here is a MWE and commands used to compile them so you can see for yourself on your browser what I mean.

\documentclass[11pt]{article}
\usepackage{amsmath}
\begin{document}

$\arctan\left( \frac{2 \alpha (a y+b x)+\beta }{a \alpha \sqrt{\frac{4 a \alpha \gamma -a \beta ^2+4 \alpha b c}{a^3 \alpha ^2}}} \right)$
\end{document}


This is how it looks in Chrome browser compiled to mathjax using the following command (all one one line)

make4ht -ulm default foo.tex
"htm,mathjax,notoc*,p-width,charset=utf-8" " -cunihtf -utf8"


And this is how it looks like compiled using the following command (all on one line)

make4ht -ulm default -f html5+dvisvgm_hashes foo.tex
"html,pic-align,notoc*,p-width,svg"


Actually in the above I had to zoom in using the browser to 150% and more to take these screen shots. At 100% magnification in the browser, one can see more clearly that the mathjax math looks sharper and more crisp and easier to read than the svg math. The svg math is more light weight also. If I can make it darker like mathjax, then that should really help. But do not know how to do this.

I know how to increase the size of the SVG math generated now. Which does help a little.

Here is a screen shot taking when the browser is 100% for both cases, side by side which might make it easier to see the difference

fyi, this is the header of the SVG image file for the above equation that is generated by dvisvgm

<?xml version='1.0' encoding='UTF-8'?>
<!-- This file was generated by dvisvgm 2.6.3 -->
<svg version='1.1' xmlns='http://www.w3.org/2000/svg'
height='47.409423pt' viewBox='124.191939 126.956513 160.406143 47.409423'>


my question is: Can SVG math generated by tex4ht be made to look better than it does now?

Using TL 2019

You can change the fonts, or the TeX engine, or the image converter. For this example, I’ll change the fonts to Latin Modern and tweak your command line. In particular, make sure you’re passing in both the svg option to generate a vector graphic and the dvisvgm_hashes option to do it with dvisvgm.

You appear to have been generating a raster image, rather than a vector one. I genuinely don’t know which other option you passed in switched to PNG output.

## With make4ht or htlatex

I added a few commands to your MCVE to change the font to the Type 1 version of Latin Modern, which is closer to what the MathJax output uses:

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage[T1]{fontenc}
\usepackage{lmodern, textcomp}
\begin{document}

$\arctan\left( \frac{2 \alpha (a y+b x)+\beta }{a \alpha \sqrt{\frac{4 a \alpha \gamma -a \beta ^2+4 \alpha b c}{a^3 \alpha ^2}}} \right)$
\end{document}


(You probably can remove textcomp.)

The following command line should work for you:

make4ht -ul -f html5+dvisvgm_hashes foo.tex "htm,svg"


When I originally posted, I ran into a bug on TeX Live 2019 in Windows 10. It should be fixed in the next update, thanks to @michal.h21! If you need a temporary, sub-optimal workaround:

htlatex sx-svgout.tex "html,html5,htm,charset=utf-8,svg" "-cmozhtf -utf8"


You probably want to tweak it further.

This produces a graphic more like the one you wanted. Here is a PNG conversion that I can upload to this site. You might need to open the image in a new tab to see it at full size.

The HTML file it generates looks like this:

<!DOCTYPE html>
<html lang="en-US" xml:lang="en-US" >
<meta  charset="utf-8" />
<meta name="generator" content="TeX4ht (http://www.tug.org/tex4ht/)" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" type="text/css" href="sx-svgout.css" />
<meta name="src" content="sx-svgout.tex">
>
<center class="par-math-display" >
<img
src="sx0x.svg" alt="      (                  )
2α(ay + bx)+ β
arctan ( --∘--------2-----)
aα  4aαγ-aa3βα2+4αbc
" class="par-math-display" ></center>
<!--l. 12--><p class="nopar" >
</body>
</html>


You could alternatively produce MathML, but you say you need a HTM file, not XHTML.

## Original Answer: Producing SVG Output

I slightly modified your example to use the same OpenType font as MathJax, and to compile as a standalone image without cropping:

\documentclass[11pt, varwidth]{standalone}
\usepackage{amsmath}
\usepackage{unicode-math}
\begin{document}

$\arctan\left( \frac{2 \alpha (a y+b x)+\beta }{a \alpha \sqrt{\frac{4 a \alpha \gamma -a \beta ^2+4 \alpha b c}{a^3 \alpha ^2}}} \right)$
\end{document}


Compiling this to PDF using LuaLaTeX, then converting from PDF to SVG using pdf2cairo -svg, gives a SVG that looks much more like your Inkscape example.

Here is a conversion of that SVG to a raster image that I can post to TeX.SX. You might need to open it in a new tab to see it at full size.

If you need DVI output or backwards compatibility with NFSS, you can still change the fonts. Replace \usepackage{unicode-math} with the commands

\usepackage[T1]{fontenc}
\usepackage{lmodern, textcomp}

• Thanks. But I do not understand something. I need to use make4ht to compile the latex to HTML. So what command did you use? This is just one small example. I have files with thousands of math equations in them. So the command you show pdf2cairo -svg, how is this going to be used by make4ht? I mean the solution needs to be part of the tool chain used by make4ht. I can't do this outside of this framework. Sorry if I am missing the point of this. – Nasser May 23 at 6:36
• If you mean that pdf2cairo -svg will produce better svg than dvisvgm currently used by make4ht, then great. But how to make make4ht then use pdf2cairo? – Nasser May 23 at 6:38
• @Nasser That sample produces a SVG file that you can embed in a HTML document Let me take another look. – Davislor May 23 at 6:38
• Yes, I understand. But I can't manually do these things image by image. The process has to be part of make4ht setup so that it is automated. – Nasser May 23 at 6:40
• the dvisvgm_hashes extension cannot work with htlatex, make4ht is necessary. the correct command line call should be something like: make4ht -ul -f html5+dvisvgm_hashes foo.tex "htm,svg" (not tested) – michal.h21 May 23 at 9:31