![TikZ 将路径的边缘设置为水平而不是 in=0 out=180 或反之亦然](https://linux22.com/image/263647/TikZ%20%E5%B0%86%E8%B7%AF%E5%BE%84%E7%9A%84%E8%BE%B9%E7%BC%98%E8%AE%BE%E7%BD%AE%E4%B8%BA%E6%B0%B4%E5%B9%B3%E8%80%8C%E4%B8%8D%E6%98%AF%20in%3D0%20out%3D180%20%E6%88%96%E5%8F%8D%E4%B9%8B%E4%BA%A6%E7%84%B6.png)
\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}