Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Strange behavior of TikZ #945

Closed
matteosecli opened this issue Feb 4, 2018 · 9 comments
Closed

[bug] Strange behavior of TikZ #945

matteosecli opened this issue Feb 4, 2018 · 9 comments

Comments

@matteosecli
Copy link
Contributor

matteosecli commented Feb 4, 2018

Consider the following MWE.tex:

\documentclass{book}
\usepackage{tikz}
\usepackage{braket}

\begin{document}

\begin{tikzpicture}
	\draw[->] (0,0) -- (1,1) node[right] {$\Ket{1}$};
	\draw[->] (0,0) -- (1,2) node[right] {$\Ket{2}$};    % If you remove this line, it works.
\end{tikzpicture}

\end{document}

When I convert using

latexmlc MWE.tex --verbose --format=html5 --pictureimages --nosvg --destination=MWE.html

I have no errors if I comment out the second line, while if I leave it I get the following errors:

(PictureImages MWE2.html processing... [1 unique; 1 new]Error: /nocurrentpoint in --show--
Operand stack:
   --nostringval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--   867   613   (i)
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1999   1   3   %oparray_pop   1998   1   3   %oparray_pop   --nostringval--   1982   1   3   %oparray_pop   1868   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--
Dictionary stack:
   --dict:989/1684(ro)(G)--   --dict:1/20(G)--   --dict:92/200(L)--   --dict:179/300(L)--   --dict:57/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
Current file position is 144672
GPL Ghostscript 9.22: Unrecoverable error, exit code 1
Error: /nocurrentpoint in --show--
Operand stack:
   --nostringval--   --nostringval--   --nostringval--   --nostringval--   --nostringval--   867   613   (i)
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1999   1   3   %oparray_pop   1998   1   3   %oparray_pop   --nostringval--   1982   1   3   %oparray_pop   1868   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--
Dictionary stack:
   --dict:989/1684(ro)(G)--   --dict:1/20(G)--   --dict:92/200(L)--   --dict:179/300(L)--   --dict:57/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
Current file position is 144672
GPL Ghostscript 9.22: Unrecoverable error, exit code 1

Warning:uninitialized:$w Use of uninitialized value $w  in concatenation (.) or string
	at /usr/local/Cellar/latexml/HEAD-673dbda/libexec/lib/perl5/LaTeXML/Post/LaTeXImages.pm line 506
	In Post::PictureImages[@0x7fcc0bee1688] ->convert_image

Warning:uninitialized:$h Use of uninitialized value $h  in concatenation (.) or string
	at /usr/local/Cellar/latexml/HEAD-673dbda/libexec/lib/perl5/LaTeXML/Post/LaTeXImages.pm line 506
	In Post::PictureImages[@0x7fcc0bee1688] ->convert_image

Warning:uninitialized:$w Use of uninitialized value $w  in concatenation (.) or string
	at /usr/local/Cellar/latexml/HEAD-673dbda/libexec/lib/perl5/LaTeXML/Post/LaTeXImages.pm line 513
	In Post::PictureImages[@0x7fcc0bee1688] ->convert_image

Warning:uninitialized:$h Use of uninitialized value $h  in concatenation (.) or string
	at /usr/local/Cellar/latexml/HEAD-673dbda/libexec/lib/perl5/LaTeXML/Post/LaTeXImages.pm line 513
	In Post::PictureImages[@0x7fcc0bee1688] ->convert_image
 [Converting /var/folders/r2/21fdm8ds1rlc552vl1mcnqx40000gn/T/LaTeXMLcZ4cmn/imgx001 => /Users/matteo/latexml-bugs/bug-002/pic/pic19.png ( x )] 0.78 sec)
(MathML::Presentation MWE2.html 2 to process... [converted 2 Maths] 0.00 sec)
(XSLT[using LaTeXML-html5.xsl] MWE2.html processing... 0.03 sec)
(Writer MWE2.html processing... 0.00 sec) 0.82 sec)
Post-processing complete: 4 warnings

