如何使文本遮罩背景形状线条

如何使文本遮罩背景形状线条

我想画一幅画,用一些文字遮盖背景形状线条。我试过不透明度,但填充效果不太自然。

\documentclass[border=5mm, convert, usenames, dvipsnames]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning, calc, arrows.meta, fit}

\begin{document}
\begin{tikzpicture}
    \node [draw, rectangle, minimum width=2cm, minimum height=4cm] (a) {};
    \node [draw, rectangle, minimum width=2cm, minimum height=4cm, right = 1cm of a] (b) {};
    \draw [->, >=latex] (a) to node [above, fill, opacity=0.2,text opacity=1] {HelloWorld} (b);
\end{tikzpicture}
\end{document}

得到以下结果 在此处输入图片描述

但我想要的是这样的

在此处输入图片描述

忘记背景颜色了,我用一些图片编辑工具来得到这个。我想要的是矩形线条的一部分消失在文本后面。我试过用,backgrounds但也没用。

有可行的解决方案吗?

答案1

我认为behind path这就是你要找的东西。使用behind path选项节点内容绘制在定义它的路径后面(参见第 214 页的示例)。这样fill=white将覆盖线条,箭头绘制在文本上方。

\documentclass[border=5mm, convert, usenames, dvipsnames]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning, calc, arrows.meta, fit}

\begin{document}
\begin{tikzpicture}
    \node [draw, rectangle, minimum width=2cm, minimum height=4cm] (a) {};
    \node [draw, rectangle, minimum width=2cm, minimum height=4cm, right = 1cm of a] (b) {};
    \draw [->, >=latex] (a) to node [above, fill=white, behind path] {HelloWorld} (b);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

从你的评论中,我认为你想阻止节点。一种方法是基于保存路径反向剪辑这个答案

\documentclass[convert, usenames, dvipsnames,border=3.14mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\makeatletter % https://tex.stackexchange.com/a/38995/121799
\tikzset{
  use path/.code={\pgfsyssoftpath@setcurrentpath{#1}}
}
\makeatother
\tikzset{block out/.style={save path=\tmprotect}}% 
% https://tex.stackexchange.com/a/12033/121799
\tikzset{reverseclip/.style={insert path={([xshift=\pgflinewidth/2,yshift=\pgflinewidth/2,]current bounding box.north
        east) rectangle ([xshift=-\pgflinewidth/2,yshift=-\pgflinewidth/2]current bounding box.south west)}}}
\begin{document}
\begin{tikzpicture}
 \node [rectangle, minimum width=2cm, minimum height=4cm] (a) {};
 \node [rectangle, minimum width=2cm, minimum height=4cm, right = 1cm of a] (b) {};
 \draw [->, >=latex] (a) to node [above, block out] {HelloWorld} (b);   
 \clip[use path=\tmprotect,reverseclip]; 
 \draw (a.south west) rectangle (a.north east)
 (b.south west) rectangle (b.north east);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容