Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

enter image description here

and what I'd like to have is:

enter image description here

(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:

enter image description here

and

enter image description here

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?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

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!).

doubled path with overlaying intersection

share|improve this answer

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

enter image description here

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}
share|improve this answer

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

enter image description here

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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.