锦绣山河网

Latex中也能展示动态图?

Latex中也能展示动态图?

技术背景

在学术领域,中展示很多文档是动态用Latex做的,甚至有很多人用Latex Beamer来做PPT演示文稿。中展示虽然在易用性和美观等角度来说,动态Latex Beamer很大程度上不如PowerPoint,中展示但是动态Beamer这种浓厚的学术色彩还是受到了很多人的喜好。但是中展示我们知道Latex生成的文档一般是用pdf格式来存储的,如果放一个gif动态图进去,动态就可能会变成一个静态图,中展示那就没办法展示整个动态的动态过程了。幸运的中展示是,Latex还有一种比较麻烦的动态方法,对动态图也做了一定的中展示支持。本文所使用的动态案例,是中展示一个比较热门的猴痘病毒蛋白的松弛过程。碍于动态图比较大,在博客里面放不下,故只能用这种链接索引+静态图展示的方案。

图片拆分

第一步,我们需要使用工具将一个Gif图片拆解成一系列的png图片。在Windows下有很多拆解工具可以使用,需要的童鞋可以自行搜索。但是对于一个Coder而言,用Python来进行拆解,可能是最方便的操作,也是这里我们比较推荐的方案——pillow。我们可以先用pip安装更新一下本地的pillow:

$ python3 -m pip install pillow --upgradeRequirement already satisfied: pillow in /home/dechin/miniconda3/lib/python3.9/site-packages (8.4.0)Collecting pillow  Downloading Pillow-9.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)     ━━━━━━━━━━━━━━━━━━━━━━ 3.1/3.1 MB 2.2 MB/s eta 0:00:00Installing collected packages: pillow  Attempting uninstall: pillow    Found existing installation: Pillow 8.4.0    Uninstalling Pillow-8.4.0:      Successfully uninstalled Pillow-8.4.0Successfully installed pillow-9.2.0

安装完成后,直接在Ipython或者普通python的终端命令行调用即可,如果确实需要比较复杂操作的话,也可以写成脚本来运行:

In [1]: from PIL import ImageIn [2]: import osIn [3]: gif_name = 'Q8V4Y0.gif'In [4]: img = Image.open(gif_name)In [5]: os.mkdir('pngs')In [6]: !ls -ltotal 15836-rwxr-xr-x 1 dechin dechin 16211906 Sep 21 10:11 Q8V4Y0.gifdrwxr-xr-x 2 dechin dechin     4096 Sep 21 10:17 pngsIn [7]: while True:   ...:     current = img.tell()   ...:     img.save('pngs/'+str(current)+'.png')   ...:     img.seek(current+1)   ...:-----------------------------------------------------------EOFError                  Traceback (most recent call last)~/miniconda3/lib/python3.9/site-packages/PIL/GifImagePlugin.py in seek(self, frame)    151             try:-->152                 self._seek(f)    153             except EOFError as e:~/miniconda3/lib/python3.9/site-packages/PIL/GifImagePlugin.py in _seek(self, frame, update_image)    185         if not s or s == b";":-->186             raise EOFError    187EOFError:The above exception was the direct cause of the following exception:EOFError                  Traceback (most recent call last)in 2     current = img.tell()      3     img.save('pngs/'+str(current)+'.png')---->4     img.seek(current+1)      5~/miniconda3/lib/python3.9/site-packages/PIL/GifImagePlugin.py in seek(self, frame)    153             except EOFError as e:    154                 self.seek(last_frame)-->155                 raise EOFError("no more images in GIF file") from e    156    157     def _seek(self, frame, update_image=True):EOFError: no more images in GIF fileIn [8]: !ls -l pngs/total 37276-rw-r--r-- 1 dechin dechin 277605 Sep 21 10:18 0.png-rw-r--r-- 1 dechin dechin 766892 Sep 21 10:18 1.png-rw-r--r-- 1 dechin dechin 770417 Sep 21 10:18 10.png-rw-r--r-- 1 dechin dechin 773093 Sep 21 10:18 11.png-rw-r--r-- 1 dechin dechin 773101 Sep 21 10:18 12.png-rw-r--r-- 1 dechin dechin 768060 Sep 21 10:18 13.png-rw-r--r-- 1 dechin dechin 772558 Sep 21 10:18 14.png-rw-r--r-- 1 dechin dechin 767242 Sep 21 10:18 15.png-rw-r--r-- 1 dechin dechin 768024 Sep 21 10:18 16.png-rw-r--r-- 1 dechin dechin 766089 Sep 21 10:18 17.png-rw-r--r-- 1 dechin dechin 771081 Sep 21 10:18 18.png-rw-r--r-- 1 dechin dechin 768937 Sep 21 10:18 19.png-rw-r--r-- 1 dechin dechin 771098 Sep 21 10:18 2.png-rw-r--r-- 1 dechin dechin 768942 Sep 21 10:18 20.png-rw-r--r-- 1 dechin dechin 774666 Sep 21 10:18 21.png-rw-r--r-- 1 dechin dechin 769060 Sep 21 10:18 22.png-rw-r--r-- 1 dechin dechin 768860 Sep 21 10:18 23.png-rw-r--r-- 1 dechin dechin 774749 Sep 21 10:18 24.png-rw-r--r-- 1 dechin dechin 769303 Sep 21 10:18 25.png-rw-r--r-- 1 dechin dechin 770692 Sep 21 10:18 26.png-rw-r--r-- 1 dechin dechin 773657 Sep 21 10:18 27.png-rw-r--r-- 1 dechin dechin 772078 Sep 21 10:18 28.png-rw-r--r-- 1 dechin dechin 769908 Sep 21 10:18 29.png-rw-r--r-- 1 dechin dechin 771034 Sep 21 10:18 3.png-rw-r--r-- 1 dechin dechin 771412 Sep 21 10:18 30.png-rw-r--r-- 1 dechin dechin 771772 Sep 21 10:18 31.png-rw-r--r-- 1 dechin dechin 771312 Sep 21 10:18 32.png-rw-r--r-- 1 dechin dechin 771232 Sep 21 10:18 33.png-rw-r--r-- 1 dechin dechin 768273 Sep 21 10:18 34.png-rw-r--r-- 1 dechin dechin 772945 Sep 21 10:18 35.png-rw-r--r-- 1 dechin dechin 775270 Sep 21 10:18 36.png-rw-r--r-- 1 dechin dechin 772346 Sep 21 10:18 37.png-rw-r--r-- 1 dechin dechin 770087 Sep 21 10:18 38.png-rw-r--r-- 1 dechin dechin 768827 Sep 21 10:18 39.png-rw-r--r-- 1 dechin dechin 771078 Sep 21 10:18 4.png-rw-r--r-- 1 dechin dechin 771802 Sep 21 10:18 40.png-rw-r--r-- 1 dechin dechin 773822 Sep 21 10:18 41.png-rw-r--r-- 1 dechin dechin 770597 Sep 21 10:18 42.png-rw-r--r-- 1 dechin dechin 771889 Sep 21 10:18 43.png-rw-r--r-- 1 dechin dechin 774457 Sep 21 10:18 44.png-rw-r--r-- 1 dechin dechin 771072 Sep 21 10:18 45.png-rw-r--r-- 1 dechin dechin 773136 Sep 21 10:18 46.png-rw-r--r-- 1 dechin dechin 771072 Sep 21 10:18 47.png-rw-r--r-- 1 dechin dechin 771411 Sep 21 10:18 48.png-rw-r--r-- 1 dechin dechin 773853 Sep 21 10:18 49.png-rw-r--r-- 1 dechin dechin 771607 Sep 21 10:18 5.png-rw-r--r-- 1 dechin dechin 771951 Sep 21 10:18 6.png-rw-r--r-- 1 dechin dechin 774519 Sep 21 10:18 7.png-rw-r--r-- 1 dechin dechin 768478 Sep 21 10:18 8.png-rw-r--r-- 1 dechin dechin 769771 Sep 21 10:18 9.png

