TransWikia.com

How can I create an animated gif with a small file size?

TeX - LaTeX Asked on August 17, 2021

The only method I know for creating animated gifs with pdflatex is by generating each frame and applying imagemagicks convert. The issue is that there is a lot of stuff in all frames

pdflatex animation.tex -output-format=pdf
pdfcrop animation.pdf
convert -verbose -delay 10 -loop 0 -density 300 animation-crop.pdf animation.gif

enter image description here

  • Original: 1708px x 829px; 2.5MB
  • Removing the [background rectangle/.style={fill=white}, show background rectangle]: 1.6 MB (but then it looks pixelated / ugly for the characters)
  • Downscaling to 1200px: 1.8MB
  • Applying an optimizer: 92.92KiB

Is there any way I can generate a small image with pdflatex, e.g. by not re-drawing the axes all the time?

MVCE

documentclass[varwidth=false, border=2pt]{beamer}

usepackage{tikz,pgfplots,multido}
usetikzlibrary{backgrounds}
pgfplotsset{compat=1.17}
usepackage{nicefrac}
pgfplotsset{every axis legend/.append style={
at={(0,0)},
anchor=north east}}

usepackage[active,tightpage]{preview}
PreviewEnvironment{tikzpicture}
begin{document}
multido{i=0+1}{100}{%
begin{tikzpicture}[background rectangle/.style={fill=white}, show background rectangle]
    begin{axis}[
        axis x line=middle,
        axis y line=middle,
        grid = major,
        width=16cm,
        height=8cm,
        grid style={dashed, gray!30},
        xmin=-2.1,   % start the diagram at this x-coordinate
        xmax= 4.1,   % end   the diagram at this x-coordinate
        ymin= 0,     % start the diagram at this y-coordinate
        ymax= 17,    % end   the diagram at this y-coordinate
        xlabel=$x$,
        ylabel=$y$,
        ytick={0,2,3,4,8,9,12,16},
        yticklabels={0,2,3,4,8,9,12,16},
        legend cell align=left,
        legend pos=south east,
        legend style={draw=none},
        tick align=outside,
        enlargelimits=false]
      % plot the function
      addplot[domain=-2:{-1.5 + (9.5*i/100)}, red, ultra thick,samples=500] {2^x};
      addplot[domain=-2:{-1.5 + (7.5*i/100)}, blue, ultra thick,samples=500] {3^x};
      addplot[domain=-2:{-1.5 + (6.5*i/100)}, green, ultra thick,samples=500] {4^x};
    end{axis}
end{tikzpicture}
}

end{document}

One Answer

Only for compare purpose!

I reproduce your code by Asymptote and I get:

Original file: ~855KB

Gif file: ~1MB

enter image description here

import animate;
settings.tex="pdflatex"; 

animation Ani;

real width=16cm;
real height=8cm;

size(width,height,false);
import graph;

real f(real x){return 2^x;}
real g(real x){return 3^x;}
real h(real x){return 4^x;}
int samples=500;

real xmin=-2.1;
real xmax=4.1;
real ymin=0;
real ymax=17;

for(int a=-5; a<=60;++a)
{
save();
picture pic;
path F=graph(f,-2,-1.5+(9.5*a/100),samples),
     G=graph(g,-2,-1.5+(7.5*a/100),samples),
     H=graph(h,-2,-1.5+(6.5*a/100),samples);

draw(pic,F,red+2.5bp);
draw(pic,G,blue+2.5bp);
draw(pic,H,green+2.5bp);
clip(pic,box((xmin,ymin),(xmax,ymax)));

real marginx=.4, marginy=0.05;
for (real i: new real[]{-2,-1.5,-1,-0.5,0.5,1,1.5,2,2.5,3,3.5,4}){
draw((i,ymin)--(i,ymax),linetype(new real[] {10,10})+gray+opacity(30/100)+0.2bp);
draw(scale(0.8)*Label("$"+(string) i+"$",Relative(0)),(i,0)-(0,marginx)--(i,0));
}
for (int j: new int[]{2,3,4,8,9,12,16}){
draw((xmin,j)--(xmax,j),linetype(new real[] {10,10})+gray+opacity(30/100)+0.2bp);
draw(scale(0.8)*Label("$"+(string) j+"$",Relative(0)),(0,j)-(marginy,0)--(0,j));
}

draw(Label("$x$",EndPoint,LeftSide),(xmin,0)--(xmax,0),Arrow(TeXHead));
draw(Label("$y$",Relative(0.99)),(0,ymin)--(0,ymax),Arrow(TeXHead));
add(pic);
Ani.add();
restore();
}
erase();
Ani.movie(BBox(Fill(white)));

Why do we need picture pic?

-> Because we usually want to display graphs above all.

enter image description here

Answered by user213378 on August 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP