我正在使用的代码这个答案在 Beamer 演示中生成围绕某些节点的凸路径。以下是 MWE:
\documentclass[12pt,aspectratio=169]{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\convexpath}[2]{
[
create hullnodes/.code={
\global\edef\namelist{#1}
\foreach [count=\counter] \nodename in \namelist {
\global\edef\numberofnodes{\counter}
\node at (\nodename) [draw=none,name=hullnode\counter] {};
}
\node at (hullnode\numberofnodes) [name=hullnode0,draw=none] {};
\pgfmathtruncatemacro\lastnumber{\numberofnodes+1}
\node at (hullnode1) [name=hullnode\lastnumber,draw=none] {};
},
create hullnodes
]
($(hullnode1)!#2!-90:(hullnode0)$)
\foreach [
evaluate=\currentnode as \previousnode using \currentnode-1,
evaluate=\currentnode as \nextnode using \currentnode+1
] \currentnode in {1,...,\numberofnodes} {
-- ($(hullnode\currentnode)!#2!-90:(hullnode\previousnode)$)
let \p1 = ($(hullnode\currentnode)!#2!-90:(hullnode\previousnode) - (hullnode\currentnode)$),
\n1 = {atan2(\y1,\x1)},
\p2 = ($(hullnode\currentnode)!#2!90:(hullnode\nextnode) - (hullnode\currentnode)$),
\n2 = {atan2(\y2,\x2)},
\n{delta} = {-Mod(\n1-\n2,360)}
in
{arc [start angle=\n1, delta angle=\n{delta}, radius=#2]}
}
-- cycle
}
\begin{document}
\tikzstyle{mynode}=[outer sep=0pt,draw,shape=circle,minimum size=11mm,inner sep=0pt]
\begin{frame}[t]{MWE}
\begin{figure}
\begin{tikzpicture}[scale=1.8]
\node[mynode,fill=red!50] at (0,0) (i1) {$A$};
\node[mynode,fill=blue!50] at (0,-1) (i2) {$B$};
\node[mynode,fill=green!50] at (0,-2) (i3) {$C$};
\node[mynode] at (3,0) (b1) {1};
\node[mynode] at (3,-1) (b2) {2};
\node[mynode] at (3,-2) (b3) {3};
\draw[black,dashed,thick] \convexpath{i1,b1}{10.5pt};
\draw[black,dashed,thick] \convexpath{i1,b3,i3}{12pt};
\draw[black,dashed,thick] \convexpath{i3,b2}{10.5pt};
\draw[black,dashed,thick] \convexpath{i1,b1,i2}{13.5pt};
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}
应该产生如下结果:
现在,我想一次显示一条不同的路径(在不同的幻灯片中),同时让其他路径保持“阴影”。我想过使用\only
,但let \p1
凸路径代码中的“”部分似乎破坏了某些东西,我得到了“未定义的控制序列”错误。我该如何解决这个问题?
答案1
图书馆overlay-beamer-styles
可以发挥重要作用。它有钥匙alt
,你可以用它说
\draw[black,dashed,thick,alt=<2>{opacity=1}{opacity=0.2}] \convexpath{i1,b3,i3}{12pt};
这样路径仅在第 2 帧上才具有完全不透明度。
\documentclass[12pt,aspectratio=169]{beamer}
\usepackage{tikz}
\usetikzlibrary{calc,overlay-beamer-styles}
\newcommand{\convexpath}[2]{
[
create hullnodes={#1}
]
($(hullnode1)!#2!-90:(hullnode0)$)
\foreach [
evaluate=\currentnode as \previousnode using \currentnode-1,
evaluate=\currentnode as \nextnode using \currentnode+1
] \currentnode in {1,...,\numberofnodes} {
-- ($(hullnode\currentnode)!#2!-90:(hullnode\previousnode)$)
let \p1 = ($(hullnode\currentnode)!#2!-90:(hullnode\previousnode) - (hullnode\currentnode)$),
\n1 = {atan2(\y1,\x1)},
\p2 = ($(hullnode\currentnode)!#2!90:(hullnode\nextnode) - (hullnode\currentnode)$),
\n2 = {atan2(\y2,\x2)},
\n{delta} = {-Mod(\n1-\n2,360)}
in
{arc [start angle=\n1, delta angle=\n{delta}, radius=#2]}
}
-- cycle
}
\begin{document}
\tikzset{create hullnodes/.code={
\global\edef\namelist{#1}
\foreach [count=\counter] \nodename in \namelist {
\global\edef\numberofnodes{\counter}
\node at (\nodename) [draw=none,name=hullnode\counter] {};
}
\node at (hullnode\numberofnodes) [name=hullnode0,draw=none] {};
\pgfmathtruncatemacro\lastnumber{\numberofnodes+1}
\node at (hullnode1) [name=hullnode\lastnumber,draw=none] {};
},mynode/.style={outer sep=0pt,draw,shape=circle,minimum size=11mm,inner
sep=0pt}}
\begin{frame}[t]{MWE}
\begin{figure}
\begin{tikzpicture}[scale=1.8]
\node[mynode,fill=red!50] at (0,0) (i1) {$A$};
\node[mynode,fill=blue!50] at (0,-1) (i2) {$B$};
\node[mynode,fill=green!50] at (0,-2) (i3) {$C$};
\node[mynode] at (3,0) (b1) {1};
\node[mynode] at (3,-1) (b2) {2};
\node[mynode] at (3,-2) (b3) {3};
\draw[black,dashed,thick,alt=<1>{opacity=1}{opacity=0.2}] \convexpath{i1,b1}{10.5pt};
\draw[black,dashed,thick,alt=<2>{opacity=1}{opacity=0.2}] \convexpath{i1,b3,i3}{12pt};
\draw[black,dashed,thick,alt=<3>{opacity=1}{opacity=0.2}] \convexpath{i3,b2}{10.5pt};
\draw[black,dashed,thick,alt=<4>{opacity=1}{opacity=0.2}] \convexpath{i1,b1,i2}{13.5pt};
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}