在 beamer 中制作一个盒子——环绕节点

在 beamer 中制作一个盒子——环绕节点

是否可以在 tikzpicture 中的两个节点周围制作一个框?具体来说,我正在制作一个 2 乘 2 的框,框外面有一些标签。例如,我在框的同一行下方有一个 + 和一个 0。我可以围绕它们制作某种圆圈(椭圆)吗?同样,对于框右侧的 0 和 -,在框的外面?这些圆圈都可以延伸到框外右下角的加号吗?

有关代码生成的近似图形,请参见:在 beamer 中制作盒子--沿对角线方向添加标签

非常感谢您的帮助!

\documentclass[professionalfont, fleqn]{beamer}
 \usepackage{tikz}
 \usetikzlibrary{arrows,shapes,positioning,fit,shapes.misc,matrix}

% commands for the cell colors
\def\coloroneone#1{\def\Coloroneone{#1}}
\def\coloronetwo#1{\def\Coloronetwo{#1}}
\def\colortwoone#1{\def\Colortwoone{#1}}
\def\colortwotwo#1{\def\Colortwotwo{#1}}

\begin{document}

\begin{frame}

% initial colors
\coloroneone{blue!60}
\coloronetwo{white}
 \colortwoone{blue!60}
\colortwotwo{white}

