如您所见,我尝试从外部节点向内部节点添加箭头。如何才能将箭头指向内部节点的正确位置(西和南)?
梅威瑟:
\documentclass[border=1pt, tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[draw=black, scale=1, transform shape
, every node/.style = {rectangle, draw=black, align=center, inner xsep=6mm, inner ysep=3mm}
]
% change default arrow style
\tikzset{very thick, ->, -latex, shorten <=0pt, shorten >=0pt}
% nodes
\node (outer) {
Caption\\[3mm]
\begin{tikzpicture}[align=center]
\node (inner) {Inner Text};
\end{tikzpicture}
};
\node [above left=1mm and 10mm of inner.west] (leftabove) {left\\above};
\node [below left=1mm and 10mm of inner.west] (leftbelow) {left\\below};
% connections
\draw (leftabove.east) to (outer.west);
\draw (leftbelow.east) to (inner.west);
\draw (leftbelow.east) to (inner.south);
\end{tikzpicture}
\end{document}
答案1
正如 Alenanno 所解释的那样,嵌套 不是一个好主意tikzpictures
。如果要嵌套节点,有几种选择。其中一种如下面的代码所示。它使用fit
库来定义一个包含inner
节点及其 的节点label
。(我已将其声明Caption
为inner
标签,尽管它可能是另一个未绘制的节点)。
\documentclass[border=1pt, tikz]{standalone}
\usetikzlibrary{positioning, fit}
\begin{document}
\begin{tikzpicture}
[mynode/.style = {rectangle, draw, align=center,
inner xsep=6mm, inner ysep=3mm},
-latex,
]
% nodes
\node[mynode, label={[name=lab]Caption}] (inner) {Inner text};
\node[fit={(inner) (lab)}, draw] (outer) {};
\node[mynode, above left=1mm and 10mm of inner.west] (leftabove) {left\\above};
\node[mynode, below left=1mm and 10mm of inner.west] (leftbelow) {left\\below};
% connections
\draw (leftabove.east) to (outer.west);
\draw (leftbelow.east) to (inner.west);
\draw (leftbelow.east) to (inner.south);
\draw (leftabove.east) to (lab);
\end{tikzpicture}
\end{document}
答案2
像这样 :
\documentclass[border=1pt, tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[draw=black, scale=1, transform shape
, every node/.style = {rectangle, draw=black, align=center, inner xsep=6mm, inner ysep=3mm}
]
% change default arrow style
\tikzset{very thick, ->, -latex, shorten <=0pt, shorten >=0pt}
% nodes
% \node (outer) {
% Caption\\[3mm]
% \begin{tikzpicture}[align=center]
% \node (inner) {Inner Text};
% \end{tikzpicture}
% };
\node[name=outer, minimum width = 4cm, minimum height= 2cm] {};
\node[name=caption, below=(2mm of outer.north), draw=white, inner sep=0pt] {Caption};
\node[name=inner, above=(2mm of outer.south)] {Inner Text};
\node [above left=1mm and 10mm of inner.west] (leftabove) {\shortstack{left\\above}};
\node [below left=1mm and 10mm of inner.west] (leftbelow) {\shortstack{left\\below}};
% connections
\draw (leftabove.east) to (outer.west);
\draw (leftbelow.east) to (inner.west);
\draw (leftbelow.east) to (inner.south);
\end{tikzpicture}
\end{document}