如何将一行放置在另一行“下方”?

如何将一行放置在另一行“下方”?

得益于此回答,我设法实现了这个代码:

\documentclass{article}
\usepackage{tikz}
  \usetikzlibrary{math}
  \usetikzlibrary{calc}
    \usetikzlibrary{positioning}
    \usetikzlibrary{arrows.meta}
    \usetikzlibrary{decorations.pathmorphing}
    \usetikzlibrary{decorations.pathreplacing}
\tikzset{%
  rev/.style={
    postaction={%
      decoration={
        show path construction,
        lineto code={
          \tikzmath{
            real \d, \a;
            \d = 0.7;
            \a = -90;
            {
              \path (\tikzinputsegmentfirst) coordinate (I);
              \path (\tikzinputsegmentlast) coordinate (F);
              \draw[arrows={-latex},decorate,decoration={snake,amplitude=.4mm,segment length=2mm,post length=1mm}]
              let
                \p1 = ($(I)-(F)$),
                \n1 = {atan2(\y1, \x1)},
                \p2 = ($(F)!.5!(I) -(\n1: \d em)$)
              in
              (\p2) ++(\n1+ \a: 1ex) -- ++(\n1: 2*\d em);
            };
          }
        }
      },
      decorate
    }
  }
}
\begin{document}
\tikzstyle{label}=[draw=none,above]
\begin{tikzpicture}[->,thick,every node/.style={draw,circle}]
\node (v) {v};
\node [below left=1cm of v] (a) {a};
\node [below right=2cm of v] (b) {b};
\draw[rev] (v) -- node[label] {$x$} (a);
\draw[rev] (v) -- node[label] {$y$} (b);
\end{tikzpicture}
\end{document}

其渲染效果如下:

在此处输入图片描述

第二行的蛇形箭头位置不对。必须以某种方式自动地放置below线,类似于label放置节点的方式above。 可以吗?

答案1

在此处输入图片描述

如果装饰的位置始终位于主段下方,则可以将其与向量第一个坐标上的符号联系起来。请注意,该函数sign返回 0 表示 0,因此这里不太方便。

评论。从美学角度来看,似乎标签的位置有点问题。

代码

\documentclass[11pt, border=.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math, calc}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{%
  rev/.style={
    postaction={%
      decoration={
        show path construction,
        lineto code={
          \tikzmath{
            coordinate \I, \F, \v;
            \I = (\tikzinputsegmentfirst);
            \F = (\tikzinputsegmentlast);
            \v = ($(\I) -(\F)$);
            real \d, \a, \r, \t;
            \d = 0.8;
            \t = atan2(\vy, \vx);
            if \vx<0 then { \a = 90; } else { \a = -90; };
            {
              \draw[arrows={-latex}, decorate,
              decoration={%
                snake, amplitude=.4mm,
                segment length=2mm,
                post length=1mm
              }]
              ($(\F)!.5!(\I) +(\t: -\d em) +(\t +\a: 1ex)$)
              -- ++(\t: 2*\d em);
            };
          }
        }
      },
      decorate
    }
  }
}
\begin{document}

\begin{tikzpicture}[->, thick,
  every node/.style={draw, circle},
  label/.style={draw=none, above}]
  \node (v) {v};
  \node [below left=2cm of v] (a) {a};
  \node [below right=3cm of v] (b) {b};
  \node [left=2cm of v] (c) {c};
  \draw[rev] (v) -- node[label] {$x$} (a);
  \draw[rev] (v) -- node[label] {$y$} (b);
  \draw[red, rev] (v) -- node[label] {$z$} (c); 
\end{tikzpicture}
\end{document}

答案2

像这样:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\usetikzlibrary{calc}
\usepackage{txfonts}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows.meta}

\begin{document}
    %\tikzstyle{label}=[draw=none,above]
    \begin{tikzpicture}[->,thick,every node/.style={draw,circle}]
        \node (v) {v};
        \node [below left=1cm of v] (a) {a};
        \node [below right=2cm of v] (b) {b};
        \draw[-latex] (v) -- node[draw=none,pos=.5,above] {$x$} (a)
            node[draw=none,pos=.5, below,sloped] () {$\rightsquigarrow$};
        \draw[-latex] (v) -- node[draw=none,above] {$y$} (b)
            node[draw=none,pos=.5, below,sloped] () {$\leftsquigarrow$};
    \end{tikzpicture}
\end{document}

相关内容