# Problem with the arrow of vectors using TIKZ and Tex4ht

I am using tikz and tex4ht in order to create a page in HTML, and when I draw a vector where I indicate its name with $\vec{u}$ for instance, the rendering with tex4ht gives a svg with weird symbols instead of the arrow above "u".

I've also tried $\overrightarrow{u}$ but nothing appears instead.

\begin{tikzpicture}[scale=1.5]
\node[circle,draw,fill=black,scale=0.1] (M) at (10,1) {};
\node[circle,draw,fill=black,scale=0.1] (N) at (12,1) {};
\node[circle,draw,fill=black,scale=0.1] (P) at (10,2) {};
\node[circle,draw,fill=black,scale=0.1] (Q) at (12,3.5) {};

\draw[thick,black,postaction={decorate,decoration={markings,mark=at position 1 with {\arrow[black,line width=1.5pt]{>}}}}](M)node[]{}--(N)node[]{};
\draw[thick,black,postaction={decorate,decoration={markings,mark=at position 1 with {\arrow[black,line width=1.5pt]{>}}}}](P)node[]{}--(Q)node[]{};

\node[below] at (11,1) {$\vec{u}$};
\node[above left] at (11,2.5) {$\vec{v}$};

\end{tikzpicture}


Does someone know why and how to get a correct display of $\vec{u}$ ? Sincerely

Edit: I've sutup repository with fixed tex4ht drivers for Pgf here

Incidentally, this is the same issue as in another recent question: TikZ tex4ht export creates SVG files that have <?xml version="1.0" encoding="iso-8859-1" standalone="no"?> declaration, even if the actual encoding is different. To fix that, you need to use the following version of 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.
%

\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

%
%
\input pgfsys-common-svg.def

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

\newcount\pgf@sys@svg@picnum

\def\pgfsys@pictureboxsynced#1{%
\pgfsys@beginscope\pgflowlevelsynccm\box#1\pgfsys@endscope%
}

%%
%% 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$};
%\endtikzpicture
%
% 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
%

%\def\pgfsys@beginpicture{%
% % \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>}
%}

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

\def\pgfsys@svg@newline{^^J}

