TikZ 是否有与 PSTricks \ncbar 命令等效的命令?

TikZ 是否有与 PSTricks \ncbar 命令等效的命令?

虽然我现在使用 TikZ,但我时不时会发现自己在寻找非常有用的 PSTricks 命令,而这些命令似乎在 TikZ 中不存在。该pst-node包有一个\ncbar命令,允许将两个节点与下垂的臂连接起来,通常成 90 度角。以下是一个例子:

\documentclass{article}
\usepackage{pst-node}
\begin{document}
\rput(0,0){\rnode{A}{\psframebox{A}}}
\rput(2,0){\rnode{B}{\psframebox{B}}}
\ncbar[angle=90]{A}{B}
\end{document}

代码输出

是否有与此有用命令等效的 TikZ?如果没有,创建一个有多容易?

答案1

没有直接等效的方法,但你可以将to路径机制与calc库一起使用来执行类似操作。如果你设置:

\tikzset{
    ncbar angle/.initial=90,
    ncbar/.style={
        to path=(\tikztostart)
        -- ($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)
        -- ($(\tikztotarget)!($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztostart)$)
        -- (\tikztotarget)
    },
    ncbar/.default=0.5cm,
}

您可以使用 在两个坐标之间绘制一个矩形连接\draw (a) to [ncbar] (b);。可以使用可选参数控制线的偏移量:\draw (a) to [ncbar=1cm] (b);。要更改连接线的角度,您可以使用以下键ncbar angle \draw [blue] (a) to [ncbar=1.5cm, ncbar angle=70] (b);

\documentclass[landscape]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
    ncbar angle/.initial=90,
    ncbar/.style={
        to path=(\tikztostart)
        -- ($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)
        -- ($(\tikztotarget)!($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztostart)$)
        -- (\tikztotarget)
    },
    ncbar/.default=0.5cm,
}

\begin{document}
\begin{tikzpicture}
\node [draw] (a) {A};
\node at (2,-1) [draw] (b) {B};

\draw  (a) to [ncbar] (b);
\draw [red] (a) to [ncbar=-1cm] (b);
\draw [blue] (a) to [ncbar=1.5cm, ncbar angle=70] (b);
\end{tikzpicture}
\end{document}

答案2

参考

代码

\documentclass{article}
\usepackage{pst-node,tikz}
\usetikzlibrary{paths.ortho}
\begin{document}
\hspace{.125cm}
\rput(0,0){\rnode{A}{\psframebox{A}}}
\rput(2,1){\rnode{B}{\psframebox{B}}}
\ncbar[angle=90]{A}{B}
\ncbar[angle=45]{A}{B}

\par
\begin{tikzpicture}[thick,nodes=draw]
\node (A)          {A};
\node (B) at (2,1) {B};

\draw (A) ud (B);
\draw[rotate=45] (A) rl (B);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

答案3

另一个答案给出了与 PSTricks \ncbar 命令等效的命令:

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}


\tikzset{% TikZ equivalent to the PSTricks \ncbar command
  label anchor/.initial={west},%
  label rotate/.initial={0},%
  arm angleA/.initial={0},%
  arm lengthA/.initial={1em},%
}
\newcommand{\anchorL}{\pgfkeysvalueof{/tikz/label anchor}}%
\newcommand{\rotateL}{\pgfkeysvalueof{/tikz/label rotate}}%
\newcommand{\angleA}{\pgfkeysvalueof{/tikz/arm angleA}}%
\newcommand{\lengthA}{\pgfkeysvalueof{/tikz/arm lengthA}}%

 \tikzstyle{arrow}=[->, >=stealth, thick, shorten <= 3pt, shorten >= 3pt]%

\newcommand{\nbar}[4][]{% \nbar[options]{node A}{node B}{label}
  \begin{tikzpicture}[remember picture,overlay,#1]
    \coordinate (p1) at ($(#2)+(\angleA:\lengthA)$) ;
    \coordinate (p2) at ($(#3)+(\angleA:\lengthA)$) ;
    \pgfmathanglebetweenpoints{\pgfpointanchor{p1}{center}}
    {\pgfpointanchor{p2}{center}}
    \pgfmathnotless{cos(\pgfmathresult-\angleA)}{0}
    \ifnum\number\pgfmathresult=1
    % Find the projection of the p2 point on the line from #2 to p1
    \coordinate (inter) at ($(#2)!(p2)!(p1)$) ;
    \draw[arrow] (#2) -- (inter) -- (p2) -- (#3) ;
    \path (inter) -- (p2) node[rotate=\rotateL,anchor=\anchorL,midway]{#4} ;
    \else
    % Find the projection of the p1 point on the line from #3 to p2
    \coordinate (inter) at ($(#3)!(p1)!(p2)$) ;
    \draw[arrow] (#2) -- (p1) -- (inter) -- (#3) ;
    \path (p1) -- (inter) node[sloped,rotate=\rotateL,anchor=\anchorL,midway]{#4} ;
    \fi
  \end{tikzpicture}}

\newcommand{\node}[3][]{% \node[options]{node's name}{my node}
  \tikz[remember picture,baseline = (#2.base)] \node[#1] (#2) {$#3$};}

\renewcommand{\node}[3][]{% \node[options]{node's name}{my node}
  \tikz[remember picture,baseline = (#2.base)] \node[#1] (#2) {#3};}

\begin{minipage}{10cm}
  \begin{center}
  \node[circle,dashed,draw]{A}{Node A}
  \hspace*{3cm}
  \raisebox{-10mm}{\node[draw]{B}{Node B}}
\end{center}
\end{minipage}


\nbar[red, arm angleA=70, arm lengthA=5mm,
label anchor=south,label rotate=\angleA-90]{A.\angleA}{B.\angleA}{label}
\nbar[green, arm angleA=150, arm lengthA=5mm,
label anchor=0,label rotate=-90]{A.\angleA}{B.\angleA}{label}
\nbar[blue, arm angleA=-90, arm lengthA=5mm,
label anchor=north,label rotate=0]{A.\angleA}{B.\angleA}{label}
 
\end{document}

相关内容