我很难调整 TikZ 节点内箭头线的起始坐标:
当我尝试通过减去 0.1 使位于节点中点下方的最后一个箭头的起始垂直坐标在垂直方向上稍微高一点时,如下所示:
\draw[*-{Stealth},very thick] let \p1 = (B.two), \p2 = (B.center) in (\x1+\nodewidth * 0.10, \y2 -0.1) |- (C);
起点跳到中点之上:
我尝试了其他几个选项,但仍然无法将起点定位到节点的中点。
代码有点长,但编译得很好。
% compile with lualatex
\documentclass[a4paper,openany,14pt]{extbook}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart,chains,positioning,arrows,calc}
\usetikzlibrary{arrows.meta}
\tikzset{*->stealth}
\usetikzlibrary{fit}
\usepackage{xparse}
\makeatletter % get the dimensions of the box (node) which was formed prior
\NewDocumentCommand {\getnodedimen} {O {\nodewidth} O {\nodeheight} m} {
\begin{pgfinterruptboundingbox}
\begin{scope}[local bounding box=bb@temp]
\node[inner sep=0pt, fit=(#3)] {};
\end{scope}
\path ($([email protected] east)-([email protected] west)$);
\end{pgfinterruptboundingbox}
\pgfgetlastxy{#1}{#2} }
\makeatother
\usepackage{fontspec}
\usetikzlibrary{matrix}
\newcommand{\columnratio}{0.62}
\newcommand{\columnseper}{12pt}
\begin{document}
\pgfmathsetlengthmacro{\ourwidth}{\columnratio * \linewidth - \columnseper}
\tikzset{kutu/.style = {rectangle split, rectangle split horizontal,rectangle split parts=2, very thick,draw=black!100, align=center,font=\footnotesize, text width=0.047 * \ourwidth,minimum width=0.040 * \ourwidth, text height=0.023 * \ourwidth,text depth=2.7pt,inner xsep=0.27cm,}} % kutu is the turkish word for "box"
\noindent\begin{tikzpicture}[start chain=1 going right,start chain=2 going right]
\tikzset{every node}=[font=\small\ttfamily]
\node[rectangle, on chain=1,inner xsep=0.05cm,](ORIG){\footnotesize 1511};
\node[kutu,on chain=1,right=0.0cm of ORIG] (A)
{\nodepart[]{one}1111 \nodepart[]{two}};
\node[kutu, on chain=1,right=0.4cm of A,] (B)
{\nodepart[]{one} \nodepart[]{two}};
\node[kutu, on chain=1,right=0.4cm of B] (C)
{\nodepart[]{one}2111 \nodepart[]{two}};
\node[kutu, on chain=2,below=0.8cm of B] (D) at (6.0, -0.5)
{\nodepart[]{one}3422 \nodepart[]{two}};
\getnodedimen{A} % we extract the dimensions of the box we just formed, in order to position
% the starting coordinate of the arrow line. It gives as output the "nodewidth"
\draw[*-{Stealth},very thick] let \p1 = (A.two), \p2 = (A.center) in (\x1+\nodewidth * 0.10, \y2) -- (B);
\draw[*-{Stealth},very thick] let \p1 = (B.one), \p2 = (B.center) in (\x1+\nodewidth * 0.10, \y2) |- (D);
\draw[*-{Stealth},very thick] let \p1 = (B.two), \p2 = (B.center) in (\x1+\nodewidth * 0.10, \y2) |- (C);
\end{tikzpicture}
\end{document}
如何精确调整箭头线的起点,使它们与 TikZ 节点内的中间点和中心点对齐?
更新 应用此代码后
\draw[very thick,red,{Circle[length=6pt,sep=-3pt]}-{Stealth}] ($(B.west)!0.25!(B.east)$) |- (D.west);
\draw[very thick,red,{Circle[length=6pt,sep=-3pt]}-{Stealth}] ($(B.two)$) |- (C.west);
经过daleif
的评论,情况大大改善:
因此,该注释解决了问题,但($(B.west)!0.25!(B.east)$)
符号到底是什么?它看起来像数学符号,TeX
但我假设 s 中的表达式在$
TikZ 上下文中使用时会进行算术/计算值,对吗?
答案1
使用chains
库并通过垂直坐标系定义箭头的起始位置:
\documentclass[border=3.141592,tikz]{standalone}
\usetikzlibrary{arrows.meta,
chains,
positioning,
shapes.multipart}
\begin{document}
\begin{tikzpicture}[
node distance = 8mm and 8mm,
start chain = A going right,
box/.style = {rectangle split, rectangle split horizontal,
rectangle split parts=2,
draw, thick,
text width=3em, minimum height=3ex,
align=center},
arr/.style = {draw=#1, thick,
{Circle[length=6pt,sep=-3pt]}-{Stealth[width=6pt]}
},
arr/.default = red
]
% nodes
\begin{scope}[nodes={on chain=A}]
\node {1511}; % A-1
\node[box] { 1111 \nodepart{two} };
\node[box] { \nodepart{two} };
\node[box] { 2111 \nodepart{two} }; % A-4
\end{scope}
\node[box, below=of A-4] (A-5) {3422 \nodepart{two} };
% arrows
\path[arr=black]
(A-2.two north |- A-2.east) -- (A-3);
\path[arr] (A-3.two north |- A-3.east) -- (A-4);
\path[arr] (A-3.one north |- A-3.west) |- (A-5);
\end{tikzpicture}
\end{document}
附录: 关于您的评论:
您应该提供已讨论的要求。它不包含更改节点大小(以及最终更改它们之间的距离)的可能性的要求。
只有当我遵守答案中的新要求时才接受这个答案的条件是不礼貌的(实际上是不可接受的)。
无论如何,图像大小会受到多种方式的影响:
- 通过缩放图像(不推荐,字体大小会变得不一致)
- 通过改变节点之间的节点距离
\nptw
(节点部分文字宽度)用于box
样式中的文字宽度- 上述两个选项您都可以在
tikzpicture
图像后定义 - 添加字体大小选项(例如,如果您需要较小的字体
针对此要求所采用的 MWE 可以是:
\documentclass[border=3.141592,tikz]{standalone}
\usetikzlibrary{arrows.meta,
chains,
positioning,
shapes.multipart}
\tikzset{mystyles/.style = {
start chain = A going right,
box/.style = {rectangle split, rectangle split horizontal,
rectangle split parts=2,
draw, thick,
text width=\nptw, minimum height=3ex, inner sep=2pt,
align=center},
arr/.style = {draw=#1, thick,
{Circle[length=6pt,sep=-3pt]}-{Stealth[width=6pt]}
},
arr/.default = red}
} % end of tikzset
\begin{document}
\begin{tikzpicture}[mystyles,
font = \scriptsize, % change to desired size
node distance = 5mm and 4mm, % <---
]
\def\nptw{2em} % <--- define box cells width
% nodes
\begin{scope}[nodes={on chain=A}]
\node {1511}; % A-1
\node[box] { 1111 \nodepart{two} };
\node[box] { \nodepart{two} };
\node[box] { 2111 \nodepart{two} }; % A-4
\end{scope}
\node[box=\nptw, below=of A-4] (A-5) {3422 \nodepart{two} };
% arrows
\path[arr=black]
(A-2.two north |- A-2.east) -- (A-3);
\path[arr] (A-3.two north |- A-3.east) -- (A-4);
\path[arr] (A-3.one north |- A-3.west) |- (A-5);
\end{tikzpicture}
\end{document}
答案2
这应该可以帮助你实现你的目标:
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.multipart}
\begin{document}
\begin{tikzpicture}[
node distance=1cm,
box/.style={
draw, thick,
rectangle split,
rectangle split horizontal,
rectangle split parts=2,
text width=1.25cm,
minimum height=.65cm,
inner sep=0pt,
align=center
},
arrow/.style={
thick,
{Circle[length=6pt,sep=-3pt]}-{Stealth[width=6pt]}
}
]
% nodes
\node (ORIG) {1511};
\node[box, right=.25cm of ORIG] (A) {
1111 \nodepart{two}
};
\node[box, right=of A] (B) {
\nodepart{two}
};
\node[box, right=of B] (C) {
2111 \nodepart{two}
};
\node[box, below=of B, anchor=west] (D) {
3422 \nodepart{two}
};
% arrows
\draw[arrow] ($(A.west)!.75!(A.east)$) -- (B.west);
\draw[arrow, red] ($(B.west)!.25!(B.east)$) |- (D.west);
\draw[arrow, red] ($(B.west)!.75!(B.east)$) -- (C.west);
% added to show how two entries can be used
\node[box, right=of D, blue] (E) {
A \nodepart{two} B
};
\draw[arrow, blue] ($(D.west)!.75!(D.east)$) -- (E.west);
\end{tikzpicture}
\end{document}