我正在学习 animate 包的基础知识。我想在一个简单的图像中插入一个表示时间的计数器。这是我使用的代码。
\documentclass[12pt]{article}
\usepackage[italian]{babel}
\usepackage{tikz}
\usepackage[controls]{animate}
%=====================
\begin{document}
%=====================
\begin{figure}
\begin{animateinline}
[
loop,
controls,
begin={
\begin{tikzpicture}
\useasboundingbox (-3,-3) rectangle (10, 5);
},
end={\end{tikzpicture}}
]{10}
\multiframe {40}{n=0+0.05} % andata
{
\draw[very thick](-2,0) -- (-1,0); %linee
\draw[very thick](-2,2) -- (-1,2);
%
\draw[->,very thick](0,0) -- (9,0); %assi
\draw[->,very thick](0,-2) -- (0,4);
\filldraw [red] (-1.5,0+\n) circle (1pt) ; %fotone
\draw[dashed,red] (-1.5,0+\n)--(0+\n,0+\n); %proiezione
\draw [thick,red] (0,0)--(0+\n,0+\n); %grafico
%%counter
\node [thick, black] at (1,-1){$t=$};
\node [thick, black] at (2,-1){$\n$};
}
%
\multiframe {40}{n=0+0.05} %ritorno
{
\draw[very thick](-2,0) -- (-1,0); %linee
\draw[very thick](-2,2) -- (-1,2);
%
\draw[->,very thick](0,0) -- (9,0); %assi
\draw[->,very thick](0,-2) -- (0,4);
\draw [red] (0,0)--(2,2);
\filldraw [blue] (-1.5,2-\n) circle (1pt) ; %fotone
\draw[dashed,blue] (-1.5,2-\n)--(2+\n,2-\n); %proiezione
\draw [thick,blue] (2,2)--(2+\n,2-\n); % grafico
}
\end{animateinline}
%
\end{figure}
%======================
\end{document}
%======================
使用此解决方案,表示时间的数字的位数会在任何帧中全部改变,从而产生闪烁的怪异效果,并且难以读取。所以我的问题是:有没有办法让计数器只改变变化的数字,就像数字计时器一样?
答案1
这个问题更多的是关于数字格式。该siunitx
包非常适合此目的,因为它允许将数字四舍五入到有效的小数位并正确排版物理单位。
例如带单位的数字:
$t=\SI[round-mode=places,round-precision=2]{123.4567}{\second}$
应显示为“吨=123.46秒”。
或者不带单位:
$t=\num[round-mode=places,round-precision=2]{123.4567}$
关于给定代码的一个animate
相关建议是总是\multiframe
用 a分隔两个后续块\newframe
(如animate
文档中某处所述)。
\documentclass[12pt]{article}
%\usepackage[italian]{babel}
\usepackage{tikz}
\usepackage[controls]{animate}
\usepackage{siunitx}
%=====================
\begin{document}
%=====================
\begin{figure}
\begin{animateinline}
[
loop,
begin={
\begin{tikzpicture}
\useasboundingbox (-3,-3) rectangle (10, 5);
},
end={\end{tikzpicture}}
]{10}
\multiframe {40}{n=0+0.05} % andata
{
\draw[very thick](-2,0) -- (-1,0); %linee
\draw[very thick](-2,2) -- (-1,2);
%
\draw[->,very thick](0,0) -- (9,0); %assi
\draw[->,very thick](0,-2) -- (0,4);
\filldraw [red] (-1.5,0+\n) circle (1pt) ; %fotone
\draw[dashed,red] (-1.5,0+\n)--(0+\n,0+\n); %proiezione
\draw [thick,red] (0,0)--(0+\n,0+\n); %grafico
%counter
\node [thick, black] at (1,-1){$t=\SI[round-mode=places,round-precision=2]{\n}{\second}$};
}
\newframe% required between two \multiframe blocks
\multiframe {40}{n=0+0.05, nTime=2+0.05} %ritorno
{
\draw[very thick](-2,0) -- (-1,0); %linee
\draw[very thick](-2,2) -- (-1,2);
%
\draw[->,very thick](0,0) -- (9,0); %assi
\draw[->,very thick](0,-2) -- (0,4);
\draw [red] (0,0)--(2,2);
\filldraw [blue] (-1.5,2-\n) circle (1pt) ; %fotone
\draw[dashed,blue] (-1.5,2-\n)--(2+\n,2-\n); %proiezione
\draw [thick,blue] (2,2)--(2+\n,2-\n); % grafico
%counter
\node [thick, black] at (1,-1){$t=\SI[round-mode=places,round-precision=2]{\nTime}{\second}$};
}
\end{animateinline}
%
\caption{\dots}
\end{figure}
%======================
\end{document}
%======================