使用 TikZ 时如何控制背景的高度和宽度?

使用 TikZ 时如何控制背景的高度和宽度?
\documentclass{beamer}
\usepackage{tikz}                          
\usetikzlibrary{backgrounds,positioning,fit}
\begin{document}
\begin{frame}
\begin{figure}
\begin{tikzpicture}[scale=0.5]
\tikzstyle{vv}=[semithick, draw, circle, inner sep=0.5pt]
\tikzstyle{wvv}=[semithick, draw, circle, inner sep=0.5pt, white]
\tikzstyle{bl}=[xshift=0.6cm]
\tikzstyle{br}=[xshift=-0.6cm]
\node [vv] (1) at (0,0) {1};
\node [vv] (2) [right=of 1] {2};
\node [vv] (3) [right=of 2] {3};
\node [vv] (5) [below left=of 3, bl] {5};
\node [vv] (4) [below right=of 1, br] {4};
\node [vv] (6) [below left=of 4, bl] {6};
\node [vv] (7) [right=of 6] {7};
\node [vv] (8) [right=of 7] {8};

\draw [semithick] (1)--(4)--(2)--(5)--(3);
\draw [semithick] (1)--(5)--(6);
\draw [semithick] (6)--(4)--(7)--(5)--(8);

\begin{scope}[on background layer]
\node (b0) [fill=black!20,rounded corners,fit=(1)(2)(3)(4)(5)(6)(7)(8), inner sep=-1.3cm] {};
\end{scope}

\begin{scope}[xshift=10cm]
\node [vv] (21) at (0,0) {2};
\node [vv] (31) [right=of 21] {3};
\node [vv] (51) [below left=of 31, bl] {5};
\node [vv] (41) [below left=of 21, bl] {4};
\node [vv] (71) [below left=of 51, bl] {7};
\node [vv] (81) [below right=of 51, br] {8};

\draw [semithick] (41)--(21)--(51)--(31);
\draw [semithick] (71)--(51)--(81);
\end{scope}

\begin{scope}[on background layer]
\node (b1) [fill=black!20,rounded corners,fit=(21)(31)(41)(51)(71)(81), inner sep=-0.8cm] {};
\end{scope}


\begin{scope}[xshift=15.5cm]
\node [vv] (12) at (0,0) {1};
\node [vv] (42) [below right=of 12, br] {4};
\node [vv] (52) [right=of 42] {5};
\node [vv] (62) [below left=of 42, bl] {6};
\node [vv] (72) [right=of 62] {7};

\draw [semithick] (62)--(42)--(12)--(52)--(62);
\draw [semithick] (42)--(72);
\end{scope}

\begin{scope}[on background layer]
\node (b2) [fill=black!20,rounded corners,fit=(12)(42)(52)(62)(72), inner sep=-1.1cm] {};
\end{scope}

\path (b0) to node {=} (b1);
\path (b1) to node {+} (b2);

\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}

我使用这段代码,效果如下:

在此处输入图片描述

我们可以看到,这三个背景的高度是不同的!如何控制这些背景的宽度和高度,以使其具有相同的高度和相同的内部分隔?

答案1

我认为你的系统出了问题。使用节点inner sep的默认设置fit,我得到的结果如下:

在此处输入图片描述

我正在使用 MikTeX 2.9 及其最新pgf版本beamer

\documentclass{beamer}
\usepackage{tikz}                          
\usetikzlibrary{backgrounds,positioning,fit}
\begin{document}
\begin{frame}
\begin{figure}
\begin{tikzpicture}[scale=0.5]
\tikzstyle{vv}=[semithick, draw, circle, inner sep=0.5pt]
\tikzstyle{wvv}=[semithick, draw, circle, inner sep=0.5pt, white]
\tikzstyle{bl}=[xshift=0.6cm]
\tikzstyle{br}=[xshift=-0.6cm]
\node [vv] (1) at (0,0) {1};
\node [vv] (2) [right=of 1] {2};
\node [vv] (3) [right=of 2] {3};
\node [vv] (5) [below left=of 3, bl] {5};
\node [vv] (4) [below right=of 1, br] {4};
\node [vv] (6) [below left=of 4, bl] {6};
\node [vv] (7) [right=of 6] {7};
\node [vv] (8) [right=of 7] {8};

\draw [semithick] (1)--(4)--(2)--(5)--(3);
\draw [semithick] (1)--(5)--(6);
\draw [semithick] (6)--(4)--(7)--(5)--(8);

\begin{scope}[on background layer]
\node (b0) [fill=black!20,rounded corners,fit=(1)(2)(3)(4)(5)(6)(7)(8)] {};
\end{scope}

\begin{scope}[xshift=10cm]
\node [vv] (21) at (0,0) {2};
\node [vv] (31) [right=of 21] {3};
\node [vv] (51) [below left=of 31, bl] {5};
\node [vv] (41) [below left=of 21, bl] {4};
\node [vv] (71) [below left=of 51, bl] {7};
\node [vv] (81) [below right=of 51, br] {8};

\draw [semithick] (41)--(21)--(51)--(31);
\draw [semithick] (71)--(51)--(81);
\end{scope}

\begin{scope}[on background layer]
\node (b1) [fill=black!20,rounded corners,fit=(21)(31)(41)(51)(71)(81)] {};
\end{scope}


\begin{scope}[xshift=15.5cm]
\node [vv] (12) at (0,0) {1};
\node [vv] (42) [below right=of 12, br] {4};
\node [vv] (52) [right=of 42] {5};
\node [vv] (62) [below left=of 42, bl] {6};
\node [vv] (72) [right=of 62] {7};

\draw [semithick] (62)--(42)--(12)--(52)--(62);
\draw [semithick] (42)--(72);
\end{scope}

\begin{scope}[on background layer]
\node (b2) [fill=black!20,rounded corners,fit=(12)(42)(52)(62)(72)] {};
\end{scope}

\path (b0) to node {=} (b1);
\path (b1) to node {+} (b2);

\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}

相关内容