如何在不影响自定义线标记的情况下减少两个节点之间的长度和边?

如何在不影响自定义线标记的情况下减少两个节点之间的长度和边?

我画了一些to and from带边的节点,其中一些边是红色的,上面有一个十字。我为十字做了一个自定义装饰,并在线的末端画了一个红色箭头。在尝试画shorten线时,为了避免超出箭头范围,我发现自定义标记也缩短了。

我目前所得到的如下面的代码和图片所示。有没有办法可以缩短线而不影响交叉,或者甚至有更好的方法来做到这一点?

感谢您的帮助!

\documentclass[tikz]{standalone}
\usepackage{tikz-cd}
\usetikzlibrary{arrows,positioning,decorations.pathmorphing,decorations.pathreplacing,decorations.shapes,decorations.markings}
\tikzset{
  shift left/.style ={commutative diagrams/shift left={#1}},
  shift right/.style={commutative diagrams/shift right={#1}},
  crossed/.style={draw=red,shorten >= 1.8pt,
    decoration={markings, mark= at position .15 with
      {
          \draw[red] (-2pt,-2pt) -- (2pt,2pt);
          \draw[red] (2pt,-2pt) -- (-2pt,2pt);
      };,
      mark= at position 1 with {\arrow[red]{>}};
    },
    postaction={decorate},
  },
}

\begin{document}
\begin{tikzpicture}[
    mynode/.style={rectangle,draw=black,thick,inner sep=0pt, minimum size=4mm},>=latex',
]

  \node[mynode,font=\tiny] (m0) {0};
  \node[mynode,right=of m0,font=\tiny] (m1) {1};

  \path[shift left=.75ex,thick]
  (m0) edge[->] node[very near start, yshift=-0.5ex, xshift=0.5ex,  font=\tiny] {2} (m1)
  (m1) edge[crossed] node[very near start, yshift=0.5ex, xshift=-0.5ex,  font=\tiny] {4} (m0);

\end{tikzpicture}
\end{document}

这给了我:

在此处输入图片描述

如果没有该shorten命令,我会得到:

在此处输入图片描述

答案1

对于箭头,你不需要mark->直接添加到边缘。然后对于你的装饰,你必须添加-

\draw[red,-] (-2pt,-2pt) -- (2pt,2pt);
\draw[red,-] (2pt,-2pt) -- (-2pt,2pt);

这样它们就没有箭头了。

\documentclass[tikz]{standalone}
\usepackage{tikz-cd}
\usetikzlibrary{arrows,positioning,decorations.pathmorphing,decorations.pathreplacing,decorations.shapes,decorations.markings}
\tikzset{
  shift left/.style ={commutative diagrams/shift left={#1}},
  shift right/.style={commutative diagrams/shift right={#1}},
  crossed/.style={draw=red,%shorten >= 1.8pt,
    decoration={markings, mark= at position .15 with
      {
          \draw[red,-] (-2pt,-2pt) -- (2pt,2pt);
          \draw[red,-] (2pt,-2pt) -- (-2pt,2pt);
      };,
      %mark= at position 1 with {\arrow[red,anchor=west]{>}};    %% not needed here
    },
    postaction={decorate},
  },
}

\begin{document}
\begin{tikzpicture}[
    mynode/.style={rectangle,draw=black,thick,inner sep=0pt, minimum size=4mm},>=latex',
]

  \node[mynode,font=\tiny] (m0) {0};
  \node[mynode,right=of m0,font=\tiny] (m1) {1};

  \path[shift left=.75ex,thick]
  (m0) edge[->] node[very near start, yshift=-0.5ex, xshift=0.5ex,  font=\tiny] {2} (m1)
  (m1) edge[crossed,->] node[very near start, yshift=0.5ex, xshift=-0.5ex,  font=\tiny] {4} (m0);

\end{tikzpicture}
\end{document}

在此处输入图片描述

如果你坚持使用箭头作为标记,那么shorten >= 0pt也可以添加装饰性的十字线,例如

 \draw[red,shorten >= 0pt] (-2pt,-2pt) -- (2pt,2pt);
 \draw[red,shorten >= 0pt] (2pt,-2pt) -- (-2pt,2pt);

以防止它们变短。

相关内容