![在 beamer 中生成一个盒子内部算法](https://linux22.com/image/255592/%E5%9C%A8%20beamer%20%E4%B8%AD%E7%94%9F%E6%88%90%E4%B8%80%E4%B8%AA%E7%9B%92%E5%AD%90%E5%86%85%E9%83%A8%E7%AE%97%E6%B3%95.png)
我想创建两个框(或两个框架)和一个箭头,该箭头从一个框的右侧中心出发,画半个圆圈并指向另一个框。
下面是一个代码示例:
\documentclass{beamer}
\usepackage{algorithm,algpseudocode}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xcolor}
\makeatletter
\begin{document}
\begin{frame}
\begin{algorithm}[H]
\begin{algorithmic}
\State $y \leftarrow 1$
% Start box 1
\If{$n < 0$}
\State $X \leftarrow 1 / x$
\State $N \leftarrow -n$
\Else
\State $X \leftarrow x$
\State $N \leftarrow n$
\EndIf
%End box 1
%Start box 2
\While{$N \neq 0$}
\If{$N$ is even}
\State $X \leftarrow X \times X$
\State $N \leftarrow N / 2$
\Else[$N$ is odd]
\State $y \leftarrow y \times X$
\State $N \leftarrow N - 1$
\EndIf
\EndWhile
% End box 2
\end{algorithmic}
\end{algorithm}
\end{frame}
\end{document}
我怎样才能在第一个\If
条件(until \EndIf
)和while 条件周围创建一个框,然后从“if 条件”框右侧的中心到“while 条件”框创建一个箭头?
答案1
有一种可能性是,放置标记,然后使用标记来绘制框和箭头:
\documentclass{beamer}
\usepackage{algorithm,algpseudocode}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,intersections}
\usepackage{xcolor}
\makeatletter
\newcommand\tikzmark[1]{%
\tikz[overlay,remember picture,baseline] \coordinate (#1);}
\begin{document}
\begin{frame}
\begin{algorithm}[H]
\begin{algorithmic}
\State\tikzmark{start1} $y \leftarrow 1$
% Start box 1
\If{$n < 0$}
\State $X \leftarrow 1 / x$
\State $N \leftarrow -n$
\Else
\State $X \leftarrow x$
\State $N \leftarrow n$
\EndIf\tikzmark{end1}
%End box 1
%Start box 2
\Statex\vskip-2.2ex
\tikzmark{start2}\While{$N \neq 0$}
\If{$N$ is even}
\State $X \leftarrow X \times X$
\State $N \leftarrow N / 2$
\Else[$N$ is odd]
\State $y \leftarrow y \times X$
\State $N \leftarrow N - 1$
\EndIf
\EndWhile\tikzmark{end2}
% End box 2
\end{algorithmic}
\end{algorithm}
\begin{tikzpicture}[remember picture,overlay]
\draw[orange] ( $ (start1) + (-2pt,-2pt) $ ) rectangle ( $ (end1) + (1.3,-2pt) $ );
\draw[orange] ( $ (start2) + (-2pt,-2pt) $ ) rectangle ( $ (end2) + (1.8,-2pt) $ );
\draw[orange,-latex] let \p1=(start1), \p2=(end1), \p3=(start2), \p4=(end2)
in ( $ (\x2,0.5*\y1+0.5*\y2) + (1.3,0) $ ) to[out=0,in=0] ( $ (\x4,0.5*\y3+0.5*\y4) + (1.8,0) $ );
\end{tikzpicture}
\end{frame}
\end{document}