% we don't want tex4ht putting <p> and </p> tags automatically in our svg code and causing invalid xhtml in our svg pictures
\newif\ifpgfsys@CssIncluded@\pgfsys@CssIncluded@false
\def\pgfsys@beginpicture{%
\EndP\HtmlParOff
\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
\ifpgfsys@CssIncluded@
\else
\Css{
.foreignobject {%
line-height:100\%;
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
font-family:STIXgeneral,Times,Symbol,cmr10,CMSY10,CMEX10;%
% can't wait for the stix font...the cmr10, etc font are
% available from the mozilla site, times and symbols should be
% okey
margin:0;
% 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
}
}%
\Css{%
math {%, math[mode="inline"] {
vertical-align:baseline;
line-height:100\%;
font-size:100\%;
font-family:STIXGeneral,Times,Symbol, cmr10,cmsy10,cmex10,cmmi10;
font-style: normal;
margin:0;
% background-color:\#99ff99;
}%
}%
\pgfsys@CssIncluded@true
\fi
}%

\def\pgfsys@endpicture{%
\HtmlParOn
\par% FIXME : was '\Par' but that seems to be undefined!?
}%

\def\getEncoding#1charset=#2,#3\@nil{\if|#2|iso-8859-1\else#2\fi}
\def\htmlencoding{\expandafter\getEncoding\Preamble ,charset=,\@nil}
% this is okay for both rendering methods for text nodes (mtext or foreignelement)...
\def\pgfsys@typesetpicturebox#1{%
% Ok, compute width/height
\pgf@xa=\pgf@picminx%
\pgf@ya=\pgf@picminy%
\pgf@xb=\pgf@picmaxx%
\pgf@yb=\pgf@picmaxy%
\pgf@x=\pgf@xb%
\pgf@y=\pgf@yb%
\ifdim\pgf@y<1pt%
\pgf@y=1pt%
\fi%
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@y%
\pgfsysprotocol@flushcurrentprotocol%
\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.
\pgf@x=1.3333333\pgf@x%
\pgf@y=1.3333333\pgf@y%
% Add one pixel for security:
\pgf@sys@pt\pgf@x%
\pgfsysprotocol@flushcurrentprotocol%
\pgfsys@invoke{" height="}%
\pgf@sys@pt\pgf@y%
\pgfsysprotocol@flushcurrentprotocol%
\pgfsys@invoke{" type="image/svg+xml"><p>SVG-Viewer needed.</p></object>}%
\special{t4ht>\jobname-\the\pgf@sys@svg@picnum.svg}%
%%%%%%%% This is necessary to control the svg through css
\pgfkeys{%
/pgf/tex4ht node/css/.get=\pgfsys@foreignobject@css
}%
\pgfsys@invoke{<?xml version="1.0" encoding="\htmlencoding" standalone="no"?>\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"
height="\the\pgf@yc" viewBox="}%
\pgf@sys@pt{\pgf@xa}\pgf@sys@pt{\pgf@ya}\pgf@sys@pt{\pgf@xc}\pgf@sys@pt{\pgf@yc}%
\pgfsysprotocol@flushcurrentprotocol%
\pgf@ya=-\pgf@ya%
\pgfsys@invoke{">\Hnewline  <g transform="translate(0,}
\pgf@sys@pt\pgf@yb\pgfsysprotocol@flushcurrentprotocol%
\pgfsys@invoke{) scale(1,-1) translate(0,}%
\pgf@sys@pt\pgf@ya\pgfsysprotocol@flushcurrentprotocol%
\pgfsys@invoke{)">\Hnewline }%
%
\pgf@ya=\pgf@shift@baseline\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
\setbox#1=\hbox{\hskip-\pgf@picminx\lower\pgf@picminy\box#1}%
\ht#1=\pgf@picmaxy%
\wd#1=\pgf@picmaxx%
\dp#1=0pt%
\leavevmode
\pgf@xa=\pgf@trimleft@final\relax  \ifdim\pgf@xa=0pt \else\kern\pgf@xa\fi
\raise-\pgf@ya\box#1%
\pgf@xa=\pgf@trimright@final\relax \ifdim\pgf@xa=0pt \else\kern\pgf@xa\fi
\pgfsys@invoke{</g>\Hnewline </svg>\Hnewline }%
\special{t4ht<\jobname-\the\pgf@sys@svg@picnum.svg}%
}%

%% we'll use this hbox for the mtext rendering method (the "text only=true" option)
\def\pgfsys@hbox#1{%
\pgf@x=.5\wd#1%
\ifx\tikz@textcolor\relax\else\ifx\tikz@textcolor\@undefined\else%
\ifx\tikz@textcolor\pgfutil@empty%
\pgfsetfillcolor{.}%
\else%
\pgfsetfillcolor{\tikz@textcolor}%
\fi%
\fi\fi% tweak for TikZ
\HCode{<text
style="stroke:none"
transform="scale(1,-1) translate(\pgf@sys@tonumber{\pgf@x},0)"
text-anchor="middle"
}%
\ifpgfutil@format@is@latex%
\ifx\f@size\pgfutil@empty%
\else%
\HCode{ font-size="\f@size"}%
\fi%
\fi%
\HCode{>\Hnewline }%
\wd#1=0pt%
\ht#1=0pt%
\dp#1=0pt%
\box#1
\HCode{</text>\Hnewline }%
}%

%% we save this definition
\let\pgfsys@mtext@hbox\pgfsys@hbox

%% we have to use some "evil" ^^ tex4ht hackery
% tex4ht hackery
{% grouping necessary cause we change the catcode of ":"
\catcode:=11\relax
%%% 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)
\gdef\DviMathAlt{%
\a:DviMath\leavevmode \MathClass
\ht:special{t4ht@(}\hbox\bgroup{\expandafter\ifx\csname
ht:everypar\endcsname\relax\expandafter\everypar\else
\expandafter\ht:everypar\fi{}\leavevmode\ht:special{t4ht@)}}\DviSend
}%
% these are configures for the foreignelement rendering method of text nodes : we let tex4ht write xhtml and mathml in the svg picture
%
\gdef\pgfsys@foreignobject@configure{%
\Configure{$}{\Configure{@math}{%\a:mathml display="inline" }\DviMathAlt}{\EndDviMath}{}%%% we REALLY need to use \DviMathAlt here instead of \DviMath see above remark \Configure{$$}{\Configure{@math}{%\a:mathml display="block" }\IgnorePar \ifvmode\else \HCode{<!--tex4ht:inline-->}\fi\EndP \DviMath}{\EndDviMath\ShowPar\pgfsys@foreignobject@par{\csname HCondtrue\endcsname\noindent} }{\DisplayMathtrue}% \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>}% \Configure{htf}{0}{+}{<span\Hnewline class="}{\%s}{-\%s}{x-x-\%d}{}{">}{</span>}% }% \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) \gdef\pgfsys@mtext@configure{% % 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 \newcount\pgf@sys@svg@nodenum % pgfutil@minipage % % I had to change it to make it play nice with the way tex4ht puts <p> and <\p> tags % \newif\ifpgfsys@textonly \def\pgfutil@minipage[#1]#2{% \hbox to#2\bgroup \hsize=#2\relax \vbox\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 \noindent%\leavevmode }% \def\pgfutil@endminipage{% % same thing here % % \ifpgfsys@textonly\else\EndP\HtmlParOff\fi \egroup\egroup }% % I'll have to fix those names later \newbox\pgfsys@foreignobject@Box \newdimen\pgf@s \newdimen\pgf@t % this is the alternate hbox routine that renders text nodes through the foreignobject tag \def\pgfsys@foreignobject@hbox#1{% % Compute box y translation (x translation is correct). \pgf@y=-\ht#1\relax \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) \pgf@x=2\wd#1\relax \pgf@y=2\ht#1\relax \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 \setbox\pgfsys@foreignobject@Box=\hbox{abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZf_1^1\overline D)_1^1\vec i\vec j\$}%
\pgf@t=\ht#1\relax
\pgf@s=\pgf@t\relax
\ifnum\pgf@s>0\relax
\pgf@s=0pt
\pgf@t=0pt
\else
\pgf@t=2\pgf@t\relax
\fi
%
\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
\pgfkeys{%
/pgf/tex4ht node/css/.get=\pgfsys@foreignobject@css,%
/pgf/tex4ht node/class/.get=\pgfsys@foreignobject@class,%
/pgf/tex4ht node/id/.get=\pgfsys@foreignobject@id
}%
% 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
<div>\Hnewline
}%
\box#1%
\HCode{%
</div>\Hnewline
</body>\Hnewline
</foreignObject>\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"/>}
\HCode{</g>\Hnewline}%
}%

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

\pgfkeys{%
/pgf/.cd,
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
%\font\SvgText=cmr10\relax

% 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@outerinvoke{\ifpgfpicture\expandafter\pgfsys@invoke\else\expandafter\pgfutil@gobble\fi}

\begingroup%
#1%
\setbox\pgfpic=\hbox to0pt{%
\pgfsys@beginpicture%
\pgfsys@beginscope%
\pgf@sys@svg@sh@defs%
\pgf@sys@svg@sh%
\pgfsys@endscope%
\pgfsys@endpicture%
\hss%
}%
\pgf@process{\pgf@sys@svg@pos}%
\pgf@picminx=0pt%
\pgf@picminy=0pt%
\pgf@picmaxx=\pgf@x%
\pgf@picmaxy=\pgf@y%
\def\pgf@shift@baseline{0pt}%
\pgfsys@typesetpicturebox\pgfpic%
\endgroup%
}

#1%
\pgf@sys@svg@sh@defs% hmmm....
\pgf@process{\pgf@sys@svg@pos}
\pgf@xa=-.5\pgf@x%
\pgf@ya=-.5\pgf@y%
\pgfsysprotocol@literal{<g transform="translate(\pgf@sys@tonumber{\pgf@xa},\pgf@sys@tonumber{\pgf@ya})">\Hnewline}%
\pgf@sys@svg@sh%
\pgfsysprotocol@literal{</g>\Hnewline}%
}

