TikZ 将路径的边缘设置为水平而不是 in=0 out=180 或反之亦然

TikZ 将路径的边缘设置为水平而不是 in=0 out=180 或反之亦然
\path[->] (A) edge [out=180, in=0] (B);

在我的文档中经常写到这个路径。有时我会把它改成:

\path[->] (B) edge [out=0, in=180] (A);

为了保持一致性,我不想触碰该->属性。但我很想写一些类似的东西:

\path[->] (B) edge [inout = horizontal] (A);

如何才能达到隐含的效果?


编辑:

澄清:我希望这两条路径是相同的路径,而无需手动更改输入输出角度。

\path[-] (B) edge [inout = horizontal] (A);
\path[-] (A) edge [inout = horizontal] (B);

这是一个更好地说明该问题的小例子:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \node (A) at (0,0) {A};
    \node (B) at (3,2) {B};

    \node (C) at (3,0) {C};
    \node (D) at (6,2) {D};

    \path[-] (B) edge [out=0, in=180] (A); %not desired - longer path
    \path[-] (A) edge [out=0, in=180] (B); %desired path - shorter path

    %\path[-] (C) edge [inout = automagic] (D); %desired path - shorter path
    %\path[-] (D) edge [inout = automagic] (C); %desired path - shorter path 
    \path[-, ultra thick, red] (C) edge [out=0, in=180] (D); %desired result of [inout = automagic]
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

我提供三种风格:

  • curve -产生“水平”曲线的风格(进出方向分别为 0 度和 180 度),
  • 产生“垂直”曲线的样式curve |(进出方向分别为 90 度和 270 度),
  • 底层curve +样式,如果直接使用,则自行决定是否使用“垂直”或“水平”曲线。

代码

\documentclass[tikz]{standalone}
\makeatletter
\tikzset{
  curve +/.default=2,
  curve +/.style={
    to path={
      \pgfextra
        \tikz@scan@one@point\pgfutil@firstofone(\tikztostart)\relax
        \pgf@xa\pgf@x
        \pgf@ya\pgf@y
        \tikz@scan@one@point\pgfutil@firstofone(\tikztotarget)\relax
        \ifnum#1=2
          \pgfmathifthenelse{abs(\pgf@xa-\pgf@x)<abs(\pgf@ya-\pgf@y)}{0}{1}% 0 = ver
                                                                           % 1 = hor
        \else
          \def\pgfmathresult{#1}%
        \fi
        \ifnum\pgfmathresult=0
          \ifdim\pgf@ya<\pgf@y
            \def\tikz@to@out{90}\def\tikz@to@in{270}%
          \else
            \def\tikz@to@out{270}\def\tikz@to@in{90}%
          \fi
        \else
          \ifdim\pgf@xa<\pgf@x
            \def\tikz@to@out{0}\def\tikz@to@in{180}%
          \else
            \def\tikz@to@out{180}\def\tikz@to@in{0}%
          \fi
        \fi
      \endpgfextra
      \tikz@to@curve@path
    }
  },
  curve -/.style={curve +=1},
  curve |/.style={curve +=0},
}
\makeatother

\begin{document}
\foreach \a in {0,2,...,359}{% Warning: Will typeset 180 pages!
\begin{tikzpicture}
\useasboundingbox (-2.2,-2.2) rectangle (2.2,2.2);
\node (A) at (0,0) {A};
\node (B) at (\a:2cm) {B};
\path[->] (A) edge [black!50, curve |] (B)
              edge [black!50, curve -] (B)
              edge [black,    curve +] (B);
\end{tikzpicture}
}
\end{document}

输出

在此处输入图片描述

答案2

您可以创建自定义.is choice系列。如果您输入错误,或者使用不属于选择系列的未指定选项,它也会发出警告,这有助于调试。

\documentclass[tikz]{standalone}

\tikzset{inout/.is choice,
inout/horizo/.style={in=180,out=0},
inout/horizorev/.style={out=180,in=0}
}

\begin{document}
\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (3,2) {B};
\node (C) at (3,-2) {C};
\path[->] (A) edge [inout=horizo] (B);
\path[->] (A) edge [inout=horizorev] (C);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容