I also take the chance to report that, in the first case (the one who runs fine without error) the arrows are converted into an image but the math is not; however, since the math is (in this case) part of the picture, I expect that it gets converted along with the arrows in the same png.

In this direction, I think it would be nice to have something like what you obtain if you use

\usetikzlibrary{external}
\tikzexternalize

which converts the tikzpicture environments into separate PDF's at compile time. In this way, I think you can even get rid of the errors you get with TikZ+LaTeXML (which don't occur in PDFLaTeX) when you use "unorthodox" math (like in #760).

@dginev dginev added this to the LaTeXML-0.8.4 milestone Feb 4, 2018
@brucemiller
Copy link
Owner

Seems to be a problem in ghostscript; the pieces appear to work... not sure how to go about fixing. What do you not like about svg; it works pretty good here?

@matteosecli
Copy link
Contributor Author

matteosecli commented Feb 6, 2018

What do you not like about svg; it works pretty good here?

The example above is just a toy example; I usually write more complex TikZ stuff and LaTeXML crashes 99% of the times, even without additional TikZ external libraries; that's why I typically use --nosvg even if in the end I get a raster image. In addition, I don't like the fact that the math is not part of the SVG itself; this causes additional crashes and inconsistent scaling when you zoom-in or zoom-out. I'd like that the TikZ picture gets translated exactly as it is, but of course I don't expect LaTeXML to do that (and it would be out of scope, I think, to support all the possible TikZ libraries out there).

Recently, I was experimenting with the following trick, related to #902 (the following MWE.tex is one of my real-life TikZ pictures):

\RequirePackage{latexml}
\documentclass{book}
\usepackage{graphicx}
\usepackage{xcolor}
\definecolor{MAT-Navy-Blue}{rgb}{0.000,0.447,0.741}        % Navy Blue        (Blue)
\definecolor{MAT-Chocolate}{rgb}{0.850,0.325,0.098}        % Chocolate        (Brown)
\definecolor{MAT-Cornflower-Blue}{rgb}{0.301,0.745,0.933}  % Cornflower Blue  (Blue)

\iflatexml
%\DeclareGraphicsExtensions{%
%	.svg,.SVG,%
%	.png,.PNG,%
%	.pdf,.PDF,%
%	.eps,.EPS,%
%	.jpg,.mps,.jpeg,.jbig2,.jb2,.JPG,.JPEG,.JBIG2,.JB2}
\else
	\usepackage{tikz}
	\usetikzlibrary{external}
	\tikzexternalize
	\tikzsetfigurename{TikZFigure-}
\fi

\newcounter{tikzpicturecounter}
\newcommand{\includetikzexternalized}{
	\includegraphics[scale=1.5]{TikZFigure-\thetikzpicturecounter}
	\stepcounter{tikzpicturecounter}
}

\begin{document}

	\iflatexml
	\includetikzexternalized
	\else
	\begin{tikzpicture}[>=latex, scale=1.0, transform shape]
		\colorlet{TextColor}{.}
		\def\a{1.8}
		\def\XLength{5}
		\def\YLength{4}
		% Fill the MUC
		\fill[color=MAT-Cornflower-Blue,opacity=0.8] (0,0) -- (0,1*\a) -- (4*\a,1*\a) -- (4*\a,0) -- cycle;
		% Fill the plaquette with the explicit phase
		\fill[color=MAT-Chocolate,opacity=0.8] (2*\a,2*\a) -- (3*\a,2*\a) -- (3*\a,3*\a) -- (2*\a,3*\a) -- cycle;
		% Draw vertical dashed lines & ticks
		\foreach \xpos in {0,...,\XLength}
		{
			\pgfmathsetmacro{\XLabel}{int(\xpos+1)}
			\draw[dashed,color=black!40!white,text=TextColor]
				({\xpos*\a},{-0.5*\a}) -- ({\xpos*\a},{(\YLength+0.5)*\a});
			\draw ({\xpos*\a},{-1.05*\a}) node[below] {$\XLabel$} -- ({\xpos*\a},{-0.95*\a});
		}
		% Draw horizontal dashed lines & ticks
		\foreach \ypos in {0,...,\YLength}
		{
			\pgfmathsetmacro{\YLabel}{int(\ypos+1)}
			\draw[dashed,color=black!40!white,text=TextColor]	
				({-0.5*\a},{\ypos*\a}) -- ({(\XLength+0.5)*\a},{\ypos*\a});
			\draw ({-1.05*\a},{\ypos*\a}) node[left] {$\YLabel$} -- ({-0.95*\a},{\ypos*\a});
		}
		% Draw the lattice sites
		\foreach \xpos in {0,...,\XLength}
		{
			\foreach \ypos in {0,...,\YLength}
			{
				\draw ({\xpos*\a},{\ypos*\a}) node[circle,fill,color=MAT-Navy-Blue] {};
			}
		}
		% Draw some hopping phases
		\draw[->] (-0.15*\a,0.05*\a) to[bend left] node[midway,left] {$e^{i2\pi\vartheta}$} (-0.15*\a,0.95*\a);
		\draw[->] (+0.85*\a,0.05*\a) to[bend left] node[midway,left] {$e^{i4\pi\vartheta}$} (+0.85*\a,0.95*\a);
		\draw[->] (+1.85*\a,0.05*\a) to[bend left] node[midway,left] {$e^{i6\pi\vartheta}$} (+1.85*\a,0.95*\a);
		% Draw the plaquette phase
		\draw[->] (2.1*\a,2*\a) to node[midway,below]        {$0$}              (2.9*\a,2*\a);
		\draw[->] (3*\a,2.1*\a) to node[midway,below,sloped] {$8\pi\vartheta$} (3*\a,2.9*\a);
		\draw[->] (2.9*\a,3*\a) to node[midway,above]        {$0$}              (2.1*\a,3*\a);
		\draw[->] (2*\a,2.9*\a) to node[midway,below,sloped] {$-6\pi\vartheta$} (2*\a,2.1*\a);
		\draw (2.5*\a,2.5*\a) node {$2\pi\vartheta$};
		% Draw units and axes
		\draw[<->] (\a,-0.35*\a) -- (2*\a,-0.35*\a) node[midway,above] {$a$};
		\draw[->] (-\a,-\a) -- ({(\XLength+1)*\a},-\a) node[below] {$m$};
		\draw[->] (-\a,-\a) -- (-\a,{(\YLength+1)*\a}) node[left] {$n$};
	\end{tikzpicture}
	\fi

\end{document}

Then, my workflow would be:

pdflatex -shell-escape -interaction=nonstopmode MWE.tex
for file in TikZFigure-*; do pdf2svg $file ${file%.pdf}.svg; done
latexml MWE.tex --verbose --inputencoding=utf8 --destination=MWE.xml
latexmlpost MWE.xml --verbose --novalidate --format=html5 --graphicimages --graphicsmap=svg --destination=MWE.html

In this way:

  1. If you skip the step with pdf2svg (and remove the option --graphicsmap=svg, which I believe is one of the defaults anyway), your TikZ picture will be converted to a .png from the PDF generated by TikZ itself; this method works even where LaTeXML fails.
  2. If you do the step with pdf2svg, the result would be a vector image in the final HTML that also embeds the math and scales well (honestly speaking the SVG container does not scale upon resizing the browser window, but I think that would be the topic of another bug report).
  3. [Bonus] If you decomment the lines after \iflatexmland change the pdf2svg step by converting all the figures that you have, you also get the inclusion (as SVG's) of all the non-TikZ-generated vector PDF's that you have (Convert PDF graphics to scalable SVGs #902).

This is so far, to me, the only solution that generates good SVG's and doesn't imply endless LaTeXML failures because of my TikZ-filled documents; if you just feed the above example to LaTeXML without all of this gimmick, it goes crazy! 😅🤣

This isn't to say that LaTeXML should have a full support for TikZ, and I don't absolutely expect that as a user; but I think that at this stage the SVG feature is not yet fit for more complex drawings or responsive scaling.

@dginev dginev modified the milestones: LaTeXML-0.8.4, LaTeXML-0.8.5 Apr 8, 2019
@dginev
Copy link
Collaborator

dginev commented Jun 26, 2020

Hi @matteosecli ,

We didn't work on this issue explicitly I think, but several tikz-related PRs have landed in the latexml master since the issue was opened. Today, using the latest latexml master and texlive 2019, the first minimal example with which you opened the issue now converts error-free and renders in HTML as:

image

Could you double-check if this advances sufficiently to get this issue closed, or if you would point our attention to something else? Thanks!

@matteosecli
Copy link
Contributor Author

Hi @dginev, sorry for the late reply but I somehow missed the notification email. I'll give it a try as soon as I can and close the issue if the problems are solved! 🙂

@matteosecli
Copy link
Contributor Author

Hi again @dginev, I've made some tests.

Apart from the true pain of making LaTeXML + ImageMagick + Image::Magick work all together on MacOS (HomeBrew's LaTeXML is truly broken, but they are not willing to fix it), I can confirm that the first MWE works as intended. I'm on TeXLive 2017.

I've also been able to convert the second, more complex MWE2 that I've sent, by deactivating all the LaTeXML-specific fiddling so that it was back to a regular LaTeX + TikZ file.

Just to be clear, this is what the document looks like:

\documentclass{book}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{xcolor}
\definecolor{MAT-Navy-Blue}{rgb}{0.000,0.447,0.741}        % Navy Blue        (Blue)
\definecolor{MAT-Chocolate}{rgb}{0.850,0.325,0.098}        % Chocolate        (Brown)
\definecolor{MAT-Cornflower-Blue}{rgb}{0.301,0.745,0.933}  % Cornflower Blue  (Blue)

\begin{document}

	\begin{tikzpicture}[>=latex, scale=1.0, transform shape]
		\colorlet{TextColor}{.}
		\def\a{1.8}
		\def\XLength{5}
		\def\YLength{4}
		% Fill the MUC
		\fill[color=MAT-Cornflower-Blue,opacity=0.8] (0,0) -- (0,1*\a) -- (4*\a,1*\a) -- (4*\a,0) -- cycle;
		% Fill the plaquette with the explicit phase
		\fill[color=MAT-Chocolate,opacity=0.8] (2*\a,2*\a) -- (3*\a,2*\a) -- (3*\a,3*\a) -- (2*\a,3*\a) -- cycle;
		% Draw vertical dashed lines & ticks
		\foreach \xpos in {0,...,\XLength}
		{
			\pgfmathsetmacro{\XLabel}{int(\xpos+1)}
			\draw[dashed,color=black!40!white,text=TextColor]
				({\xpos*\a},{-0.5*\a}) -- ({\xpos*\a},{(\YLength+0.5)*\a});
			\draw ({\xpos*\a},{-1.05*\a}) node[below] {$\XLabel$} -- ({\xpos*\a},{-0.95*\a});
		}
		% Draw horizontal dashed lines & ticks
		\foreach \ypos in {0,...,\YLength}
		{
			\pgfmathsetmacro{\YLabel}{int(\ypos+1)}
			\draw[dashed,color=black!40!white,text=TextColor]	
				({-0.5*\a},{\ypos*\a}) -- ({(\XLength+0.5)*\a},{\ypos*\a});
			\draw ({-1.05*\a},{\ypos*\a}) node[left] {$\YLabel$} -- ({-0.95*\a},{\ypos*\a});
		}
		% Draw the lattice sites
		\foreach \xpos in {0,...,\XLength}
		{
			\foreach \ypos in {0,...,\YLength}
			{
				\draw ({\xpos*\a},{\ypos*\a}) node[circle,fill,color=MAT-Navy-Blue] {};
			}
		}
		% Draw some hopping phases
		\draw[->] (-0.15*\a,0.05*\a) to[bend left] node[midway,left] {$e^{i2\pi\vartheta}$} (-0.15*\a,0.95*\a);
		\draw[->] (+0.85*\a,0.05*\a) to[bend left] node[midway,left] {$e^{i4\pi\vartheta}$} (+0.85*\a,0.95*\a);
		\draw[->] (+1.85*\a,0.05*\a) to[bend left] node[midway,left] {$e^{i6\pi\vartheta}$} (+1.85*\a,0.95*\a);
		% Draw the plaquette phase
		\draw[->] (2.1*\a,2*\a) to node[midway,below]        {$0$}              (2.9*\a,2*\a);
		\draw[->] (3*\a,2.1*\a) to node[midway,below,sloped] {$8\pi\vartheta$} (3*\a,2.9*\a);
		\draw[->] (2.9*\a,3*\a) to node[midway,above]        {$0$}              (2.1*\a,3*\a);
		\draw[->] (2*\a,2.9*\a) to node[midway,below,sloped] {$-6\pi\vartheta$} (2*\a,2.1*\a);
		\draw (2.5*\a,2.5*\a) node {$2\pi\vartheta$};
		% Draw units and axes
		\draw[<->] (\a,-0.35*\a) -- (2*\a,-0.35*\a) node[midway,above] {$a$};
		\draw[->] (-\a,-\a) -- ({(\XLength+1)*\a},-\a) node[below] {$m$};
		\draw[->] (-\a,-\a) -- (-\a,{(\YLength+1)*\a}) node[left] {$n$};
	\end{tikzpicture}

\end{document}

and I convert it to vector this way:

latexmlc MWE2.tex --verbose --format=html5 --graphicimages --destination=MWE2.html

This is the result:
Schermata 2020-07-05 alle 17 42 53

I would say that's really amazing! 😄 👏 🎉

Thank you for the big improvements you've made, I think this bug can be now closed!

@dginev
Copy link
Collaborator

dginev commented Jul 5, 2020

That does look quite respectable! Thanks again for testing and for including the example + screenshot, now we can always come back to the issue and re-test if there's some doubt of regressions in newer latexml+texlive combinations. It's encouraging we also knocked this issue down with the pgf stability upgrades 👍

@Yunelsy
Copy link

Yunelsy commented Oct 11, 2021

Hi @matteosecli ,

We didn't work on this issue explicitly I think, but several tikz-related PRs have landed in the latexml master since the issue was opened. Today, using the latest latexml master and texlive 2019, the first minimal example with which you opened the issue now converts error-free and renders in HTML as:

image

Could you double-check if this advances sufficiently to get this issue closed, or if you would point our attention to something else? Thanks!

Hello @dginev. However, if you observe well in the conversion, the arrows are rotated to a vertical and horizontal position that is not the angle of the line. I am having this problem with all arrows that are not vertical or horizontal. Do you know something about this?
wrong

@dginev
Copy link
Collaborator

dginev commented Oct 11, 2021

Hi @Yunelsy ! No, I am not aware of that, and most recently it has been @brucemiller progressively upgrading the tikz conversion.

I think it may be very helpful if you can distill a small example and open a new issue. The smaller the example, the better, as it takes quite a long trip to debug which piece of the internals need to be fine-tuned.

@Yunelsy
Copy link

Yunelsy commented Oct 11, 2021

Hi @Yunelsy ! No, I am not aware of that, and most recently it has been @brucemiller progressively upgrading the tikz conversion.

I think it may be very helpful if you can distill a small example and open a new issue. The smaller the example, the better, as it takes quite a long trip to debug which piece of the internals need to be fine-tuned.

thank you for the replay @dginev, I will do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants