sagemath / sagetex

Embed code, results of computations, and plots from the Sage mathematics software suite (https://www.sagemath.org) into LaTeX documents. Source repository for https://pypi.org/project/sagetex/ and https://ctan.org/pkg/sagetex
https://ctan.org/pkg/sagetex
Other
58 stars 22 forks source link

Sage9.7 & Sagetex3.6: Runaway argument for "Draw polytopes in LaTeX using TikZ" #64

Closed kiryph closed 1 year ago

kiryph commented 1 year ago

Consider following minimal LaTeX file:

\documentclass{article}
\usepackage{sagetex} 
\usepackage{tikz} 

\begin{document}

\sagestr{(polytopes.great_rhombicuboctahedron()).tikz([4,5,6],45,scale=0.75)}

\end{document}

It is derived from

Compiling with

$ pdflatex minimal-sagetex-issue-tikz.tex
...
(/usr/local/texlive/texmf-local/tex/latex/local/sagetex.sty
...

$ /usr/local/bin/sage minimal-sagetex-issue-tikz.sagetex.sage
Processing Sage code for minimal-sagetex-issue-tikz.tex...
Inline formula 0 (line 10)
Sage processing complete. Run LaTeX on minimal-sagetex-issue-tikz.tex again.

$ pdflatex minimal-sagetex-issue-tikz.tex

gives me follwoing error:

...
(./minimal-sagetex-issue-tikz.sagetex.sout
Runaway argument?
{{\begin {tikzpicture}[x={(0.767968cm, 0.559570cm)}, y={(-0.407418cm,\ETC.
! Paragraph ended before \@newl@bel was complete.
<to be read again>
                   \par
l.25

Manually expanding content within \newlabel{@sageinline0}{{ from .sout into the LaTeX file allows me to have the result:

Screenshot 2023-01-05 at 08 13 44

Also the example given on https://doc.sagemath.org/html/en/tutorial/sagetex.html works. It seems only a tikz related issue.

Possibly related issues:

Environment

dimpase commented 1 year ago

Thanks for the report. Is MacTeX 2022 essentially TeXLive 2022 (with macOS gui)?

kiryph commented 1 year ago

Is MacTeX 2022 essentially TeXLive 2022 (with macOS gui)?

Yes, MacTeX 2022 is TeXLive 2022. It is a macOS installer of TeXLive with some macOS GUI applications bundled (e.g. the Editor TeXShop and the Bib Management Software BibDesk which I actually do not use but neovim+vimtex).

dimpase commented 1 year ago

yes, I can reproduce this.

dimpase commented 1 year ago

It seems that tikzpicture plays fast and rough with \newlabel macro. Indeed,

\documentclass{article}
\usepackage{tikz} 
\usepackage{sagetex} 
\begin{document}
\[
\sagestr{latex(x^2+1)}
\]
\end{document}

works just fine, and the corresponding ..sage.sout file is

% This file was *autogenerated* from z.sagetex.sage with
 % sagetex.py version 2021/10/16 v3.6
\newlabel{@sageinline0}{{%
x^{2} + 1}{}{}{}{}}
%4245208a0c868fead4f0a36f6d32b191% md5sum of corresponding .sage file (minus "goboom", "current_tex_line", and pause/unpause lines)

where the difference is in

x^{2} + 1}{}{}{}{}}

vs, in you example,

\begin{tikzpicture}%
%... lots of lines
\end{tikzpicture}}{}{}{}{}}
dimpase commented 1 year ago

no, tikzpicture by itself is OK - e.g. if I replace the picture in .sage.sout of your example with

\begin{tikzpicture}%
\node (a) at (0,0){example node};
\end{tikzpicture}

then all is fine. So it's what's inside your generated tikz picture that breaks the thing.

kiryph commented 1 year ago

Thanks for looking into this.

So it's what's inside your generated tikz picture that breaks the thing.

The code generating the tikz picture is here:

https://github.com/sagemath/sage/blob/4541564cf151a5138810e9c2144349f59469d769/src/sage/geometry/polyhedron/plot.py#L1229

The blog post https://jplab.github.io/polytope.html I have referenced mentions Sage 9.2. I am happen to have Sage 9.3 (released May 9, 2021) using SageTeX 3.5 and Sage 9.7 installed but both versions have this issue. I am not keen to install also Sage 9.2 to see what it looked like when it worked.

dimpase commented 1 year ago

could be the TeX version that matters. Actually I can reproduce this without sagetex. Away from the kbd now.

kiryph commented 1 year ago

could be the TeX version that matters. Actually I can reproduce this without sagetex.

I have actually installed Sage 9.2 and yes, the issue persists as you anticipated.

So the difference is the TeXLive Version.

I am not sure who to bother now.

The generated tikz code compiles on its own with the new TeXLive version. Sagetex without tikz polyhedra works as well. For example, the tikz code in http://mirrors.ctan.org/macros/latex/contrib/sagetex/example.tex works as well (uncommenting necessary).

IMHO, it is the tikz code from sage polyhedra which runs into problems.

Since the development of Sage moves to github

Sage development is scheduled to move to GitHub in February 2023

I will postpone this matter a little bit.

I'll leave this open if you do not mind until a solution is found. If you want to close it, since sagetex itself should be ruled out to mitigate the problem, feel free to do so.

dimpase commented 1 year ago

I fugured it out. The problem is that \sagestr{} assumes it gets one paragraph (otherwise the \newlabel macro used there gives this error) , but it's not the case here. For the example at hand, one can manually remove the empty line (there is just one) in the ..sout file before running LaTeX for the 2nd time, and it works.

One way to fix this, in Sage itself, would be to make sure that tikz() method does not produce empty lines.

Another, in SageTex, would be to sanitize input to \sagestr{}, so that it does not have empty lines.

dimpase commented 1 year ago

here is the fix in Sage itself:

--- a/src/sage/geometry/polyhedron/plot.py
+++ b/src/sage/geometry/polyhedron/plot.py
@@ -1532,7 +1532,7 @@ class Projection(SageObject):
         tikz_pic += "%% facet_color = {}\n".format(facet_color)
         tikz_pic += "%% opacity = {}\n".format(opacity)
         tikz_pic += "%% vertex_color = {}\n".format(vertex_color)
-        tikz_pic += "%% axis = {}\n\n".format(axis)
+        tikz_pic += "%% axis = {}\n%%\n".format(axis)

         # Draws the axes if True
         if axis:
@@ -1681,7 +1681,7 @@ class Projection(SageObject):
         tikz_pic += "%% facet_color = {}\n".format(facet_color)
         tikz_pic += "%% opacity = {}\n".format(opacity)
         tikz_pic += "%% vertex_color = {}\n".format(vertex_color)
-        tikz_pic += "%% axis = {}\n\n".format(axis)
+        tikz_pic += "%% axis = {}\n%%\n".format(axis)

         # Draws the axes if True
         if axis:
@@ -1881,7 +1881,7 @@ class Projection(SageObject):
         tikz_pic += "%% facet_color = {}\n".format(facet_color)
         tikz_pic += "%% opacity = {}\n".format(opacity)
         tikz_pic += "%% vertex_color = {}\n".format(vertex_color)
-        tikz_pic += "%% axis = {}\n\n".format(axis)
+        tikz_pic += "%% axis = {}\n%%\n".format(axis)

         # Draws the axes if True
         if axis:

I'll put it on Sage ticket - perhaps there yet more similar places to fix.

dimpase commented 1 year ago

See https://trac.sagemath.org/ticket/34904

kiryph commented 1 year ago

@dimpase Thank you for figuring it out.

One way to fix this, in Sage itself, would be to make sure that tikz() method does not produce empty lines.

Another, in SageTex, would be to sanitize input to \sagestr{}, so that it does not have empty lines.

Ideally, I would do both. I also would show a message to the user when sanitising was necessary.

It could happen that a user is writing the input for \sagestr{} with empty lines and should be informed that the input was corrected. The content of the file .sout is not something a user directly writes and therefore is surprised that a generated files throws errors which will not happen anymore.

dimpase commented 1 year ago

A TeX guru doesn't recommend to do such sanitizing, see
https://tex.stackexchange.com/a/536802/7110

I'll add the no \par requirement to the docs, and try to improve the error message.

kiryph commented 1 year ago

A TeX guru doesn't recommend to do such sanitizing, see https://tex.stackexchange.com/a/536802/7110

I'll add the no \par requirement to the docs, and try to improve the error message.

Sounds good to me.

dimpase commented 1 year ago

903d1c77422d06d91710650dc67a2504a6ffc0f9 adds this piece of documention.