1

I am running Windows 10 or 11 (I don't know), have WSL2 running on my system - Ubuntu distribution, and have SageMath downloaded on my computer. I have successfully established contact between all three softwares, and have been able to run .tex files containing Sage code.

From the Ubuntu terminal, I run:

  1. cd /mnt/c/users/myname

  2. pdflatex filename.tex

  3. sage filename.sagetex.sage

  4. pdflatex filename.tex

I would like to make a TikZ (or asymptote, but that's more complicated and I believe it probably legitimately requires the python method mentioned below) animation which is based on math that is beyond TeX's capabilities, using the sagetex package. I am having trouble getting this to work and would like some advice. Worst case scenario would be making a Python script to rewrite the whole document for each frame - without using the animate package, then append them together using ImageMagick or something - which is okay if I need to. All I care about is the product; I do not care how long it takes for my computer to make it.

Here is a Minimal non-Working Example:

\documentclass{beamer}
\usepackage{sagetex,animate,tikz}
\begin{document}
\begin{sageblock}
    f(x) = x
\end{sageblock}
\begin{animateinline}[palindrome]{1}
\multiframe{10}{x=0+1}{
\begin{tikzpicture}
\draw (0,0) -- (\sage{f(\x)},0);
\end{tikzpicture}}
\end{animateinline}
\end{document}

Please tell me; can I make this work for tikz, or is the Python script option the better way to go? I don't think I need to ask about whether this would work for asymptote, lol :)

Thanks!

1 Answers1

0

Your approach in the code above, using the animate package in LaTeX doesn't work because your LaTeX code includes the code \sage{f(\x)}. For the sagetex package to work your document has compile properly in your step 2 (pdflatex filename.tex). Since Sage won't run until step 3, and that missing content/calculation is necessary to construct the plot in step 2, LaTeX compilation fails. That's the essential reason why, in many of my answers for sagetex problems, I create a string of LaTeX code that is then inserted into the LaTeX document on your step 4. For example, see my answer to How do you plot the Riemann Zeta function using Tikz/Pgfplot?. However, I don't see how this approach can work with the animate package in LaTeX. I think you could with a lot of time and effort use the animate command in Sage to create an animated .gif. That would involve creating the Tikz images using Sage along with the animate command in Sage. Your list of graphics objects would in Sage's animate code would be the Tikz images. I think this would significantly increase the time/complexity in creating the animated gif. If it were me, I would construct the animation using Sage plotting along with Sage `animate'. To illustrate that approach, go to the Sage Cell Server. Copy/paste the code below into the box. Then press the 'Evaluate' button as shown in the picture. You will see Sage run the animation.

def Rotate(A,P,degrees):
    A1 = [A[0]-P[0],A[1]-P[1]]
    theta = degrees*pi/180
    R = [A1[0]*cos(theta)-A1[1]*sin(theta), A1[0]*sin(theta)+A1[1]*cos(theta)]
    return [R[0]+P[0],R[1]+P[1]]

L=[] for j in range(1,15): G = Graphics() N = j Start = [1,0] Finish = [0,0] Curve = [[1,0], [0,0]] n = 1

while n<N:
    CurveR=[]
    for i in range(0,len(Curve)-1):
        CurveR += [Rotate(Curve[i],Curve[len(Curve)-1],-90)] 
    for i in range(len(CurveR)-1,-1,-1):    
        Curve += [CurveR[i]]
    n += 1
G += line(Curve)
L += [G.plot()]

d=animate([L[k]for k in range(1,14)],xmin=-22,xmax=87,ymin=-44,ymax=90,axes=False)

d.show(delay=100)

The result is the animation in the picture below:

enter image description here

You can download the gif by right clicking on the image. With respect to your code above, I think this code is closer to what you wrote:

f(x)=x
L=[]
for j in range(0,15):
    G = Graphics()
    Curve = line([(0,0), (f(j),0)])
G += Curve
L += [G.plot()]

d=animate([L[k]for k in range(1,14)],xmin=0,xmax=15,ymin=0,ymax=2,axes=False)

d.show(delay=100)

You can see the result of right clicking it below:

enter image description here

Creating the animation using only Sage should be relatively quick and easy. To have an animation of Tikz images, the line L += [G.plot()] would have to be modified to include, png/pdf Tikz plots that have already been created (extra steps) or are created on the fly (lots of extra coding). So I suspect you could adapt animate in Sage to create an animation of Tikz pictures but that's a lot more complicated and time consuming to do.

DJP
  • 12,451
  • Thank you for your very detailed response; this will be helpful to people in the future trying to navigate the same problem. In the end I decided to ditch the animate package completely and just use Python to do the math and make each "frame." Then I just appended the frames together in a gif. Since Sage is Python-based, this is most likely accomplishable in Sage as well. Thanks again for your response. Basically, I used a for loop instead of animate. –  Feb 03 '24 at 01:10