\ifpgfutil@format@is@latex
% Protect against color.4ht evil meddling with xcolor:
\RequirePackage{xcolor}
\let\pgf@xcolor@declaredcolor=\@declaredcolor
\let\pgf@xcolor@undeclaredcolor=\@undeclaredcolor
\fi

\AtBeginDocument{
\let\pgfsys@invoke=\pgfsys@body@invoke
\ifpgfutil@format@is@latex
\let\pgf@texht@declaredcolor=\@declaredcolor
\let\pgf@texht@undeclaredcolor=\@undeclaredcolor
\def\@declaredcolor{\ifpgfpicture\expandafter\pgf@xcolor@declaredcolor\else\expandafter\pgf@texht@declaredcolor\fi}
\def\@undeclaredcolor{\ifpgfpicture\expandafter\pgf@xcolor@undeclaredcolor\else\expandafter\pgf@texht@undeclaredcolor\fi}
\fi
\def\pgfutil@color{\pgfsetcolor}
% \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 !
}%

\endinput

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


Somebody should probably make a bug report on TikZ site, even if the other error in SVG export is still unfixed.

It looks much better in utf8 encoded SVG file:

But here is still a problem that the arrows are not above the letters. You need to use /pgf/tex4ht node/escape=true option on TikZ picture to enable method which works for more complicated text inside nodes. It uses HTML and MathML to render the text, which means that some SVG renderers may have problem, but I hope that web browsers should be OK. At least Firefox is, I am not so sure about Chrome.

You can use this trick to specify the tex4ht node/escape in the configuration file:

\Preamble{xhtml}
\tikzset{every node/.style={/pgf/tex4ht node/escape=true}}
\begin{document}

\EndPreamble
`

This version looks better:

• Better, but still wrong. The arrow should be placed on top of the letter. Sep 9 '17 at 15:58
• @TorbjørnT. you are right. Default TikZ configuration for SVG doesn't handle any more complicated text nodes well, I added some example showing better solution, but I am afraid that it doesn't work in every browser. Sep 9 '17 at 17:20
• Thanks @michal.h21 ! I did as you said and it works better. However, and as shown in you seconde picture, when I use \tikzset{every node/.style={/pgf/tex4ht node/escape=true}} in the preamble, I have only half of the tags (half of the vector, half of some letters). Is it possible to show the whole tags ? Sep 9 '17 at 20:07
• @Jocelyn I see... I think it is caused by wrong bounding boxes of the nodes in the generated SVG. It is really hard to do that. You may also try the TikZ externalization, it should produce better results, but it is harder to use: tex.stackexchange.com/a/158921/2891 Sep 9 '17 at 20:53
• Thanks @michal.h21, I will try tomorrow and let you know. Sep 9 '17 at 20:55