我有两个范围,我想从一个范围到另一个范围画一个箭头。
我希望红色箭头是水平的,而不是像现在这样倾斜的。
它是倾斜的,因为节点n
比节点“低” m
,因此它在垂直维度上扩展。
有人对如何使红色箭头变为水平有什么建议吗?
\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=scope1]
\node [anchor=base east] (m) {m};
\filldraw (0,0) circle (1pt);
\draw (0,0) rectangle (3,2);
\end{scope}
\begin{scope}[local bounding box=scope2,shift={(scope1.base east)},xshift=2cm]
\filldraw (0,0) circle (1pt);
\draw (0,0) rectangle (4,2);
\node [anchor=north] (n) {n};
\end{scope}
\draw [->,red] (scope1.east) -- (scope2.west);
\end{tikzpicture}
\end{document}
答案1
像这样:
\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=scope1]
\node [anchor=base east] (m) {m};
\filldraw (0,0) circle (1pt);
\draw (0,0) rectangle (3,2);
\end{scope}
\begin{scope}[local bounding box=scope2,shift={(scope1.base east)},xshift=2cm]
\filldraw (0,0) circle (1pt);
\draw (0,0) rectangle (4,2);
\node [anchor=north] (n) {n};
\end{scope}
\draw [->,red] (scope1.east) -- (scope1.east -| scope2.west);
\end{tikzpicture}
\end{document}
附录:
- 目前还不完全清楚使用
scope
环境的提议 - 获取图像的代码更简单,无需使用它们,而是使用节点而不是绘制矩形:
\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 0mm and 20mm,
dot/.style = {circle, fill, inner sep=2pt, label=#1},
N/.style = {rectangle, draw, minimum height=20mm, minimum width=30mm}
]
\node [N,anchor=base east] (m) {};
\node [dot=left:$m$] at (m.south west) {};
%
\node [N,right=of m] (n) {};
\node [dot=below:$n$] at (n.south west) {};
%
\draw [-Straight Barb,red] (m) -- (n);
\end{tikzpicture}
\end{document}
现在箭头在第二个矩形前没有间隙。如果应该有间隙,只需将箭头页面缩短一点。例如:
\draw [-Straight Barb,red, shorten >=5pt] (m) -- (n);
答案2
像这样:
\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=scope1]
\node [anchor=base east] (m) {m};
\filldraw (0,0) circle (1pt);
\draw (0,0) rectangle (3,2);
\end{scope}
\begin{scope}[local bounding box=scope2,shift={(scope1.base east)},xshift=2cm]
\filldraw (0,0) circle (1pt);
\draw (0,0) rectangle (4,2);
\node [anchor=base west] (n) {n};
\end{scope}
\draw [->,red] (scope1.east) -- (scope2.west);
\end{tikzpicture}
\end{document}
输出: