michal-h21 / dvisvgm4ht

TikZ driver for tex4ht which uses dvisvgm for the SVG production
10 stars 1 forks source link

Failed to generate SVG with `pgfsys-dvisvgm4ht` and TeX4ht generates a broken `dvi` #4

Closed Iey4iej3 closed 4 years ago

Iey4iej3 commented 4 years ago

MWE

\documentclass{amsart}

\usepackage{caption}

% Include the following code before \usepackage{tikz}
\ifdefined\HCode
  \def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi 
\usepackage{tikz}
\usetikzlibrary{patterns,matrix}

\begin{document}
\begin{figure}[t]
  \captionsetup{width=.9\linewidth}
  \begin{tikzpicture}[scale=0.9]
    \draw[step=1cm,gray,very thin] (0,0) grid (8,8);

    \draw
    (0,8) -- (0,0) -- (8,0);

    \fill[pattern=north east lines,pattern color=gray,opacity=0.5]
    (2,8) -- (2,5) -- (5,5) -- (5,3) -- (6,3) -- (6,2) -- (8,2) -- (8,8);
    \draw
    (2,8) -- (2,5) -- (5,5) -- (5,3) -- (6,3) -- (6,2) -- (8,2);

    \fill[fill=black] (2,5.25) -- (2,5) -- (2.25,5) -- (2.25,5.25);
    \fill[fill=black] (5,3.25) -- (5,3) -- (5.25,3) -- (5.25,3.25);
    \fill[fill=black] (6,2.25) -- (6,2) -- (6.25,2) -- (6.25,2.25);

    \fill[fill=blue!40!white,opacity=0.5]
    (0,8) -- (0,0) -- (4,0) -- (4,3) -- (3,3) -- (3,5) -- (2,5) -- (2,8);

    \fill[fill=red!40!white,opacity=0.5]
    (3,3) -- (4,3) -- (4,4) -- (3,4);

    % !perl -we 'for my $y (reverse 0..7) { for my $x (0..7) { print % "\$x^$x\Ey^$y\$ & " } print "\\\\ \n" }'
    \begin{scope}[yshift=4cm, xshift=4cm]
      \matrix[matrix of nodes,nodes={inner sep=0pt,text width=0.9cm,align=center,minimum height=0.9cm}]{
        \tiny $x^0y^7v_1$ & \tiny $x^1y^7v_1$ & \tiny $x^2y^7v_1$ & \tiny $x^3y^7v_1$ & \tiny $x^4y^7v_1$ & \tiny $x^5y^7v_1$ & \tiny $x^6y^7v_1$ & \tiny $x^7y^7v_1$ & \\
        \tiny $x^0y^6v_1$ & \tiny $x^1y^6v_1$ & \tiny $x^2y^6v_1$ & \tiny $x^3y^6v_1$ & \tiny $x^4y^6v_1$ & \tiny $x^5y^6v_1$ & \tiny $x^6y^6v_1$ & \tiny $x^7y^6v_1$ & \\
        \tiny $x^0y^5v_1$ & \tiny $x^1y^5v_1$ & \tiny $x^2y^5v_1$ & \tiny $x^3y^5v_1$ & \tiny $x^4y^5v_1$ & \tiny $x^5y^5v_1$ & \tiny $x^6y^5v_1$ & \tiny $x^7y^5v_1$ & \\
        \tiny $x^0y^4v_1$ & \tiny $x^1y^4v_1$ & \tiny $x^2y^4v_1$ & \tiny $x^3y^4v_1$ & \tiny $x^4y^4v_1$ & \tiny $x^5y^4v_1$ & \tiny $x^6y^4v_1$ & \tiny $x^7y^4v_1$ & \\
        \tiny $x^0y^3v_1$ & \tiny $x^1y^3v_1$ & \tiny $x^2y^3v_1$ & \tiny $x^3y^3v_1$ & \tiny $x^4y^3v_1$ & \tiny $x^5y^3v_1$ & \tiny $x^6y^3v_1$ & \tiny $x^7y^3v_1$ & \\
        \tiny $x^0y^2v_1$ & \tiny $x^1y^2v_1$ & \tiny $x^2y^2v_1$ & \tiny $x^3y^2v_1$ & \tiny $x^4y^2v_1$ & \tiny $x^5y^2v_1$ & \tiny $x^6y^2v_1$ & \tiny $x^7y^2v_1$ & \\
        \tiny $x^0y^1v_1$ & \tiny $x^1y^1v_1$ & \tiny $x^2y^1v_1$ & \tiny $x^3y^1v_1$ & \tiny $x^4y^1v_1$ & \tiny $x^5y^1v_1$ & \tiny $x^6y^1v_1$ & \tiny $x^7y^1v_1$ & \\
        \tiny $x^0y^0v_1$ & \tiny $x^1y^0v_1$ & \tiny $x^2y^0v_1$ & \tiny $x^3y^0v_1$ & \tiny $x^4y^0v_1$ & \tiny $x^5y^0v_1$ & \tiny $x^6y^0v_1$ & \tiny $x^7y^0v_1$ & \\
      };
    \end{scope}
  \end{tikzpicture}
  \caption{\label{fig:good}A graphical depiction of a good generating family (the non-hatched cells) in
    the special case~$n = 2, m = 1$, writing~``$x$'' and~``$y$'' for~$x_1$ and~$x_2$. The hatched cells indicate vectors which
    have already been removed from the family. The small black squares indicate
    \emph{corners}. If the vector in the red cell will be found to be expressible
    as a linear combination of vectors with smaller index (blue cells), it will
    be removed, along with the vectors in all cells to the top and to the right
  of the red cell.}
