填充 tikz 箭头的开始和结束

填充 tikz 箭头的开始和结束

我正在制作一个函数箭头图,并注意到我的箭头“接触”了两端的数字:

在此处输入图片描述

\begin{center}
\begin{tikzpicture}
[
    >=stealth,
    bullet/.style=
    {
        fill=black,
        circle,
        minimum width=1pt,
        inner sep=1pt
    },
    -latex/.style=
    {
        ->,
        thick,
        shorten <=2pt,
        shorten >=2pt
    },
    every fit/.style=
    {
        ellipse,
        draw,
        inner sep=0pt
    }
]
    \coordinate (e) at (0.25,0);
    
    %Draw bullets and labels
    \foreach \y/\l in {1/a, 2/b, 3/c, 4/d}
    {
        \draw node[] at (0,\y) {$\l$};
        \coordinate(a\y) at (0,\y) {};
    }
    \foreach \y/\l in {1/x, 2/y, 3/z, 4/w}
    {
        \draw node[] at (4,\y) {$\l$};
        \coordinate (b\y) at (4,\y) {};
    }
    
    %Draw ellipses
    \node
    [
        draw,
        fit=(a1) (a2) (a3) (a4),
        minimum width=2.5cm,
        label={[name=l-dom(f)]above:$\dom(f)$}
    ] {} ;
    \node
    [
        draw,
        fit=(b1) (b2) (b3) (b4),
        minimum width=2.5cm,
        label={[name=l-range(f)]above:$\range(f)$}
    ] {} ;
        
    %Draw arrows
    \draw[-latex] (a1)+(e) -- (b3);
    \draw[-latex] (a2)+(e) -- (b4);
    \draw[-latex] (a3)+(e) -- (b3);
    \draw[-latex] (a4)+(e) -- (b1);
\end{tikzpicture}
\end{center}

如您所见,我将坐标 (e) 定义为 (0.25,0),用于水平填充,然后我将其添加到域中有效的点:

在此处输入图片描述

请注意,箭头看起来更好,从域中的点稍微右侧开始。

但是,接下来我尝试修复右侧的箭头。不幸的是,从范围内的点中减去这个坐标 (e) 不起作用并导致错误。有人知道原因以及如何修复这个问题吗?

还有一个请求:我想画一个连接 $dom(f)$ 和 $range(f)$ 的箭头,类似于这个答案

答案1

我将按照以下方式绘制您的图表:

\documentclass[margin=3mm]{standalone}
\usepackage{amsmath}
    \DeclareMathOperator{\dom}{dom}
    \DeclareMathOperator{\range}{range}
\usepackage{tikz}
\usetikzlibrary{fit, 
                chains,
                positioning,
                shapes.geometric}


\begin{document}
    \begin{tikzpicture}[
   node distance = 4mm and 44mm,
every fit/.style = {ellipse, draw,
                    minimum width=2cm, inner sep=0pt,
                    node contents={}
                    },
     start chain = going below,
                        ]
%Draw labels
\foreach \i/\j [count=\n] in {d/w, c/z, b/y, a/d}
{
    \node[on chain]     (a\n) {$\i$};
    \node[right=of a\n] (b\n) {$\j$};
}
%Draw ellipses
\node
[
    fit=(a1) (a4),
    label={[name=dom]:$\dom(f)$}
];
\node
[
    fit=(b1) (b4),
    label={[name=range]:$\range(f)$}
];

%Draw arrows
\foreach \i/\j in {a1/b4, a2/b2, a3/b1, a4/b2}    % changed
  \draw[-stealth, semithick] (\i) -- (\j);
% added
\draw[-stealth, semithick] (dom) -- node[above, font=\footnotesize] {text}    (range);
    \end{tikzpicture}
\end{document}

如您所见,我从 MWE 中删除了图表中未使用的所有样式定义,合并了用于在椭圆中绘制节点的循环,并使用库来chains定位两个椭圆中的节点。此外,箭头的绘制方式也略有不同,现在节点“z”处的箭头不再相互接触。

在此处输入图片描述

编辑:
在椭圆上方的标签之间添加了箭头上方的文本。参见上面的 MWE 和生成的图像)

如果您愿意,文本可以放在箭头中。在这种情况下,此箭头的代码为:

\draw[-stealth, semithick] (dom) -- node[fill=white, font=\footnotesize] {text}    (range);

在此处输入图片描述

答案2

像这样吗?

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit, shapes.geometric}

\DeclareMathOperator{\dom}{dom}
\DeclareMathOperator{\range}{range}

\begin{document}
\begin{center}
\begin{tikzpicture}
[
    >=stealth,
    bullet/.style=
    {
        fill=black,
        circle,
        minimum width=1pt,
        inner sep=1pt
    },
    -latex/.style=
    {
        ->,
        thick
    },
    every fit/.style=
    {
        ellipse,
        draw,
        inner sep=0pt
    }
]
    %Draw bullets and labels
    \foreach \y/\l in {1/a, 2/b, 3/c, 4/d}
    {
        \draw node (a\y) at (0,\y) {$\l$};
        \coordinate(a\y') at (0,\y) {}; % to keep the ellipse slim
    }
    \foreach \y/\l in {1/x, 2/y, 3/z, 4/w}
    {
        \draw node (b\y) at (4,\y) {$\l$};
        \coordinate (b\y') at (4,\y) {}; % to keep the ellipse slim
    }

    %Draw ellipses
    \node
    [
        draw,
        fit=(a1') (a4'),
        minimum width=2cm,
        label={[name=l-dom(f)]above:$\dom(f)$}
    ] {} ;
    \node
    [
        draw,
        fit=(b1') (b4'),
        minimum width=2cm,
        label={[name=l-range(f)]above:$\range(f)$}
    ] {} ;

    %Draw arrows
    \foreach \i/\j in {a1/b3, a2/b4, a3/b3, a4/b1, {{l-dom(f)}}/{{l-range(f)}}} {
      \draw[-latex] (\i.east) -- (\j.west);
    }
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

相关内容