有可能吗tikz
自动地根据用于连接两个节点的第三个节点确定两个节点之间的距离?
例如,下图中连接节点的箭头太短。能否tikz
自动计算其长度,以便标签更适合它?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw] (a) {source program};
\node [rectangle,draw] (b) [right=of a] {token list};
\draw [->] (a.east) -- (b.west) node[above] {lexical analysis};
\end{tikzpicture}
\end{document}
答案1
(我确信我以前回答过类似的问题,但现在找不到了。)
一个解决方案就是简单地改变绘制顺序。由于token list
节点的位置应该取决于节点的长度lexical analysis
,因此lexical analysis
先绘制节点。
\documentclass{article}
%\url{http://tex.stackexchange.com/q/46842/86}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw] (a) {source program};
\node[above right] at (a.east) (l) {lexical analysis};
\draw[->] (l.south west) -- (l.south east);
\node [rectangle,draw,right] at (l.south east) (b) {token list};
\end{tikzpicture}
\end{document}
要将lexical analysis
和node
命令放在同一个命令中,你可以写
\draw[->] node [
above right,
append after command={
(\tikzlastnode.south west) -- (\tikzlastnode.south east)
}
] at (a.east) (l) {lexical analysis};
结果:
答案2
\pgfmathwidth
您还可以使用或width
使用来获取参数的长度\pgfmathparse
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw] (a) {source program};
\pgfmathwidth{"Lexical Analysis"}
\node [rectangle,draw] (b) [right= \pgfmathresult pt of a] {token list};
\draw [->] (a.east) -- (b.west) node[midway,above] {lexical analysis};
\end{tikzpicture}
\end{document}
如果定位太紧,可以进一步添加一些额外的空间。
答案3
您可以使用带有 \ phantom 的中间节点(如下所示)来计算宽度
\documentclass{独立} \usepackage{tikz} \usetikzlibrary{定位} \开始{文档} \开始{tikzpicture} \node [rectangle,draw] (a) {源程序}; \node[right =0em of a](ph){\phantom{词汇分析}}; \node [rectangle,draw] (b) [right=0em of ph] {token list}; \draw [->] (a.east) -- (b.west) node[midway,above] {词汇分析}; \结束{tikzpicture} \结束{文档}