# Can I use TikZ (instead of PSTricks) for latex2html5?

I found this today:

http://latex2html5.com/

Author interactive math equations and diagrams online using LATEX and PSTricks

It looks nice, but I'm more familiar with TikZ. Is anybody aware of something equivalent which is not based on PSTricks but TikZ?

• you can use tex4ht for this job - tex.stackexchange.com/q/35145/2891 for tex4ht + tikz, albany.edu/~hammond/demos/Html5/arXiv for tex4ht + mathjax + html5 Commented Nov 18, 2013 at 10:03
• You need to know that not all PSTricks constructs are currently supported. The author probably chose PSTricks because it is much less verbose compared to TikZ. Parsing TikZ is much more complicated. Commented Nov 18, 2013 at 14:29
• @michal.h21 Do you mind cooking up the simplest example with tex4ht? Commented Mar 14, 2014 at 19:03
• @percusse not really the simples example, because I added also other features, but I added something :) Commented Mar 17, 2014 at 15:08

There is simple example which shows usage of tikz-to-svg, mathjax and webfonts with tex4ht.

First some sample document:

\documentclass[]{article}
\usepackage{tikz, myexternalize, myfontspec, mathtools}
\setmainfont{STIXGeneral}
\setmathfont{XITS Math}
\begin{document}

First, show me some inline math:
$$\forall x \in X, \quad \exists y \leq \epsilon$$

Now some display math:

$\frac{n!}{k!(n-k)!} = \binom{n}{k}$

And finally, some tikz picture, source is some answer on TeX.sx:

\begin{tikzpicture}
% Time axis
\draw (0,0) -- node[below=1cm] {Time $\to$} (6,0);
% Emotional axis
\draw (0,0) -- node[left] {\parbox{2cm}{\centering $\uparrow$ \\ Emotional \\ intensity}} (0,4);
% Time ticks
\foreach \x [count=\j] in {0.2,3,4.5,6} {
\draw (\x,0) coordinate (t\j) -- (\x,-0.1cm) node[below] (tt\j) {Time \j};
}
\node[below] at (tt1.south) {(Emotional event)};
% Curves
\draw (t1) .. controls +(1,2) .. node[above right] {Experienced} (t2);
\draw[dashed] (t1) .. controls +(1,4) .. node[above right] {Predicted} (t4);
\end{tikzpicture}

Some samples \textit{of font} \textbf{changes} and diacritics: Příliš žluťoučký kůň úpěl ďábelské ódy.
\end{document}


Two custom packages are used, myexternalize and myfontspec. These packages tries to resolve issues of fontspec and tikz-externalize with tex4ht.

Now we should show these packages. myexternalize.sty:

\usetikzlibrary{external}
\tikzset{
tex4ht inc/.style={
/pgf/images/include external/.code={%
\includegraphics[]{##1.svg}%
}

}
}

\tikzexternalize[mode=only graphics]
\tikzset{tex4ht inc}
}{
\tikzset{external/system call={lualatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}}
\tikzset{
{; inkscape -z -f "\image.pdf" -l "\image.svg"
}
}
\tikzexternalize
}


this package uses lualatex to convert tikz pictures to pdf and inkscape to convert these pdf to svg. Some details can be found here and here. You also need file myexternalize.4ht, which configures tex4ht to include svg images:

\Configure{graphics*}
{svg}{
{\Configure{Needs}{File: \[email protected]}\Needs{}}
\Picture[\csname a:GraphicsAlt\endcsname]{\csname Gin@base\endcsname.svg \csname a:Gin-dim\endcsname}%
}


Now we need to handle fonts, with package myfontspec.sty:

\@ifpackageloaded{tex4ht}{
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\newcommand\setmainfont[1]{}
\newcommand\setmathfont[1]{}
}{
\usepackage{fontspec}
\usepackage{unicode-math}
}


This file only loads inputenc and fontenc packages and defines some dummy commands when compiled with tex4ht, fontspec is used otherwise. Now you can compile your document to get svg images:

lualatex -shell-escape filename


Now we need to provide some configurations for mathjax and webfonts for tex4ht. File myconfig.cfg:

\Preamble{xhtml, mathml}
% Configure for mathjax
\Configure{VERSION}{}
\Configure{DOCTYPE}{\HCode{<!DOCTYPE html>\Hnewline}}
\Configure{HTML}{\HCode{<html>\Hnewline}}{\HCode{\Hnewline</html>}}
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"\Hnewline
></script>\Hnewline}}
\begin{document}
\Css{@font-face {
font-family: #1;
src: local("#2"),
url('#3');
#4
}}
\special{t4ht+@File: #3}
}
\edef\CurrentFontFamily{rmfamily}
\newcommand\SetFontFamily[1]{
\edef\CurrentFontFamily{#1}
}

\NormalFont{STIXDefault}{STIXGeneral-Regular.woff}
\BoldFont{STIXDefault}{STIXGeneral-Bold.woff}
\ItalicFont{STIXDefault}{STIXGeneral-Italic.woff}
\BoldItalicFont{STIXDefault}{STIXGeneral-BoldItalic.woff}
\Css{body{font-family:rmfamily, "STIXDefault", sans-serif;}}
\EndPreamble


There are definitions for mathjax before \begin{document}, after that, definitions for fonts follow. I created new command \AddFontFace which add font definition to the css file, and number of commands for including files for different font weights and styles. Stix fonts are used, because these fonts are used by mathjax, so it looks good when they are used also for text. You need to copy used .woff files from zip file provided on Sourceforge.

Now we can compile the document with:

htlatex filename "myconfig, mathml, charset=utf-8" " -cunihtf -utf8"


Resulting pdf:

and html:

you can also see the html page in action.