4

I want to draw a picture with some text masking the background shape lines. I tried with opacity but the fill is not very nature.

\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}

Yield the following enter image description here

However what I want is like this

enter image description here

Forget the background color, i use some image editing tool to get this. What I want is that part of rectangle shape line dissolved behind the text. I tried to use backgrounds but it also don't work.

Is there a possible solution?

5
  • 1
    fill=white,fill opacity=1?
    – user121799
    Oct 16, 2018 at 14:37
  • fill=white will mask the arrow line below. yshift may be added as a workaround. But I am wondering if there are some elegant way of "fill of no color", it is better that it only mask the lines intersecting with the text
    – Eric Sun
    Oct 16, 2018 at 14:46
  • 1
    \documentclass[border=5mm, convert, usenames, dvipsnames]{standalone} \usepackage{tikz} \usetikzlibrary{positioning} \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) {}; \path (a) to node [above, fill=white] {HelloWorld} (b); \draw [-latex] (a) to (b); \end{tikzpicture} \end{document}
    – user121799
    Oct 16, 2018 at 14:51
  • Yeah, first text then arrow line is a good solution. But I still want to know if there are any chance that fill=white can be replaced with things like fill=nocolor so that I can use the generated png in any color background
    – Eric Sun
    Oct 16, 2018 at 15:02
  • Not really. You could draw the node first, use save path and use path (from [this answer](ttps://tex.stackexchange.com/a/38995/121799)) to define the reverseclip trick (see this answer) to spare out the node when drawing the boxes. A corresponding ERASER style has been defined here.
    – user121799
    Oct 16, 2018 at 15:27

2 Answers 2

1

I think behind path is what you are looking for. With behind path option node contents is drawn behind the path where it is defined (see example on page 214). This way fill=white will cover the lines and the arrow is drawn over the text.

\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}

enter image description here

1
  • Thank you, behind path is one option I don't know before, quite useful here
    – Eric Sun
    Oct 17, 2018 at 8:47
1

From your comment I take you want to block out the node. One way to do that is based on save path, reverseclip and this answer.

\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}

enter image description here

1
  • Seems the magic happens when applying insert path to clip, and it now becomes outsider(reverse) clip. I read the manual about insert path and it says it "add something to the current path". How to understand it in this case?
    – Eric Sun
    Oct 17, 2018 at 8:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .