需要“graphics”包。将行(动画)错误

需要“graphics”包。将行(动画)错误

我找到了下面的代码并尝试重现它,但出现了以下错误。

在此处输入图片描述

    \documentclass[preview,border={10pt 0pt 10pt 10pt}]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{atom.tex}
\documentclass[tikz,border=20pt]{standalone}
\usepackage{tikz,graphicx}
\usetikzlibrary{decorations.markings,calc}

\definecolor{myyellow}{RGB}{254,241,24}
\definecolor{myorange}{RGB}{234,125,1}

\def\proton(#1,#2){%
    \fill[shade=ball,ball color=myyellow] ({rnd*#1},{rnd*#2}) circle (10pt) node {\texttt{+}};
}
\def\neutron(#1,#2){%
    \fill[shade=ball,ball color=myorange] ({rnd*#1},{rnd*#2}) circle (10pt);
}
\def\electron{%
    \fill[shade=ball,ball color=gray!30] (0,0) circle (5pt);
    \node at (0,0) {\texttt{-}};
}
\def\orbit(#1,#2,#3){%
  \draw[
  color=violet,
  rotate=#1,
  postaction=decorate,
  decoration={markings,pre=moveto,pre length=#3,
  mark=at position {#2} with {\electron},
}]
  (0,0) ellipse (1 and 4);
}
\begin{document}
\foreach \pos in {0,0.08,0.16,...,0.96}{%
\begin{tikzpicture}%
%%Nucleons
\neutron(0.4,0.4)
\proton(0.4,-0.4)
\neutron(-0.4,-0.4)
\neutron(0.4,0.4)
\proton(-0.4,-0.4)
\proton(0.4,0.4)
\proton(-0.4,-0.4)
\proton(0.4,-0.4)
\neutron(-0.4,0.4)
\neutron(-0.4,0.4)
\neutron(0.4,-0.4)
\proton(-0.4,0.4)
%%orbits
\orbit(0,-\pos,0cm)
\orbit(195,\pos,0cm)
\orbit(120,-\pos,0cm)
\orbit(65,\pos,0cm)
\orbit(270,\pos,0cm)
\orbit(330,-\pos,0cm)
\end{tikzpicture}
}

\end{document}
\end{filecontents*}
%
\immediate\write18{pdflatex atom}
%
% convert to GIF animation
\immediate\write18{convert -delay 10 -loop 0 -density 200 -alpha remove atom.pdf atom.gif}
%
% convert to PNG
\makeatletter
\immediate\write18{convert -density 200 -alpha on atom.pdf atom-\@percentchar02d.png}
\makeatother
%
\usepackage{animate}
\begin{document}
\begin{preview}
%\animategraphics[controls,autoplay,loop,scale=<integer>]{<frame rate>}{<PDF filename without extension>}{<left blank>}{<left blank>}
\animategraphics[controls,autoplay,loop,scale=1]{8}{atom}{}{}
\end{preview}
\end{document}

更新:我确实将 graphicx 包添加到主文件中,但现在仍然收到缺失数字错误

       \documentclass[preview,border={10pt 0pt 10pt 10pt}]{standalone}
\usepackage{filecontents}
\usepackage{tikz,graphicx}
\usepackage{animate}
\begin{filecontents*}{atom.tex}
\documentclass[tikz,border=20pt]{standalone}
\usepackage{tikz,graphicx}
\usepackage{animate}
\usetikzlibrary{decorations.markings,calc}

\definecolor{myyellow}{RGB}{254,241,24}
\definecolor{myorange}{RGB}{234,125,1}

\def\proton(#1,#2){%
    \fill[shade=ball,ball color=myyellow] ({rnd*#1},{rnd*#2}) circle (10pt) node {\texttt{+}};
}
\def\neutron(#1,#2){%
    \fill[shade=ball,ball color=myorange] ({rnd*#1},{rnd*#2}) circle (10pt);
}
\def\electron{%
    \fill[shade=ball,ball color=gray!30] (0,0) circle (5pt);
    \node at (0,0) {\texttt{-}};
}
\def\orbit(#1,#2,#3){%
  \draw[
  color=violet,
  rotate=#1,
  postaction=decorate,
  decoration={markings,pre=moveto,pre length=#3,
  mark=at position {#2} with {\electron},
}]
  (0,0) ellipse (1 and 4);
}
\begin{document}
\foreach \pos in {0,0.08,0.16,...,0.96}{%
\begin{tikzpicture}%
%%Nucleons
\neutron(0.4,0.4)
\proton(0.4,-0.4)
\neutron(-0.4,-0.4)
\neutron(0.4,0.4)
\proton(-0.4,-0.4)
\proton(0.4,0.4)
\proton(-0.4,-0.4)
\proton(0.4,-0.4)
\neutron(-0.4,0.4)
\neutron(-0.4,0.4)
\neutron(0.4,-0.4)
\proton(-0.4,0.4)
%%orbits
\orbit(0,-\pos,0cm)
\orbit(195,\pos,0cm)
\orbit(120,-\pos,0cm)
\orbit(65,\pos,0cm)
\orbit(270,\pos,0cm)
\orbit(330,-\pos,0cm)
\end{tikzpicture}
}

\end{document}
\end{filecontents*}
%
\immediate\write18{pdflatex atom}
%
% convert to GIF animation
\immediate\write18{convert -delay 10 -loop 0 -density 200 -alpha remove atom.pdf atom.gif}
%
% convert to PNG
\makeatletter
\immediate\write18{convert -density 200 -alpha on atom.pdf atom-\@percentchar02d.png}
\makeatother
%

\begin{document}
\begin{preview}
%\animategraphics[controls,autoplay,loop,scale=<integer>]{<frame rate>}{<PDF filename without extension>}{<left blank>}{<left blank>}
\animategraphics[controls,autoplay,loop,scale=1]{8}{atom}{}{}
\end{preview}
\end{document}

答案1

原来的问题,报告的缺少包的错误graphicx,已经被OP解决了。

然而,代码还存在更多问题:

filecontents软件包已过时,同名环境现在作为内核内置提供。

TikZ 部分无法在当前的 TeXLive 2022 设置中编译;必须更改为着色命令\shade[ball color=...] ...才能进行编译。

中的动画帧的 GIF 和 PNG 版本atom.pdf不用于生成最终的 PDF 动画。\write18{...}可以安全地删除相应的系统调用。

由于电子穿过椭圆的顶点,第四个动画帧的边界框与其他帧不同。这使得动画在此帧“跳跃”。为了解决这个问题,添加了一些代码来确定所有动画帧上边界框的最大范围,最终将其作为命令写入文件\useasboundingboxaux。在第二次latex运行时,此命令会插入到每个环境中tikzpicture,以确保在所有帧中使用相同的边界框。

事实上,无需先通过 在单独的 PDF 中创建动画帧--shell-escape。相反,可以通过animateinline环境一次性排版和制作动画帧。

动画 SVG 版本:

\documentclass[border=10pt]{standalone}

\usepackage{animate,tikz,graphicx}
\usetikzlibrary{decorations.markings,calc}

\definecolor{myyellow}{RGB}{254,241,24}
\definecolor{myorange}{RGB}{234,125,1}

\def\proton(#1,#2){%
    \shade[ball color=myyellow] ({rnd*#1},{rnd*#2}) circle (10pt) node {\texttt{+}};
}
\def\neutron(#1,#2){%
    \shade[ball color=myorange] ({rnd*#1},{rnd*#2}) circle (10pt);
}
\def\electron{%
    \shade[ball color=gray!30] (0,0) circle (5pt);
    \node at (0,0) {\texttt{-}};
}
\def\orbit(#1,#2,#3){%
  \draw [
  color=violet,
  rotate=#1,
  postaction=decorate,
  decoration={markings,pre=moveto,pre length=#3,
  mark=at position {#2} with {\electron},
}]
  (0,0) ellipse (1 and 4);
}

% starting values for finding largest bounding box
\def\xmin{100000}
\def\ymin{100000}
\def\xmax{-100000}
\def\ymax{-100000}

\begin{document}

\begin{animateinline}[controls,autoplay,loop]{8}
\multiframe{12}{rPos=0+0.08}{  
  \begin{tikzpicture}%
  %insert \useasboundingbox with max bbox coordinates
  \ifdefined\bboxmax\bboxmax\fi
  %%Nucleons
  \neutron(0.4,0.4)
  \proton(0.4,-0.4)
  \neutron(-0.4,-0.4)
  \neutron(0.4,0.4)
  \proton(-0.4,-0.4)
  \proton(0.4,0.4)
  \proton(-0.4,-0.4)
  \proton(0.4,-0.4)
  \neutron(-0.4,0.4)
  \neutron(-0.4,0.4)
  \neutron(0.4,-0.4)
  \proton(-0.4,0.4)
  %%orbits
  \orbit(0,-\rPos,0cm)
  \orbit(195,\rPos,0cm)
  \orbit(120,-\rPos,0cm)
  \orbit(65,\rPos,0cm)
  \orbit(270,\rPos,0cm)
  \orbit(330,-\rPos,0cm)
  %%%%%%%%%%%%%%%%%%%%%% <determine max bbox> %%%%%%%%%%%%%%%%%%%%%%
  \pgfpointanchor{current bounding box}{south west} \pgfgetlastxy{\llx}{\lly}
  \pgfpointanchor{current bounding box}{north east} \pgfgetlastxy{\urx}{\ury}
  \xdef\xmin{\fpeval{\xmin<\llx ? \xmin : \llx}}
  \xdef\ymin{\fpeval{\ymin<\lly ? \ymin : \lly}}
  \xdef\xmax{\fpeval{\xmax>\urx ? \xmax : \urx}}
  \xdef\ymax{\fpeval{\ymax>\ury ? \ymax : \ury}}
  %%%%%%%%%%%%%%%%%%%%%% </determine max bbox> %%%%%%%%%%%%%%%%%%%%%%
  \end{tikzpicture}
}
\end{animateinline}

%%%% <write max bbox command to aux file> %%%%
\makeatletter
\immediate\write\@mainaux{\string\gdef\string\bboxmax{\string\useasboundingbox (\xmin pt,\ymin pt) rectangle (\xmax pt,\ymax pt);}}%
\makeatother
%%%% </write max bbox command to aux file> %%%%

\end{document}

为了完整性,修复版本带有嵌入文档,用于在单独的文件中创建动画帧。要求--shell-escape

\documentclass[border=10pt]{standalone}

\begin{filecontents}[overwrite,noheader]{atom.tex}
\documentclass[tikz]{standalone}

\usetikzlibrary{decorations.markings,calc}

\definecolor{myyellow}{RGB}{254,241,24}
\definecolor{myorange}{RGB}{234,125,1}

\def\proton(#1,#2){%
    \shade[ball color=myyellow] ({rnd*#1},{rnd*#2}) circle (10pt) node {\texttt{+}};
}
\def\neutron(#1,#2){%
    \shade[ball color=myorange] ({rnd*#1},{rnd*#2}) circle (10pt);
}
\def\electron{%
    \shade[ball color=gray!30] (0,0) circle (5pt);
    \node at (0,0) {\texttt{-}};
}
\def\orbit(#1,#2,#3){%
  \draw [
  color=violet,
  rotate=#1,
  postaction=decorate,
  decoration={markings,pre=moveto,pre length=#3,
  mark=at position {#2} with {\electron},
}]
  (0,0) ellipse (1 and 4);
}

% starting values for finding largest bounding box
\def\xmin{100000}
\def\ymin{100000}
\def\xmax{-100000}
\def\ymax{-100000}

\begin{document}

\foreach \pos in {0,0.08,0.16,...,0.96}{%
  \begin{tikzpicture}%
  %insert \useasboundingbox with max bbox coordinates
  \ifdefined\bboxmax\bboxmax\fi
  %%Nucleons
  \neutron(0.4,0.4)
  \proton(0.4,-0.4)
  \neutron(-0.4,-0.4)
  \neutron(0.4,0.4)
  \proton(-0.4,-0.4)
  \proton(0.4,0.4)
  \proton(-0.4,-0.4)
  \proton(0.4,-0.4)
  \neutron(-0.4,0.4)
  \neutron(-0.4,0.4)
  \neutron(0.4,-0.4)
  \proton(-0.4,0.4)
  %%orbits
  \orbit(0,-\pos,0cm)
  \orbit(195,\pos,0cm)
  \orbit(120,-\pos,0cm)
  \orbit(65,\pos,0cm)
  \orbit(270,\pos,0cm)
  \orbit(330,-\pos,0cm)
  %%%%%%%%%%%%%%%%%%%%%% <determine max bbox> %%%%%%%%%%%%%%%%%%%%%%
  \pgfpointanchor{current bounding box}{south west} \pgfgetlastxy{\llx}{\lly}
  \pgfpointanchor{current bounding box}{north east} \pgfgetlastxy{\urx}{\ury}
  \xdef\xmin{\fpeval{\xmin<\llx ? \xmin : \llx}}
  \xdef\ymin{\fpeval{\ymin<\lly ? \ymin : \lly}}
  \xdef\xmax{\fpeval{\xmax>\urx ? \xmax : \urx}}
  \xdef\ymax{\fpeval{\ymax>\ury ? \ymax : \ury}}
  %%%%%%%%%%%%%%%%%%%%%% </determine max bbox> %%%%%%%%%%%%%%%%%%%%%%
  \end{tikzpicture}
}

%%%% <write bbox command to aux file> %%%%
\makeatletter
\immediate\write\@mainaux{\string\gdef\string\bboxmax{\string\useasboundingbox (\xmin pt,\ymin pt) rectangle (\xmax pt,\ymax pt);}}%
\makeatother
%%%% </write box command to aux file> %%%%

\end{document}
\end{filecontents}

% process embedded file for animation frames --> atom.pdf
\immediate\write18{pdflatex atom}

\usepackage{animate}
\usepackage{graphicx}
\begin{document}
%\animategraphics[controls,autoplay,loop,scale=<integer>]{<frame rate>}{<PDF filename without extension>}{<left blank>}{<left blank>}
\animategraphics[controls,autoplay,loop]{8}{atom}{}{}
\end{document}

相关内容