\end{figure}
\end{document}

Remark

I try to manipulate dvisvgm manually, and get PostScript error: undefined in pgfpat3.

michal-h21 commented 4 years ago

This issue seems to be caused by this command:

 \fill[pattern=north east lines,pattern color=gray,opacity=0.5]
  (2,8) -- (2,5) -- (5,5) -- (5,3) -- (6,3) -- (6,2) -- (8,2) -- (8,8);

I hope it was fixed by the last commit: 9ec3171. I don't remember why I used dvips driver instead of dvisvgm. I guess there were some issues, so it is possible that other TikZ images will fail. Please test it, if you have more examples.

Iey4iej3 commented 4 years ago

The image shows but it is truncated. I opened the svg file, it is also truncated. You can compile it with pdflatex and compare the result, see the leftmost column.

michal-h21 commented 4 years ago

Ah I can see it. It isn't truncated when I load the dvips driver, but the conversion fails when the pattern is used then. I guess similar issue in the past was the reason to use dvips driver instead of dvisvgm. I need to investigate this more.

michal-h21 commented 4 years ago

You can try this version of pgfsys-dvisvgm4ht.def. It seems to not crop the image:

% 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.

% Driver commands for tex4ht

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

\AtBeginDocument{
  % configure the output picture format to svg
  \Configure{Picture}{.svg}
  % insert tex4ht hook to the code used at the start and end of each TikZ picture
  \def\pgfsys@beginpicture{%
    \bgroup%
    \ifdefined\inside@pict@cmd% handle nested uses
    \def\run@pict@cmd{}% insert the \Picture hooks only in the top nesting level
    \def\end@pict@cmd{}%
    \else
    % use different version of \Picture depending on the vertical mode
    \ifvmode\def\run@pict@cmd{\Picture*}\else\def\run@pict@cmd{\Picture+}\fi%
    \def\end@pict@cmd{\EndPicture}%
    \fi%
    % command used to detect nesting
    \def\inside@pict@cmd{}%
    \csname a:tikzpicture\endcsname%
    \orig@pgfsys@begin%
  }%
  \def\pgfsys@endpicture{%
    \orig@pgfsys@end%
      \csname b:tikzpicture\endcsname%
      \egroup%
      \par%
  }%
  \ConfigureEnv{tikzpicture}{\ifvmode\Picture*{}\else\Picture+{}\fi\def\inside@pict@cmd{}}{\EndPicture}{}{}
}

% Make the code inserted by tex4ht configurable

\NewConfigure{tikzpicture}{2}
\Configure{tikzpicture}{%
  % \ifvmode\IgnorePar\fi\EndP%\HtmlParOff
  \protect\csname nested:math\endcsname% support display math
  \run@pict@cmd{}%
}{\end@pict@cmd}

\let\orig@pgfsys@begin\pgfsys@beginpicture
\let\orig@pgfsys@end\pgfsys@endpicture
\def\pgf@sys@postscript@header#1{{\special{! #1}}}

\endinput

%%% Local Variables: 
%%% mode: latex
%%% End: 
Iey4iej3 commented 4 years ago

That seems working (I tried another tikz picture).

Iey4iej3 commented 4 years ago

Seemingly this does not work for tikz-cd. Sample code:

\documentclass{amsart}

% Include the following code before \usepackage{tikz}
\ifdefined\HCode
  \def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi 

\usepackage{tikz}
\usepackage{tikz-cd}

\DeclareMathOperator\HH{HH}
\DeclareMathOperator\HP{HP}
\DeclareMathOperator\myH{H}
\DeclareMathOperator\dR{dR}

\begin{document}
  Look at
  \[
    \begin{tikzcd}
      (R_1,M_1) \rar[shift left,"f"]\rar[shift right,"g"'] & (R_0,M_0) \lar\rar & (R,M)
    \end{tikzcd}
  \]
  and
  \begin{tikzcd}
    &\HH_*(X/k)\arrow[Rightarrow]{dr}{\text{Tate}}&\\
    \myH^*(X,\Omega^*_X)\arrow[Rightarrow]{ur}{\text{HKR}}\arrow[Rightarrow]{dr}[swap]{\text{Hodge--de Rham}}&&\HP_*(X/k)\\
    &\myH^*_{\dR}(X/k)\arrow[Rightarrow]{ur}[swap]{\text{de Rham--HP}}&
  \end{tikzcd}
\end{document}

Both images are broken (strange enough, the images in this extracted form are broken differently from those integrated in the original ebook).

michal-h21 commented 4 years ago

I hope it is fixed by the last commit. But please test it also with other files.

Iey4iej3 commented 4 years ago

Seemingly working, except: is it expected that if I enable \usepackage{todonotes}, then the generated SVG is incorrect? I tested the preamble and found that it is this package which leads to a problem.

michal-h21 commented 4 years ago

@Iey4iej3 maybe you use todonotes before \def\pgfsysdriver{pgfsys-dvisvgm4ht.def}? It loads TikZ, but with wrong driver if it is used earlier.

Iey4iej3 commented 4 years ago

Thanks for clarifications.