绘制一条横跨形状和文本内容的线

绘制一条横跨形状和文本内容的线

我想在两个节点之间画一条线,该线可能跨越现有形状和文本内容。在下面的示例中,连接和的线a与节点b的标签相交init。从 tikz 结果中注意到这一事实后,我可以手动执行“线飞”。但是是否有某种工具可以判断这种情况并在运行时自动处理?比如\draw [flyline, ->] (a) to (b)

\documentclass[convert]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning}

\begin{document}
    \begin{tikzpicture}[auto]
        \node[rectangle, draw=black, label={right:this is a test}] (init) {hello};
        \node[rectangle, draw=black, above right=of init] (a) {a};
        \node[rectangle, draw=black, below right=of init] (b) {b};
        \draw [->] (a) to (b);
        % I can change to use this after I find the intersect fact from the result
        %\draw (a) to (a|-init.north);
        %\draw [->] (b|-init.south) to (b);
    \end{tikzpicture}
\end{document}

更新:回复@Rmano 的评论:谢谢回复。是的,使用 layer 可以部分解决我的问题。我使用了 `tikz-layers` 包,它提供了 5 个预定义层。代码片段如下:
\documentclass[convert]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning, fit}
\usepackage{tikz-layers}

\begin{document}
    \begin{tikzpicture}[auto]
        \node[rectangle, draw=black, label={[fill=white, inner sep=2pt, name=lbl]right:this is a test}] (init) {hello};
        \node[rectangle, draw=black, above right=of init] (a) {a};
        \node[rectangle, draw=black, below right=of init] (b) {b};
        \begin{scope}[on behind layer]
            \draw [->] (a) to (b);
        \end{scope}
        \begin{scope}[on background layer]
            \node [fit=(init)(a)(b)(lbl), fill=cyan] () {};
        \end{scope}
    \end{tikzpicture}
\end{document}

结果如下

我觉得不太完美的一点是,这会使标签覆盖背景填充。原本label只有文本,现在它变成了对背景填充不透明的形状。我们可以像更改背景填充一样更改标签填充cyan,但这会产生另一个依赖关系。

是否有可能label有一个虚拟边界,它只掩盖下划线draw但不掩盖下划线fill


更新:根据@Rmano 使用“contour”的建议,将 MWE 更新为以下内容
\documentclass[convert]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning, fit}
\usepackage{tikz-layers}
\usepackage{bidicontour}
\usepackage{bidi}
\bidicontourlength{2pt}

\begin{document}
    \begin{tikzpicture}[auto]
        \node[rectangle, draw=black, label={[inner sep=2pt, name=lbl]right:{\bidicontour{cyan}{this is a test}}}] (init) {hello};
        \node[rectangle, draw=black, above right=of init] (a) {a};
        \node[rectangle, draw=black, below right=of init] (b) {b};
        \begin{scope}[on behind layer]
            \draw [->] (a) to (b);
        \end{scope}
        \begin{scope}[on background layer]
            \node [fit=(init)(a)(b)(lbl), fill=cyan] () {};
        \end{scope}
        %\draw (a) to (a|-init.north);
        %\draw [->] (b|-init.south) to (b);
    \end{tikzpicture}
\end{document}

几乎完美的结果

谢谢您的帮助!

答案1

是否有可能label有一个虚拟边界,它只掩盖下划线draw但不掩盖下划线fill

我猜不会。

label={[fill=cyan]text}另外,我认为,书写和之间没有太大区别label={[...]\bidicontour{cyan}{text}}。人们总是需要在 中明确写出背景颜色label={...}

下面的例子展示了只写一次背景颜色的尝试。pgfonlayerreversed环境的定义是从我的上一个答案

\documentclass[tikz]{standalone}
\usetikzlibrary{backgrounds, shapes, positioning, fit}

\usepackage{xpatch}

\makeatletter
% copied from my previous answer https://tex.stackexchange.com/a/562606
\let\pgfonlayerreversed\pgfonlayer
\let\endpgfonlayerreversed\endpgfonlayer

\xpatchcmd\pgfonlayerreversed
  {\expandafter\box\csname pgf@layerbox@#1\endcsname\begingroup}
  {\begingroup}
  {}{\fail}

\xpatchcmd\endpgfonlayerreversed
  {\endgroup}
  {\endgroup\expandafter\box\csname pgf@layerbox@\pgfonlayer@name\endcsname}
  {}{\fail}

% similar to \tikz@background@framed, but using "pgfonlayerreversed" envi
\def\tikz@background@framed@reversed{%
  \tikz@background@save%
  \pgfonlayerreversed{background}
    \path[style=background rectangle] (\tikz@bg@minx,\tikz@bg@miny) rectangle (\tikz@bg@maxx,\tikz@bg@maxy);
  \endpgfonlayerreversed
}%

% similar to option "show background rectangle"
\tikzset{
  show background rectangle reversed/.style={
    execute at end picture=\tikz@background@framed@reversed
  }
}
\makeatother

% user interface
\tikzset{
  background color/.style={
    show background rectangle reversed,
    inner frame sep=2pt,
    background rectangle/.append style={draw=none, #1},
    every node/.append style={#1},
    every label/.append style={#1}
  }
}

\begin{document}
    \begin{tikzpicture}[background color={fill=cyan}]
        \node[draw, label={[inner sep=2pt, name=lbl]right:this is a test}] (init) {hello};
        \node[draw, above right=of init] (a) {a};
        \node[draw, below right=of init] (b) {b};
        
        \begin{scope}[on background layer]
            \draw [->] (a) to (b);
        \end{scope}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容