微调 TikZ 节点内箭头的位置

微调 TikZ 节点内箭头的位置

我想要一个 TikZ 节点,里面有几行文本。节点应该是一个矩形,比文本宽,文本应该左对齐。在每一行文本后,我想放置指向其他节点的箭头。这是我目前得到的结果:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}

\makeatletter
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}
\makeatother



\begin{document}

\begin{tikzpicture}[innode/.style={inner sep=0pt, minimum width=1pt},
                    env/.style={draw,rectangle,align=left,inner sep=0.3cm}]
  \node[env,text width=4cm,matrix](q1){\node[innode](sp){first item};\\
                                       \node[innode](at){and the second item};\\};
  %
  \node(c1)[below=of q1.south west,anchor=north]{\ttfamily code for 2};
  \node(c2)[below=of c1.south east,anchor=east]{\ttfamily code for 1 goes here};
  %
  \gettikzxy{(c1)}{\xcu}{\ycu}
  \gettikzxy{(c2)}{\xcd}{\ycd}
  \gettikzxy{(at.east)}{\xate}{\yate}
  \gettikzxy{(sp.east)}{\xspe}{\yspe}
  %
  \draw[->] (sp.east) -- ++(3,0)
                      -- ($ (\xspe,\ycd) + (3,0) $)
                      -- (c2.east);
  \draw[->] (at.east) -- ++(2,0)
                      -- ($ (\xate,\ycu) + (2,0) $)
                      -- (c1.east);
\end{tikzpicture}

\end{document}

在此处输入图片描述

但是,箭头的起点距离行尾有点太远了。有没有更好的方法来实现我想要的效果?如何让箭头的起点更靠近文本?

答案1

如果您nodes={anchor=west}matrix选项中设置,矩阵中的单元格将左对齐,而无需text width明确设置。这样,即使文本的长度不同,行也会从靠近文本的位置开始。

请注意,对于连接线,您可以简单地使用语法|-来获得正交连接,而不是使用语法calc

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\makeatletter
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}
\makeatother



\begin{document}

\begin{tikzpicture}[innode/.style={inner sep=0pt, minimum width=1pt},
                    env/.style={draw,rectangle,align=left,inner sep=0.3cm}]
  \node[env,matrix, nodes={anchor=west}](q1){\node[innode](sp){first item};\\
                                       \node[innode](at){and the second item};\\};
  %
  \node(c1)[below=of q1.south west,anchor=north]{\ttfamily code for 2};
  \node(c2)[below=of c1.south east,anchor=east]{\ttfamily code for 1 goes here};
  %
  \gettikzxy{(c1)}{\xcu}{\ycu}
  \gettikzxy{(c2)}{\xcd}{\ycd}
  \gettikzxy{(at.east)}{\xate}{\yate}
  \gettikzxy{(sp.east)}{\xspe}{\yspe}
  %
  \draw[->] (sp.east) -- ++(3,0) |- (c2.east);
  \draw[->] (at.east) -- ++(1,0) |- (c1.east);
\end{tikzpicture}

\end{document}

答案2

可以在这里使用tkz-linknodes

\documentclass{scrartcl}
\usepackage{tkz-linknodes}
\usetikzlibrary{matrix,positioning}


\begin{document}
\hspace*{3cm}
\begin{NodesList}
\begin{tikzpicture}[innode/.style={inner sep=0pt, minimum width=1pt},
                    env/.style={draw,rectangle,align=left,inner sep=0.3cm}]
  \node[env,matrix, nodes={anchor=west}](q1){%
                                \node[innode](sp){first item\AddNode};  \\
                                \node[innode](at){and the second item\AddNode[2]};\\};

  \node(c1)[below=of q1.south west,anchor=north]{\ttfamily code for 2\AddNode };
  \node(c2)[below=of c1.south east,anchor=east]{\ttfamily code for 1 goes here\AddNode[2]};
\LinkNodes[margin=9cm]{}
\LinkNodes[margin=10cm]{}   

\end{tikzpicture}
\end{NodesList}

\end{document}

在此处输入图片描述

相关内容