节点大小计算不符合预期

节点大小计算不符合预期

我试图将两个相似的形状并排放置,并在两者之间放置一个恰好相同的形状。但相反,这两个形状(AB)重叠。minimum size这里似乎忽略了 (仅用于测试)。我猜这与嵌入的 有关tikzpicture

\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{calc}
\begin{document}
\def\bracket{
  \draw[-] (1,0)--(0,0);
  \draw[-] (0,0)--(0,10);
  \draw[-] (0,10)--(1,10);

  \draw[-] (9,10)--(10,10);
  \draw[-] (10,10)--(10,0);
  \draw[-] (10,0)--(9,0);
}
\begin{tikzpicture}
\tikzstyle{every path}=[draw, line width=0.3mm];
\coordinate (v1) at (1,1) {};
\coordinate (v2) at (9,1) {};
\coordinate (v3) at (9,9) {};
\coordinate (v4) at (1,9) {};

\node (A) [minimum size=4cm]  {
  \begin{tikzpicture}
    \fill [opacity=0.9,gray] (v2) \foreach \i in {3,4,1}{ -- (v\i) } -- cycle;
    \bracket
  \end{tikzpicture}
} ;
\node (B) [right of=A,minimum size=4cm]  {
  \begin{tikzpicture}
    \fill [opacity=0.9,gray] (v2) \foreach \i in {3,4}{ -- (v\i) } -- cycle;
    \bracket
  \end{tikzpicture}
} ;
%\node (C) [right of=B] {
%  \begin{tikzpicture}
%    \fill [opacity=0.9,gray] (v2) \foreach \i in {4,1}{ -- (v\i) } -- cycle;
%    \bracket
%  \end{tikzpicture}
%} ;
\coordinate (AB) ($(A)!0.5!(B)$);
\node (a) at (AB) { \Large = };

%\coordinate (BC) ($(B)!0.5!(C)$);
%\node (a) at (BC) { \Large X };

\end{tikzpicture}
\end{document}

答案1

您不应嵌套tikzpictures。而且没有必要这样做,您只需使用 即可pics

\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{calc,positioning}
\def\bracket{
  \draw[-] (1,0)--(0,0);
  \draw[-] (0,0)--(0,10);
  \draw[-] (0,10)--(1,10);

  \draw[-] (9,10)--(10,10);
  \draw[-] (10,10)--(10,0);
  \draw[-] (10,0)--(9,0);
}
\tikzset{pics/.cd,
  my pic/.style={
    code={\coordinate (-v1) at (1,1);
        \coordinate (-v2) at (9,1);
        \coordinate (-v3) at (9,9);
        \coordinate (-v4) at (1,9);
        \fill [opacity=0.9,gray] (-v2) \foreach \i in {3,4,1}{ -- (-v\i) } -- cycle;
        \bracket
    }}}

\begin{document}

\begin{tikzpicture}[every path/.style={draw, line width=0.3mm}]
\pic[local bounding box=A]  at (0,0) {my pic};
\pic[local bounding box=B,right=4cm of A.south east] {my pic};
\pic[local bounding box=C,right=4cm of B.south east] {my pic};
\node[scale=3] (a) at ($(A.east)!0.5!(B.west)$) {$=$};
\node[scale=3] (b) at ($(B.east)!0.5!(C.west)$) {$\times$};
\end{tikzpicture}
\end{document}

enter image description here

当然,可以通过允许将参数传递给 s 来简化这一点并使其更加灵活pic

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc,positioning}
\tikzset{pics/.cd,
  my pic/.style={
    code={\coordinate (-v1) at (1,1);
        \coordinate (-v2) at (9,1);
        \coordinate (-v3) at (9,9);
        \coordinate (-v4) at (1,9);
        \fill [#1] (-v1) rectangle (-v3);
        \draw[-] (1,10) -| (0,0) -- (1,0);
        \draw[-] (9,10) -| (10,0) -- (9,0);
    }}}

\begin{document}

\begin{tikzpicture}[every path/.style={draw, line width=0.3mm}]
\pic[local bounding box=A]  at (0,0) {my pic={opacity=0.9,gray}};
\pic[local bounding box=B,right=4cm of A.south east] {my pic={opacity=0.9,gray}};
\pic[local bounding box=C,right=4cm of B.south east] {my pic={opacity=0.9,gray}};
\node[scale=3] (a) at ($(A.east)!0.5!(B.west)$) {$=$};
\node[scale=3] (b) at ($(B.east)!0.5!(C.west)$) {$\times$};
\end{tikzpicture}
\end{document}

当然,您也可以使用带有path pictures 的节点,在这种情况下定位会更容易一些,并且您可以向节点添加一些内容并使用minimum size

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc,positioning}
\tikzset{square with brackets/.style={minimum size=10cm,path picture={
 \fill[#1] ($(path picture bounding box.south west)!0.9!(path picture bounding
 box.north east)$) rectangle
 ($(path picture bounding box.north east)!0.9!(path picture bounding
 box.south west)$);
 \draw[ultra thick] 
 ($(path picture bounding box.south west)!0.1!(path picture bounding
 box.south east)$) -| (path picture bounding box.north west) --
 ($(path picture bounding box.north west)!0.1!(path picture bounding
 box.north east)$);
 \draw[ultra thick] 
 ($(path picture bounding box.south west)!0.9!(path picture bounding
 box.south east)$) -| (path picture bounding box.north east) --
 ($(path picture bounding box.north west)!0.9!(path picture bounding
 box.north east)$);
}}}

\begin{document}
\begin{tikzpicture}[every path/.style={draw, line width=0.3mm}]
\node[square with brackets={opacity=0.9,gray}] (A){};
\node[square with brackets={opacity=0.9,gray},right=4cm of A] (B){};
\node[square with brackets={opacity=0.9,gray},right=4cm of B] (C){};
\node[scale=3] (a) at ($(A.east)!0.5!(B.west)$) {$=$};
\node[scale=3] (b) at ($(B.east)!0.5!(C.west)$) {$\times$};
\end{tikzpicture}
\end{document}

enter image description here

相关内容