5

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"

Mathematica graphics

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"

Mathematica graphics

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

Mathematica graphics

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' 
 xmlns:xlink='http://www.w3.org/1999/xlink' width='160.406143pt' 
 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

Nasser
  • 20,220

1 Answers1

5

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.

ht4latex sample

The HTML file it generates looks like this:

<!DOCTYPE html> 
<html lang="en-US" xml:lang="en-US" > 
<head><title></title> 
<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"> 
</head><body 
>
   <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.

Latin Modern Math sample

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}
Davislor
  • 44,045
  • 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 '19 at 06: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 '19 at 06:38
  • @Nasser That sample produces a SVG file that you can embed in a HTML document Let me take another look. – Davislor May 23 '19 at 06: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 '19 at 06:40
  • Thank you very much. It does look better now! I need to try it on large file. But I wanted to use make4ht, so I used the options you used and now used this command make4ht -ulm default foo.tex "xhtml,pic-align,notoc*,p-width,charset=utf-8,svg,dvisvgm_hashes" "-cmozhtf -utf8" which seems to produce same output as your command using htlatex. I had no idea I was not using the best options all this time ! – Nasser May 23 '19 at 08:13
  • @Nasser A friend of mine who knows more about tex4ht than I do just woke up, so I’ll show her this. It’s possible I didn’t give you the best options either. – Davislor May 23 '19 at 08:18
  • 2
    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 '19 at 09:31
  • @michal.h21 I tried make4ht first, and t4ht crashes in TeX Live 2019. Error message: ...make4ht-dvireader.lua:106: attempt to call a number value (local 'op') – Davislor May 23 '19 at 19:35
  • @michal.h21 I considered writing a build file, but the OP asked for a command line. For now, I removed the superfluous option; the command works without it. – Davislor May 23 '19 at 19:40
  • dvisvgm gets called by htlatex, the dvisvgm_hashes extension is a more efficient version, as it executes it only on the changed pictures. this is important, because Nasser has files with tens of thousands pictures, so it can save lot of time needed for the compilation – michal.h21 May 23 '19 at 19:46
  • I am not on TL 2019 now, but your the code sample compiles correctly for me on TL 2018 – michal.h21 May 23 '19 at 19:49
  • @michal.h21 So is this a bug that should be reported? Or is there a t4ht option I’m supposed to use? – Davislor May 23 '19 at 19:50
  • @michal.h21 I tried several variants, but I get it with the one you suggested: make4ht -ul -f html5+dvisvgm_hashes foo.tex "htm,svg" and also when I replace -ul with -u or -ux, or remove it entirely. – Davislor May 23 '19 at 19:54
  • That looks correct. I can only think that this may be caused by using Lua 5.3 instead of Lua 5.2 in TL 2019. – michal.h21 May 23 '19 at 19:57
  • @michal.h21 I’ll add your command to my post with a remark that it’s been confirmed to work on TL 2018, but is currently broken on TL 2019. – Davislor May 23 '19 at 19:58
  • I've just installed TL 2019 and I cannot reproduce this issue :/ – michal.h21 May 23 '19 at 20:44
  • @michal.h21 Huh. I tested on Windows 10. On Ubuntu, I’m not running 2019 yet. – Davislor May 23 '19 at 20:48
  • ah, I found the issue, it was necessary to read the DVI file in the binary mode. it worked on Linux, but not on Windows. It should be fixed in the development make4ht version – michal.h21 May 24 '19 at 09:41
  • @michal.h21 Thank you! Glad I could help find and reproduce the bug. – Davislor May 24 '19 at 09:45