这个运行的过程中有一处报错,但是可以不用在意,有条件的童鞋可以加一个try-except即可。而这里我们最终是在指定路径下成功生成了一系列的png图片文件,接下来可以考虑放到Latex里面去。

Latex加载动态图

先看一下加载这个动态图的Latex案例,其实非常简单,就是用animate来加载一系列带数字的png图片,用来动态展示:

\documentclass{ beamer}\usepackage{ graphicx} % Allows including images\usepackage{ animate}\begin{ document}\animategraphics[width=0.5\linewidth, autoplay=True]{ 24}{ imgs/}{ 0}{ 49}\end{ document}

其中imgs/需要替换为你自己的图片存储路径,{ 0}{ 49}是图片后缀编号,我这里Gif拆完之后是有50张,width=0.5\linewidth是用来调整图片大小的,一般调节这几个参数就够了。不过,为了避免Latex使用中可能出现的各种环境依赖问题,推荐测试的时候使用Overleaf自带的环境,这样可以避免很多的麻烦:

这里还有一个问题是,不是所有的pdf阅读器都支持这样的插件,即使是生成了动态图,在一部分的阅读器下可能也无法正常播放,推荐使用Adobe Acrobat Reader DC,具体的安装方法这里就不过多的介绍,大家可以自行搜索。

打开之后是可以正常播放的,这里还是放了一个截图用来展示功能。还有一个比较酷的功能是,Latex的animate还可以附带一些控制组件:

\documentclass{ beamer}\usepackage{ graphicx} % Allows including images\usepackage{ animate}\begin{ document}\animategraphics[width=0.5\linewidth, autoplay=True, loop, controls]{ 24}{ imgs/}{ 0}{ 49}\end{ document}

加上控制组件之后,不仅可以循环播放,还可以暂停、逐帧控制、加速、慢放、回放等等,依然是先用Overleaf的环境进行编译:

然后下载到本地使用Acrobat Reader打开:

虽然比PowerPoint麻烦了许多,但是Latex也能够实现这样的功能,还是很让人欣慰的。

总结概要

本文介绍了一个可以在Latex生成的PDF中展示动态图的解决方案,该方案依赖于三个东西:Python+Pillow的环境(或者其他可以将Gif拆分成众多png图片的工具/网站)、Overleaf的账号(或者具备animate的Latex环境)以及Acrobat Reader阅读器(或者其他可以支持pdf中的动态演示功能的阅读器)。虽然一系列的操作下来非常的麻烦,但是最终我们还是可以在Latex生成的PDF中成功的进行了动态展示。

版权声明

本文首发链接为:https://www.cnblogs.com/dechinphy/p/latex-gif.html

作者ID:DechinPhy

更多原著文章请参考:https://www.cnblogs.com/dechinphy/

打赏专用链接:https://www.cnblogs.com/dechinphy/gallery/image/379634.html

腾讯云专栏同步:https://cloud.tencent.com/developer/column/91958

CSDN同步链接:https://blog.csdn.net/baidu_37157624?spm=1008.2028.3001.5343

51CTO同步链接:https://blog.51cto.com/u_15561675

参考链接

  1. https://www.cnblogs.com/easyidea/p/10520354.html
  2. https://liam.page/2017/08/10/importing-animate-in-LaTeX/

未经允许不得转载:锦绣山河网 » Latex中也能展示动态图?