![使用 TikZ 时如何控制背景的高度和宽度?](https://linux22.com/image/310774/%E4%BD%BF%E7%94%A8%20TikZ%20%E6%97%B6%E5%A6%82%E4%BD%95%E6%8E%A7%E5%88%B6%E8%83%8C%E6%99%AF%E7%9A%84%E9%AB%98%E5%BA%A6%E5%92%8C%E5%AE%BD%E5%BA%A6%EF%BC%9F.png)
\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}