通过latex的方式在pdf中嵌入gif

通过latex实现将gif嵌入到pdf中

准备资源与工具

  • macOS平台,win平台同逻辑
  • homebrew
  • imageMagick [采用homebrew 安装] brew install imageMagick
  • macTeX[采用homebrew安装] brew install mactex
  • visual studio code 或者 sublime Text编辑器 并配置好

操作流程

gif 文件的准备

  • 采用imagemagick将gif转换成png文件,假设my.gif位于 默认下载目录 ~/Downloads目录下,则执行如下命令,
  • convert ~/Downloads/my.gif -coalesce ~/Downloads/outputs/latex_demo.png 其中 my.gif替换为你自己的gif名称即可。
  • 命令执行完毕后,在downloads目录下的outputs目录下就生成了名字为latex_demo-xx.png 的图片

latex 文件的准备

直接复制如下内容,存储为ppt.tex

% !TEX TS-program = xelatex
% !TEX encoding = UTF-8

% This is a simple template for a XeLaTeX document using the "article" class,
% with the fontspec package to easily select fonts.

\documentclass[11pt]{article} % use larger type; default would be 10pt

% 开启下面的设置可以插入中文
\usepackage{xeCJK,fontspec, xunicode, xltxtra}  
% 确认一下mainfont是有的,否则要选一种你有的中文字体
\setCJKmainfont{Hiragino Sans GB}  
\usepackage{color}

\usepackage{fontspec} % Font selection for XeLaTeX; see fontspec.pdf for documentation
\defaultfontfeatures{Mapping=tex-text} % to support TeX conventions like ``---''
\usepackage{xunicode} % Unicode support for LaTeX character names (accents, European chars, etc)
\usepackage{xltxtra} % Extra customizations for XeLaTeX

%\setmainfont{Charis SIL} % set the main body font (\textrm), assumes Charis SIL is installed
%\setsansfont{Deja Vu Sans}
%\setmonofont{Deja Vu Mono}

% other LaTeX packages.....
\usepackage{geometry} % See geometry.pdf to learn the layout options. There are lots.
\geometry{a4paper} % or letterpaper (US) or a5paper or....
%\usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent

% support the \includegraphics command and options
% PDF中插入GIF可以使用包 animate,前提是必须要先包含宏包 graphicx
\usepackage{graphicx}
\usepackage{animate}


\title{测试含gif的pdf}
\author{LinTeX9527}
\date{\today}
%\date{} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed 

\begin{document}
\maketitle

\begin{center}

    \animategraphics[autoplay,
    loop,
    controls,
    width=.7\textwidth]{15}{/Users/yourUserName/Downloads/outputs/latex_demo-}{0}{140}
% 此处的yourUserName要更换你自己的用户名,最后的两个数字编号代表了你想做成gif的起始png文件最后的索引数字
\end{center}


\end{document}

编译完成,用acrobat查看可以播放

发表回复