This question is an extension of this one.
That question is about drawing a path in the background of nodes. There are three nodes equally separated on a line, and there is a straight line connecting all three in the background such that the middle one is not crossed visually. The nodes are filled, so the part of the path underneath the middle node is not visible (because it is behind the filling so to speak).
I want to achieve the same with transparent nodes (without filling) so that I can reuse the figure in different contexts (posters/slides) without the need to recolor the filling.
Here's my MWE (mostly borrowed from the same question)
\documentclass[border=20pt]{standalone}
\usepackage{tikz}
\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}
\begin{document}
\begin{tikzpicture}
\node [draw] (foo) at (0,0) { foo };
\node [draw] (bar) at (2,0) { bar };
\node [draw] (baz) at (4,0) { baz };
\begin{pgfonlayer}{bg}
\draw [pink] (foo) -- (baz) ;
\draw [cyan] (foo.25) -- (bar.155) (bar.25) -- (baz.155) ;
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
Which compiles to
The desired behavior can be achieved similarly to how the cyan line is drawn, but that notation is very long when there are more nodes and even longer when the line goes not through the vertical center, as here. Moreover, if I decide to add more nodes, I will have to correct all that code over and over again.
Is there any trick to produce a line like the cyan one by specifying its coordinates only at the outermost nodes?
\draw [pink] (foo) -- (bar) -- (baz);
is not a solution for you?yshift
a line like here. That won't work with non-rectangular nodes, but that's not my case currently, so thanks for the suggestion!\colorlet{bgcolor}{yellow}
and fill all nodes with this color. This way it would be easy to change the background color (and that of the nodes). Another way to go would be to inversely clip the nodes from the line (see my answer), but this is admittedly a bit tricky.cd
library (or thetikz-cd
package), you can use\draw [cyan, commutative diagrams/shift left=5pt] (foo) to (bar); \draw [cyan, commutative diagrams/shift left=5pt] (bar) to (baz);
(There is room for improvement to have a neater input syntax.) I believe this to be the best solution because you don't have to play around with clips ortransform canvas
which can have other side effects.