# How to control the crossings between double lines?

I want to draw ribbon lines and to be able to control the crossings.

For instance

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\clip (-1,-2) rectangle (2,2);
\draw [double,double distance=3pt]
plot [smooth cycle] coordinates{(-4,0) (0,0) (4,0)}
plot [smooth cycle] coordinates{(0,0) (0,1) (1,1) (1,-1) (0,-1)};
\end{tikzpicture}
\end{document}


produces the following:

and what I'd like to have is:

(which I corrected manually).

Ideally (and eventually I'll need that) I'd like to be able to treat as nodes the figures like, say, these:

and

and be able to choose one of the four double lines and connect it with a double line of other node(s); so I'm sure my approach is not the best one. Any suggestions?

-

The knots TikZ library was designed almost precisely for this purpose. I say "almost" because I didn't consider the possibility of leaving an intersection alone when I designed it, however you're in luck because of how you crafted the curve (namely, that the intersection that you want to "ignore" is at an end point of the path component) and I did put in an option to ignore intersections at end points.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/175582/86}

\usepackage{tikz}
\usetikzlibrary{knots}
\begin{document}
\begin{tikzpicture}
\begin{knot}[
consider self intersections=no splits,
only when rendering/.style={
double,
double distance=3pt
}
]
\strand (-4,0) -- (0,0) -- (4,0) plot [smooth cycle] coordinates{(0,0) (0,1) (1,1) (1,-1) (0,-1)};
\end{knot}
\end{tikzpicture}
\end{document}


For details, see the documentation and the question that inspired it (Braid diagrams in TikZ). Interestingly, it essentially implements the method proposed in Jesse's answer of redrawing the diagram in the region of the intersection but the correct way around. However, it does it (fairly) automatically.

The one big change to your code is that I've replaced the line plot[smooth cycle] {(-4,0) (0,0) (4,0)} with a simple straight line. By making it a smooth cycle you were drawing along it in both directions which isn't necessary (and which really confuses the knots library as the number of intersections goes through the roof!).

-

This attempt basically adds a scope environment to redraw the loop, limited to the tiny clip window, again.

Code

\documentclass{article}%[border=5pt]{standalone}
\usepackage{tikz}
\begin{document}

OP's solution:

\begin{tikzpicture}
\clip (-1,-2) rectangle (2,2);
\draw [double,double distance=3pt]
plot [smooth cycle] coordinates{(-4,0) (0,0) (4,0)}
plot [smooth cycle] coordinates{(0,0) (0,1) (1,1) (1,-1) (0,-1)};
\end{tikzpicture}\par

Proposed solution

\begin{tikzpicture}
\clip (-1,-2) rectangle (2,2);
\draw [double,double distance=3pt]
plot [smooth cycle] coordinates{(-4,0) (0,0) (4,0)}
plot [smooth cycle] coordinates{(0,0) (0,1) (1,1) (1,-1) (0,-1)};
\begin{scope}
\clip [] (1,-0.2) rectangle (1.2,0.2);
\draw [double,double distance=3pt] plot [smooth cycle] coordinates{(0,0) (0,1) (1,1) (1,-1) (0,-1)};
\end{scope}
\end{tikzpicture}
\end{document}

-

This is not a general solution but works for this example. OP's code uses one \draw command which considers all intersections.

\draw [double,double distance=3pt]
plot [smooth cycle] coordinates{(-4,0) (0,0) (.5,0)}
plot [smooth cycle] coordinates{(0,0) (0,1) (1,1) (1,-1) (0,-1)};


First solution consists in dividing the drawing process between several commands

\draw [double,double distance=3pt] plot [smooth cycle] coordinates{(-4,0) (0,0) (.5,0)}
\draw plot [smooth cycle] coordinates{(0,0) (0,1) (1,1) (1,-1) (0,-1)};


which generates

This image solves right hand intersection but left hand is wrong. Therefore a better solution consists in drawing part of horizontal line and rectangle within one command but after drawing right hand part of horizontal line:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\clip (-1,-2) rectangle (2,2);
\draw [double,double distance=3pt]
plot [smooth cycle] coordinates{(0.5,0) (1,0) (4,0)};
\draw [double,double distance=3pt]
plot [smooth cycle] coordinates{(-4,0) (0,0) (.5,0)}
plot [smooth cycle] coordinates{(0,0) (0,1) (1,1) (1,-1) (0,-1)};
\end{tikzpicture}
\end{document}


-