如何使 tikz 圆形箭头与它们的连接点同心?

如何使 tikz 圆形箭头与它们的连接点同心?

我有一个框图,但是它看起来不对,因为圆形箭头与它连接的点相切,而不是与该点同心。

我怎样才能解决这个问题?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\title{immutable list}
\begin{document}
\begin{tikzpicture} 
  \tikzstyle{square} = [draw, shape=rectangle, minimum height=1cm, minimum width=1cm, node distance=2cm, line width=1pt]
  \node[square] (1a) at (0,0)     {};
  \node[square] (1b) at (1cm,0)   {};

  \node[square] (2a) at (3cm,0)   {};
  \node[square] (2b) at (4cm,0)   {};

  \node[square] (3a) at (6cm,0)   {};
  \node[square] (3b) at (7cm,0)   {};

  \node[square] (4a) at (9cm,0)   {};
  \node[square] (4b) at (10cm,0)  {};

  \node (1) at (0,-2) {1};
  \node (2) at (3,-2) {2};
  \node (3) at (6,-2) {3};
  \node (4) at (9,-2) {4};

  \draw [*-latex] (1b.center) -- (2a);
  \draw [*-latex] (2b.center) -- (3a);
  \draw [*-latex] (3b.center) -- (4a);
  \draw (4b.north east) -- (4b.south west);

  \draw [*-latex] (1a.center) -- (1);
  \draw [*-latex] (2a.center) -- (2);
  \draw [*-latex] (3a.center) -- (3);
  \draw [*-latex] (4a.center) -- (4);

\end{tikzpicture}
\end{document}

答案1

使用“老式”箭头很容易获得所需的效果:

\documentclass[tikz,border=5]{standalone}
\pgfarrowsdeclare{dot}{dot}
{
  \pgfarrowsleftextend{0pt}
  \pgfarrowsrightextend{0pt}
}
{
  \pgfpathcircle{\pgfqpoint{0pt}{0pt}}{2pt}
  \pgfusepathqfill
}
\begin{document}
\begin{tikzpicture}
\draw [help lines] (0,0) grid (3,2);
\draw [dot-stealth, red]  (0,0) -- (3,2);
\end{tikzpicture}
\end{document}

在此处输入图片描述

...但是使用图书馆就容易多了arrows.meta

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{arrows.meta}
\pgfarrowsdeclare{dot}{dot}
{
  \pgfarrowsleftextend{0pt}
  \pgfarrowsrightextend{0pt}
}
{
  \pgfpathcircle{\pgfqpoint{0pt}{0pt}}{2pt}
  \pgfusepathqfill
}
\tikzset{Dot/.tip={Circle[length=4pt,sep=-2pt]}}
\begin{document}
\begin{tikzpicture}
\draw [help lines] (0,0) grid (3,2);
\draw [dot-stealth, red]  (0,0) -- (3,2);
\draw [Dot-Stealth, blue] (0,2) -- (3,0);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

如果在这些点处画圆圈,它们将默认以该点为中心,这可能比尝试调整箭头长度更容易:

\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}
  [
    square/.style = {draw, shape=rectangle, minimum height=1cm, minimum width=1cm, node distance=2cm, line width=1pt},
  ]
  \node[square] (1a) at (0,0)     {};
  \node[square] (1b) at (1cm,0)   {};

  \node[square] (2a) at (3cm,0)   {};
  \node[square] (2b) at (4cm,0)   {};

  \node[square] (3a) at (6cm,0)   {};
  \node[square] (3b) at (7cm,0)   {};

  \node[square] (4a) at (9cm,0)   {};
  \node[square] (4b) at (10cm,0)  {};

  \node (1) at (0,-2) {1};
  \node (2) at (3,-2) {2};
  \node (3) at (6,-2) {3};
  \node (4) at (9,-2) {4};

  \foreach \i / \j in {1b/2a,2b/3a,3b/4a,1a/1,2a/2,3a/3,4a/4}
  {
    \draw [-latex] (\i.center) -- (\j);
    \path [fill] (\i.center) circle (1.5pt);
  }

  \draw (4b.north east) -- (4b.south west);


\end{tikzpicture}
\end{document}

圆圈和箭头

或者,你也可以将箭头的起点缩短为圆的半径。大致如下:

\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}
  [
    square/.style = {draw, shape=rectangle, minimum height=1cm, minimum width=1cm, node distance=2cm, line width=1pt},
  ]
  \node[square] (1a) at (0,0)     {};
  \node[square] (1b) at (1cm,0)   {};

  \node[square] (2a) at (3cm,0)   {};
  \node[square] (2b) at (4cm,0)   {};

  \node[square] (3a) at (6cm,0)   {};
  \node[square] (3b) at (7cm,0)   {};

  \node[square] (4a) at (9cm,0)   {};
  \node[square] (4b) at (10cm,0)  {};

  \node (1) at (0,-2) {1};
  \node (2) at (3,-2) {2};
  \node (3) at (6,-2) {3};
  \node (4) at (9,-2) {4};

  \foreach \i / \j in {1b/2a,2b/3a,3b/4a,1a/1,2a/2,3a/3,4a/4}
  {
    \draw [*-latex, shorten <=-2pt] (\i.center) -- (\j);
  }

  \draw (4b.north east) -- (4b.south west);


\end{tikzpicture}
\end{document}

缩短箭头

但需要进行微调才能达到完全正确的效果。

例如:

\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{tikzpicture}
  [
    square/.style = {draw, shape=rectangle, minimum height=1cm, minimum width=1cm, node distance=2cm, line width=1pt},
  ]
  \node[square] (1a) at (0,0)     {};
  \node[square] (1b) at (1cm,0)   {};

  \node[square] (2a) at (3cm,0)   {};
  \node[square] (2b) at (4cm,0)   {};

  \node[square] (3a) at (6cm,0)   {};
  \node[square] (3b) at (7cm,0)   {};

  \node[square] (4a) at (9cm,0)   {};
  \node[square] (4b) at (10cm,0)  {};

  \node (1) at (0,-2) {1};
  \node (2) at (3,-2) {2};
  \node (3) at (6,-2) {3};
  \node (4) at (9,-2) {4};

  \foreach \i / \j in {1b/2a,2b/3a,3b/4a,1a/1,2a/2,3a/3,4a/4}
  {
    \draw [{Circle[length=4pt]}-latex, shorten <=-2pt] (\i.center) -- (\j);
  }

  \draw (4b.north east) -- (4b.south west);


\end{tikzpicture}
\end{document}

将给出半径为 2pt(直径为 4pt)的圆,如果将箭头缩短 2pt,则该圆将位于中心。

调整后的圆圈

相关内容