Consider the following simple latex file:


% see https://tex.stackexchange.com/questions/386757/work-around-for-bug-in-pgf-when-used-with-htlatex
% Fix due to Michal Hoftich
\input pgfsys-tex4ht.def

         \foreach \x in {-3,...,3} {
            \draw(\x,0.25) --(\x,0)node[below]{$\x$};
          \foreach \x in {-2.5,...,2.5} {
            \draw(\x,0.18) --(\x,0);
          \filldraw[blue](1.5,0) circle (1mm);

-3 -2 -1 0 1 2 3


When run through pdflatex this produces the pdf image:

enter image description here

When run through make4ht this produces essentially the same html/svg image:

enter image description here

Unfortunately, this goes pear shaped when we run through the MWE through make4ht -u, using the utf-8 option, as we get:

enter image description here

The first two images are correct but there is an encoding/svg issue when using make4ht -u with the negative numbers appearing in the tikz code; more accurately, there is a problem with the minus sign. My real application uses utf-8 so dropping the -u switch is not really an option for me.

Any idea how to fix the last image when using make4ht -u?

I would think that you get the same error using a utf-8 encoding with htlatex but I prefer to use make4ht because it has a much nicer syntax.

[The use of the filecontents package in the MWE is to correct a bug in PGF that prevents tikz from playing nicely with tex4ht; see Work around for bug in pgf when used with htlatex? for details. Run pdflatex on the MWE first otherwise make4ht will give an error because the file pgfsys-tex4ht-alt.def won't have been created yet.]

  • 1
    btw, have you seen another question from this weekend: tex.stackexchange.com/a/390594/2891 ? it seems that TikZ /pgf/tex4ht node/escape=true solves some problems with formatted text in nodes. – michal.h21 Sep 11 '17 at 6:43
  • @michal.h21 Thanks@ I hadn't seen this. – Andrew Sep 11 '17 at 16:08

The problem is that pgfsys-tex4ht.def sets the encoding of the SVG file to iso-8859-1:

 \pgfsys@invoke{<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>

This definition is hardwired in this file, so it cannot be really changed from the outside, we need to edit pgfsys-tex4ht.def directly.

We can use the following macros to get value of output encoding used by tex4ht:

\def\htmlencoding{\expandafter\getEncoding\Preamble ,charset=,\@nil}

It uses Latin1 by default, but it will detect utf-8 with -u option of make4ht.

This is the full pgfsys-tex4ht.def:

% Copyright 2006 by Till Tantau
% This file may be distributed and/or modified
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\ProvidesFileRCS $Header: /cvsroot/pgf/pgf/generic/pgf/systemlayer/pgfsys-tex4ht.def,v 1.25 2013/08/06 17:46:08 tantau Exp $

% Driver commands for tex4ht

% Load common pdf commands:
\input pgfsys-common-svg.def

% tex4ht-specific stuff:
\def\pgfsys@invoke#1{\pgf@sys@fail{svg code in preamble}}



%% initial support of Mathml and xhtml inside svg through the svg:foreignelement tag
%% it'll allow us to have complicated text nodes in the tex4ht driver

%Example  (to improve the display, you have to use css)
%\tikzpicture[svg text only=false]
%\draw (0,0) node[inner sep=0pt,draw] {tight}
%(0cm,2em) node[inner sep=5pt,draw] {loose}
%(0cm,4em) node[fill=red!20] {default};
%\draw (3,0) node[inner sep=0pt,draw] {TIGHT}
%(3cm,2em) node[inner sep=5pt,draw] {LOOSE}
%(3cm,4em) node[fill=red!20] {DEFAULT};
%\draw (6,0) node[inner sep=0pt,draw] {$tight$}
%(6cm,2em) node[inner sep=5pt,draw] {$loose$}
%(6cm,4em) node[fill=red!20] {$default$};
%\draw (9,0) node[inner sep=0pt,draw] {$TIGHT$}
%(9cm,2em) node[inner sep=5pt,draw] {$LOOSE$}
%(9cm,4em) node[fill=red!20] {$Default$};
% you can use the options "svg text css=filename", "svg text id=idname", "svg text class=classname" to customize the display through the use of css

% % \Configure{[]}{$}{$}   these two lines aren't needed for tex4ht and moreother they produce errors with plain tex !
% % \Configure{()}{$}{$}
% \Configure{$$}{}{}{}%$$
% \Configure{$}{}{}{}%$
%  \Configure{SUB}{\HCode{<tspan baseline-shift="sub">}}{\HCode{</tspan>}}
%  \Configure{SUP}{\HCode{<tspan baseline-shift="sub">}}{\HCode{</tspan>}}
%  \Configure{SUBSUP}{}{}{}
%  \Configure{htf} 
%    {0}{+}{<tspan font-family="}
%    {\%s}{" font-size="\%s}{}{}
%    {">}{</tspan>}

%  % undo:
%  \Configure{htf}{0}{+}{<span\Hnewline
%     class="}{\%s}{-\%s}{x-x-\%d}{}{">}{</span>}


% we don't want tex4ht putting <p> and </p> tags automatically in our svg code and causing invalid xhtml in our svg pictures
  \pgfkeys{/pgf/tex4ht node/escape=false}%
  % text only nodes are the default. Advantage : pure svg pictures are
  % the default, nicer for text only nodes.  
  % Disadvantages : this rendering methods produce more invalid or degraded svg pictures than with the foreignelement tag
  %% we use css to control the display of foreignelement rendered text nodes
      .foreignobject {%
        font-size:120\%; % this is the trick....a font of size 100% in
                         % a box scaled down to half size makes it a
                         % half size font. 
                         % you'll have to double the size of things
                         % that you want to have a regular size in
                         % this half-scaled box though  
        % can't wait for the stix font...the cmr10, etc font are
        % available from the mozilla site, times and symbols should be
        % okey  
        % background-color:\#12ff55;  usefull for debugging positioning
        text-align:center; % good for centered text nodes...will have to improve that later on...with more integration with pgf options for text nodes
      math {%, math[mode="inline"] {
        font-family:STIXGeneral,Times,Symbol, cmr10,cmsy10,cmex10,cmmi10;
        font-style: normal;
        % background-color:\#99ff99;

  \par% FIXME : was '\Par' but that seems to be undefined!?

\def\htmlencoding{\expandafter\getEncoding\Preamble ,charset=,\@nil}
% this is okay for both rendering methods for text nodes (mtext or foreignelement)...
  \global\advance\pgf@sys@svg@picnum by 1\relax%
  % Ok, compute width/height
  \advance\pgf@x by-\pgf@xa%
  \advance\pgf@y by-\pgf@ya%
  \HCode{<object data="\jobname-\the\pgf@sys@svg@picnum.svg" width="}%
  % Calculate pixel coordinates (this does not really make sense, but
  % we have to do it anyway). Assume 96dpi.
  % Add one pixel for security:
  \advance\pgf@x by1pt%
  \advance\pgf@y by1pt%
  \pgfsys@invoke{" height="}%
  \pgfsys@invoke{" type="image/svg+xml"><p>SVG-Viewer needed.</p></object>}%
  %%%%%%%% This is necessary to control the svg through css
    /pgf/tex4ht node/css/.get=\pgfsys@foreignobject@css
  \pgfsys@invoke{<?xml version="1.0" encoding="\htmlencoding" standalone="no"?>\Hnewline% added that
  <?xml-stylesheet href="\pgfsys@foreignobject@css.css" type="text/css"?>\Hnewline}% added that 
%%%%%%%%%%%%%%% maybee I should add an option to change the css, for the whole picture
  \pgfsys@invoke{<svg xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink" width="\the\pgf@xc"
    height="\the\pgf@yc" viewBox="}%
  \pgfsys@invoke{">\Hnewline  <g transform="translate(0,}
  \pgfsys@invoke{) scale(1,-1) translate(0,}%
  \pgfsys@invoke{)">\Hnewline }%
  \advance\pgf@ya by-\pgf@picminy\relax%
  \advance\pgf@picmaxy by-\pgf@picminy\relax% maxy is now the height
  \advance\pgf@picmaxx by-\pgf@picminx\relax% maxx is now the width
  \pgf@xa=\pgf@trimleft@final\relax  \ifdim\pgf@xa=0pt \else\kern\pgf@xa\fi
  \pgf@xa=\pgf@trimright@final\relax \ifdim\pgf@xa=0pt \else\kern\pgf@xa\fi
  \pgfsys@invoke{</g>\Hnewline </svg>\Hnewline }%

%% we'll use this hbox for the mtext rendering method (the "text only=true" option)
  \fi\fi% tweak for TikZ
    transform="scale(1,-1) translate(\pgf@sys@tonumber{\pgf@x},0)"
      \HCode{ font-size="\f@size"}%
  \HCode{>\Hnewline }%
  \HCode{</text>\Hnewline }%

%% we save this definition

%% we have to use some "evil" ^^ tex4ht hackery
% tex4ht hackery
{% grouping necessary cause we change the catcode of ":"
  %%% this is necessary to calculate the size of text nodes with
  %%% inline maths  
  % (it is an improvement of tex4ht, got to ask eitan gurary if he
  % could put it in the tex4ht code) 
    \a:DviMath\leavevmode \MathClass
  % these are configures for the foreignelement rendering method of text nodes : we let tex4ht write xhtml and mathml in the svg picture
        display="inline" }\DviMathAlt}{\EndDviMath}{}%%% we REALLY need to use \DviMathAlt here instead of \DviMath see above remark
        display="block" }\IgnorePar
      \ifvmode\else \HCode{<!--tex4ht:inline-->}\fi\EndP
      \DviMath}{\EndDviMath\ShowPar\pgfsys@foreignobject@par{\csname HCondtrue\endcsname\noindent}
    \Configure{SUB}{\Send{BACK}{<\a:mathml msub><\a:mathml mrow\Hnewline>}%
      \Tg</\a:mathml mrow>\Tg<\a:mathml mrow\Hnewline>\trap:base}{\Tg</\a:mathml mrow>\TG</\a:mathml msub\Hnewline>}%
    \Configure{SUP}{\Send{BACK}{<\a:mathml msup><\a:mathml mrow\Hnewline>}%
      \Tg</\a:mathml mrow>\Tg<\a:mathml mrow\Hnewline>\trap:base}{\Tg</\a:mathml mrow>\TG</\a:mathml msup\Hnewline>}%
    \Configure{SUBSUP}{\Send{BACK}{<\a:mathml msubsup><\a:mathml mrow\Hnewline>}%
      \Tg</\a:mathml mrow>\Tg<\a:mathml mrow\Hnewline>\trap:base}{\Tg</\a:mathml mrow>\Tg<\a:mathml mrow\Hnewline>\trap:base}{\Tg</\a:mathml mrow>\TG</\a:mathml msubsup\Hnewline>}%
  \global\let\pgfsys@foreignobject@par=\par % use this instead of \par
                                % -- \Configure is no \long macro (at
                                % least in one of the older
                                % versions). 

  % these are the configures for the mtext rendering method of text
  % nodes : we turn everything into regular character (don't want math
  % modes or subsup) 
  % and we hope it works (it doesn't if the text nodes aren't simple) 
    % no configures for \(\) and \[\] those might be needed for
    % context or Latex. Let me know ! 
    \Configure{$$}{}{}{}% no block maths
    \Configure{$}{}{}{}%$ no inline maths
    \Configure{SUB}{\HCode{<tspan baseline-shift="sub">}}{\HCode{</tspan>}}%
    \Configure{SUP}{\HCode{<tspan baseline-shift="sub">}}{\HCode{</tspan>}}%
    \Configure{SUBSUP}{}{}{}% not both sub and sup
    \Configure{htf} {0}{+}{<tspan font-family="}{\%s}{" font-size="\%s}{}{}{">}{</tspan>}%

% a counter to produce unique ids for each text node rendered with foreign element

% pgfutil@minipage
%  I had to change it to make it play nice with the way tex4ht puts <p> and <\p> tags 

  \hbox to#2\bgroup
% though it works, I'm desabling the above mentioned hack to make tex4ht behave with <p> <\p>, because it blurps the positioning 
% (I''l try to fix that later, it's a css thing)
%    \ifpgfsys@textonly\else\HtmlParOn\fi
% \noindent is better as it doesn't produce indentation AND it makes you leave vertical mode
%  same thing here
%  \ifpgfsys@textonly\else\EndP\HtmlParOff\fi

% I'll have to fix those names later
% this is the alternate hbox routine that renders text nodes through the foreignobject tag 
  % Compute box y translation  (x translation is correct).
  \HCode{<g transform="scale(1,-1) translate(0,\pgf@sys@tonumber{\pgf@y}) % got to translate in y
    scale(0.5,0.5)">\Hnewline}% scaling work around (damn dumb browsers !)
  % Compute box size (scaled 2 times)
  % this is necessary to make the browser scale the font down 50% (the STUPID browsers won't make font-size:50% happen, 
  % so we have to work around this by making svg scale the things down 50%, doubling the size of the text node frame (i.e. no changement there)
  \advance\pgf@y by 2\dp#1\relax
  % this is to adjust the y translation to compensate for the differences between the TeX and the Html models for lines
    $abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZf_1^1\overline D)_1^1\vec i\vec j$}%
  \advance\pgf@t by -\ht\pgfsys@foreignobject@Box\relax
  \advance\pgf@s by \dp#1\relax
  \advance\pgf@s by -\dp\pgfsys@foreignobject@Box\relax
    \advance \pgf@y by -2\pgf@s\relax
  \HCode{<foreignObject x="0" y="\pgf@sys@tonumber{\pgf@t}"
    width="\pgf@sys@tonumber{\pgf@x}" height="\pgf@sys@tonumber{\pgf@y}">}%
  % this is to make use of the css, class and id options through which you can control the way things display
    /pgf/tex4ht node/css/.get=\pgfsys@foreignobject@css,%
    /pgf/tex4ht node/class/.get=\pgfsys@foreignobject@class,%
    /pgf/tex4ht node/id/.get=\pgfsys@foreignobject@id
    <link rel="stylesheet" type="text/css" href="\pgfsys@foreignobject@css.css" />\Hnewline
    % I'll have to look into this, perhaps there are better
    % doctype....&nbsp; aren't recognised and produces invalid svg
    % pictures a workaround (not implemented yet) would be to make
    % them into entities of the svg picture. 
    <body id="\pgfsys@foreignobject@id" class="\pgfsys@foreignobject@class" xmlns="http://www.w3.org/1999/xhtml" >\Hnewline 
  % debug positioning not needed but it would be nice to let it there to control things later on
  % \HCode{<rect x="0" y="0" width="\pgf@sys@tonumber{\pgf@x}" height="\pgf@sys@tonumber{\pgf@y}" 
  %   style="fill:yellow;stroke:red; fill-opacity:0.5;stroke-opacity:0.5"/>}
  \global\advance\pgf@sys@svg@nodenum by 1\relax    

% the options and the code that controls it all (switches between \pgfsys@mtext@hbox and \pgfsys@foreignelement@hbox)

  tex4ht node/escape/.is choice,
  tex4ht node/escape/false/.code={\let\pgfsys@hbox\pgfsys@mtext@hbox\pgfsys@mtext@configure\pgfsys@textonlytrue},
  tex4ht node/escape/true/.code={\let\pgfsys@hbox\pgfsys@foreignobject@hbox\pgfsys@foreignobject@configure\pgfsys@textonlyfalse},
  tex4ht node/css/.initial=\jobname,
  tex4ht node/class/.initial=foreignobject,
  tex4ht node/id/.initial=\jobname\the\pgf@sys@svg@picnum-\the\pgf@sys@svg@nodenum

% this might be needed 

% There is something rellay wrong with the way "%" chars are used in here... you should code "}%" to avoid unnecessary spaces
% and it is unnecessary to code \cs%
% got to be carefull with numbers though, cf the texbook...2\relax and 2 % are okey


\def\pgfsys@shadingoutsidepgfpicture#1{\pgf@sys@fail{svg code in preamble}}
\def\pgfsys@shadinginsidepgfpicture#1{\pgf@sys@fail{svg code in preamble}}

    \setbox\pgfpic=\hbox to0pt{%

  \pgf@sys@svg@sh@defs% hmmm....
  \pgfsysprotocol@literal{<g transform="translate(\pgf@sys@tonumber{\pgf@xa},\pgf@sys@tonumber{\pgf@ya})">\Hnewline}%

  % Protect against color.4ht evil meddling with xcolor:

  % \ConfigureEnv{pgfpicture}{}{}{}{} there is no environment in plain TeX and this produces errors
  % this might be needed for context or latex though....let me know !


%%% Local Variables: 
%%% mode: latex
%%% End: 

The SVG is now rendered correctly:

enter image description here

  • Michal, thank you as always. I can repackage your solution to fit my needs. It is too long for a comment so I will post it as a solution. – Andrew Sep 8 '17 at 16:06

Michal's solution answers my question perfectly. For my use-case I need to repackage it slightly as I want to automatically apply this from within a document class file for a package, so editing pgfsys-tex4ht.def is not an option for me. As this is too long for a comment I give the details here but this is really only a small variation on Michal's ideas.

To apply Michal's "patch" automatically we extend the ideas from Work around for bug in pgf when used with htlatex? by making a new file pgfsys-tex4ht-alt.def that contains:

% Two fixes to pgfsys-tex4ht.def due to Michal Hoftich
\input pgfsys-tex4ht.def

% from https://tex.stackexchange.com/questions/386757

% from https://tex.stackexchange.com/questions/390421
\RequirePackage{etoolbox}% for \patchcmd
\def\htmlencoding{\expandafter\getEncoding\Preamble ,charset=,\@nil}

The first lines here fix the \Hnewline bug from Work around for bug in pgf when used with htlatex? and the last section is the code from Michal's solution which is applied using \patchcmd from the etoolbox package.

I want the "patched" version of pgfsys-tex4ht.def to be loaded automatically by my document class whenever the (optional) class option tikz is given. This is achieved by the following lines in the class file:



   % hack to fix two pgf bugs - apply only when when using tex4ht

With this in place tikz and tex4ht will play more happily together.

  • This is nice trick with the \patchcmd. – michal.h21 Sep 8 '17 at 16:53
  • @michal.h21 I think that I learned this trick on TeX.SX from one of egreg's answers... – Andrew Sep 8 '17 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.