\only<1-4>{%
\centering
\begin{tikzpicture}

 % definitions of colors for the four initial slides
 \only<2>{% colors for second slide
  \coloroneone{white}
\coloronetwo{blue!60}
\colortwoone{white} 
\colortwotwo{blue!60}}
 \only<3>{% colors for third slide
 \coloroneone{red!30}
 \coloronetwo{red!30}
 \colortwoone{white}
 \colortwotwo{white}}
 \only<4>{% colors for fourth slide
\coloroneone{white}
 \colortwoone{red!30}
\colortwotwo{red!30}}

 % the matrix
\matrix 
[ 
matrix of nodes,
ampersand
 replacement=\&,
 nodes={draw,rectangle,minimum size=1.0cm,text width=2cm,text depth=1cm,text height=.8cm,align=center,inner sep=0pt},
row sep=0cm,
 column sep=0cm
] (mat)
{
|[fill=\Coloroneone]|  
\& 
|[fill=\Coloronetwo]| Smaller \\
|[fill=\Colortwoone]| Smaller
\& 
|[fill=\Colortwotwo]|  \\
};

 % the labels
\node[left=.5cm of mat,text width=1.5cm,align=center] {\textbf{actual \\ value}};
 \node[above=.5cm of mat] {\textbf{prediction}};

\node[anchor=south] at (mat-1-1.north) {$\mathbf{p}$};
\node[anchor=south] (n) at (mat-1-2.north) {$\mathbf{n}$};
\node[anchor=east] at (mat-1-1.west) {$\mathbf{p'}$};
\node[anchor=east] (np) at (mat-2-1.west) {$\mathbf{n'}$};

\only<1-4>{\node[anchor=north]  at (mat-2-1.south) [below = 2pt+7pt]{$\textbf{{+}}$};}
\only<2-4>{\node[anchor=north]  at (mat-2-2.south) [below = 2pt+7pt]{$\textbf{{0}}$};}

\only<3-4>{\node[anchor=west]  at (mat-1-2.east)  [right = 2pt+10pt]{$\textbf{{0}}$};}
\only<4-4>{\node[anchor=west]  at (mat-2-2.east)  [right = 2pt+10pt]{$\textbf{{--}}$};}

\only<1-4>\node[anchor=north] at ([yshift=-8.75mm, xshift=-2mm]np.south){\textbf{A}};
\only<3-4>\node[anchor=west] at ([xshift=8mm]n.east)  {\textbf{D}};

 \only<4-4>\node[font=\color{red}, anchor=north] at ([yshift=-9.5mm, xshift=51mm]np.south){\textbf{+}};

 \end{tikzpicture}
 }

 \only<5>{Some text}

 \end{frame}

 \end{document}

答案1

只需命名节点并使用该 fit库即可。此外,类选项已过时(请参阅编译后的警告)

\documentclass{beamer}
\usefonttheme{professionalfonts}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,fit,shapes,matrix}

% commands for the cell colors
\def\coloroneone#1{\def\Coloroneone{#1}}
\def\coloronetwo#1{\def\Coloronetwo{#1}}
\def\colortwoone#1{\def\Colortwoone{#1}}
\def\colortwotwo#1{\def\Colortwotwo{#1}}

\begin{document}

\begin{frame}

% initial colors
\coloroneone{blue!60}
\coloronetwo{white}
\colortwoone{blue!60}
\colortwotwo{white}

\only<1-5>{%
\centering
\begin{tikzpicture}

 % definitions of colors for the four initial slides
\only<2>{% colors for second slide
\coloroneone{white}
\coloronetwo{blue!60}
\colortwoone{white} 
\colortwotwo{blue!60}}
\only<3>{% colors for third slide
\coloroneone{red!30}
\coloronetwo{red!30}
\colortwoone{white}
\colortwotwo{white}}
\only<4>{% colors for fourth slide
\coloroneone{white}
\colortwoone{red!30}
\colortwotwo{red!30}}

 % the matrix
\matrix 
[ 
matrix of nodes,
ampersand
 replacement=\&,
 nodes={draw,rectangle,minimum size=1.0cm,text width=2cm,text depth=1cm,text height=.8cm,align=center,inner sep=0pt},
row sep=0cm,
 column sep=0cm
] (mat)
{
|[fill=\Coloroneone]|  
\& 
|[fill=\Coloronetwo]| Smaller \\
|[fill=\Colortwoone]| Smaller
\& 
|[fill=\Colortwotwo]|  \\
};

 % the labels
\node[left=.5cm of mat,text width=1.5cm,align=center] {\textbf{actual \\ value}};
 \node[above=.5cm of mat] {\textbf{prediction}};

\node[anchor=south] at (mat-1-1.north) {$\mathbf{p}$};
\node[anchor=south] (n) at (mat-1-2.north) {$\mathbf{n}$};
\node[anchor=east] at (mat-1-1.west) {$\mathbf{p'}$};
\node[anchor=east] (np) at (mat-2-1.west) {$\mathbf{n'}$};

\only<1->{\node[anchor=north]  (plusnode) at (mat-2-1.south) [below = 2pt+7pt]{$\textbf{{+}}$};}
\only<2->{\node[anchor=north]  (bottomzeronode) at (mat-2-2.south) [below = 2pt+7pt]{$\textbf{{0}}$};}

\only<3->{\node[anchor=west]  (rightzeronode) at (mat-1-2.east)  [right = 2pt+10pt]{$\textbf{{0}}$};}
\only<4->{\node[anchor=west]  (dashednode) at (mat-2-2.east)  [right = 2pt+10pt]{$\textbf{{--}}$};}

\only<1->\node[anchor=north] at ([yshift=-8.75mm, xshift=-2mm]np.south){\textbf{A}};
\only<3->\node[anchor=west] at ([xshift=8mm]n.east)  {\textbf{D}};

\only<4->\node[font=\color{red}, anchor=north] (diagplusnode)at ([yshift=-9.5mm, xshift=51mm]np.south){\textbf{+}};
\only<5->\node[ellipse,fit=(plusnode)(bottomzeronode),draw] {};
\only<5->\node[ellipse,fit=(dashednode)(rightzeronode)(diagplusnode),draw] {};
\end{tikzpicture}
}

\only<6>{Some text}

 \end{frame}

\end{document}

我真的非常建议你仔细阅读beamer和的手册TikZ。简单的例子就能让你对这里给出的概念(如库和其他细节)有足够的了解。

在此处输入图片描述

相关内容