在 tikZ 中绘制带箭头的框时出现问题

在 tikZ 中绘制带箭头的框时出现问题

有人可以发布一个最小的 tikZ 示例来绘制这个吗?我还想在上面的框内放一个“R”。到目前为止,这是 mi 代码(我知道,它很乱):

\documentclass[tikz]{standalone}
\begin{document}
\tikzstyle{vspecies}=[rectangle, minimum size=0.5cm,draw=black,fill=blue]
\begin{tikzpicture}

\node [vspecies] (I) {I} ;
\node [vspecies, above left of = I] (N) {M} ;
\node [vspecies, below left of = I] (K) {K} ;
\draw [->,thick] (S.south east) --  node {\small{$N$}}  (M) ; ;
\draw [->,thick] (C) --  node [below right]    {\small{$M$}} (M) ;

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

只是为了好玩。

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{vspecies/.style={rectangle, minimum size=0.5cm,draw=blue}}
\begin{tikzpicture}[>=latex]
 \node[draw,minimum width=1cm,minimum height=2cm,
 label=left:$M$,label=above:$N$] (R){$R$};
 \node [vspecies,below left=1cm and 0.2cm of R,minimum height=1.2cm,
 label=left:$M$,label=above:$K$] (R1) {} ;
 \node [vspecies,below right=1cm and 0.2cm of R,minimum width=1.2cm,
 label=left:$K$,label=above:$N$] (R2) {} ;
 \draw [->,thick,shorten >=1mm,shorten <=1mm] (R.south west) -- (R1.north east);
 \draw [->,thick,shorten >=1mm,shorten <=1mm] (R.south east) -- (R2.north west);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

像这样吗?

\documentclass[tikz,margin=1mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=a]
    \node[minimum width=2cm,minimum height=3cm,draw] (main-a) {};
    \path (main-a.west) node[left] {$N$} (main-a.north) node[above] {$M$};
\end{scope}
\begin{scope}[local bounding box=b2]
    \node[minimum width=2cm,minimum height=1cm,draw,below right=of main-a] (main-b2) {};
    \path (main-b2.east) node[right] {$K$} (main-b2.north) node[above] {$M$};
\end{scope}
\begin{scope}[local bounding box=b1]
    \node[minimum width=1cm,minimum height=3cm,draw,below left=of main-a] (main-b1) {};
    \path (main-b1.west) node[left] {$N$} (main-b1.north) node[above] {$K$};
\end{scope}
\draw[-stealth] (main-a.south) -- (b1.north east);
\draw[-stealth] (main-a.south) -- (b2.north west);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

这是一个过度的解决方案,它定义了一个新的pics“形状”(参见 3.1.1 的第 18.2 节蒂克兹手册),以及一些定制鍵盤来设置样式。最终结果是您可以生成:

在此处输入图片描述

使用代码:

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}
\pgfkeys{/vspecies/.is family, /vspecies,
  colour/.initial  = black, % rectangle colour
  fill/.initial    = white, % default fill
  top label/.initial = M,   % label on right
  right label/.initial= N,  % label on left
  bottom label/.initial=,   % label on left
  left label/.initial=,     % label on left
  X/.initial       = 5mm,   % default width
  Y/.initial       = 5mm,   % default height
  shape/.initial   = rectangle % default shape
}

\tikzset{
pics/vspecies/.style args={#1,#2}{% node name, pgfkeys key-value pairs
     code = {
      \pgfkeys{/vspecies, #2}
      \node[\pgfkeysvalueof{/vspecies/shape},
            minimum size=\pgfkeysvalueof{/vspecies/X},
            minimum height=\pgfkeysvalueof{/vspecies/Y},
            draw=\pgfkeysvalueof{/vspecies/colour},
            fill=\pgfkeysvalueof{/vspecies/fill},
            label=east:\pgfkeysvalueof{/vspecies/left label},
            label=north:\pgfkeysvalueof{/vspecies/top label},
            label=south:\pgfkeysvalueof{/vspecies/bottom label},
            label=west:\pgfkeysvalueof{/vspecies/right label},
      ](#1) at (0,0){};
     }
  }
}

\begin{tikzpicture}
  \pic at (0,0) {vspecies={A,colour=red,Y=10mm}};
  \pic[below left=10mm of A] {vspecies={B,top label=K,colour=blue,Y=20mm}};
  \pic[below right=10mm of A] {vspecies={C,fill=blue!10,X=10mm,shape=circle,left label=N,right label=}};
  \draw[->](A.south west)--(B.north east);
  \draw[->](A.south east)--(C.north west);
\end{tikzpicture}

\end{document}

pic命令vspecies接受两个参数:第一个是(普通) tikz 节点标签的名称,第二个参数是用逗号分隔的键值对列表,指定:

  • colour矩形的(默认黑色)
  • 颜色fill(默认白色)
  • top label默认 M)
  • left label默认“”)
  • bottom label默认“”)
  • left label默认 N)
  • 最小X宽度
  • 最低Y高度
  • (默认矩形shape

正如我所说,这太过分了(而且我感到无聊:)

答案4

最终版本:

\documentclass[tikz,margin=1mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\sffamily
\begin{tikzpicture}
\begin{scope}[local bounding box=a]
\node[minimum width=2cm,line width=0.199mm,minimum   height=3cm,draw=black] (main-a) {R};
\path (main-a.west) node[left] {N} (main-a.north) node[above] {M};
\end{scope}
\begin{scope}[local bounding box=b2]
\node[minimum width=2cm,line width=0.199mm,minimum height=1cm,draw=blue,below right=of main-a] (main-b2) {};
\path (main-b2.east) node[right] {K} (main-b2.north) node[above] {M};
\end{scope}
\begin{scope}[local bounding box=b1]
\node[minimum width=1cm,line width=0.199mm,minimum height=3cm,draw=blue,below left=of main-a] (main-b1) {};
\path (main-b1.west) node[left] {N} (main-b1.north) node[above] {K};
\end{scope}
\draw[-stealth] (main-a.south) -- (b1.north east);
\draw[-stealth] (main-a.south) -- (b2.north west);
\end{tikzpicture}
\end{document}

相关内容