3

I'm drawing some crossing lines, some of which are emphasized by a thick, gray line in the background. Now, I'd like to have gaps where the lines cross, and I am able to achieve this by using inverted clipping, but the problem is that the resulting PDF seems to be a bit heavy to render. I'll be using the PDFs in Keynote (OS X), and in the more recent versions, it's really slow to render more complex PDFs. (Tips for solving that are also appreciated, but probably not on topic here. I've had some success with rendering to images or movies, but that's less than satisfactory…)

First, a version using just a white line for the gap—a common tecnique that obviously doesn't work:

\begin{tikzpicture}[draw=solarized-blue]
    \pts
    \draw[line width=3pt,black!15] (a) -- (b);
    \draw[thick] (a) -- (b);
    \draw[ultra thick, white] (x) -- (y);
    \draw[thick] (x) -- (y);
\end{tikzpicture}

Gap in the background line

This is my current solution, which leads to PDFs that some viewers (or at least Keynote, and perhaps other Quartz apps) have some trouble rendering when i have more than a handful of lines:

\begin{tikzpicture}[draw=solarized-blue]
    \pts
    \draw[line width=3pt,black!15] (a) -- (b);
    \begin{scope}[even odd rule]
    \draw[thick] (x) -- (y);
    \clip[overlay]
            (-1cm,-1cm) rectangle (1cm,1cm)
            ($(x)!0.8pt!90:(y)$) -- ($(y)!0.8pt!-90:(x)$) --
            ($(y)!0.8pt!90:(x)$) -- ($(x)!0.8pt!-90:(y)$) --
            cycle;
    \draw[thick] (a) -- (b);  
    \end{scope}
\end{tikzpicture}

Correct, but overly complex

I've tried one alternate solution, using transparency, and putting the “background” line on top – but then I have to adjust the color of the main line itself. Here I've just added some white, but I guess I'd really have to reduce the amount of black. Also, it messes with the colors of the crossing lines, so this doesn't really work:

\begin{tikzpicture}[draw=solarized-blue]
    \pts
    \draw[thick,draw=solarized-blue!85!white] (a) -- (b);
    \draw[ultra thick, white] (x) -- (y);
    \draw[thick] (x) -- (y);
    \draw[line width=3pt,black,opacity=.15] (a) -- (b);
\end{tikzpicture}
\end{document}

enter image description here

Are there any clean ways of doing this that won't lead to overly complex clipping paths or the like?

Complete document:

\documentclass[tikz,border=3pt]{standalone}
\usepackage{tikz,xcolor-solarized}
\usetikzlibrary{calc}
\def\pts{\path (-10pt,-10pt) coordinate (a)
               (+10pt,+10pt) coordinate (b)
               (+10pt,-10pt) coordinate (x)
               (-10pt,+10pt) coordinate (y);}
\begin{document}
\begin{tikzpicture}[draw=solarized-blue]
    \pts
    \draw[line width=3pt,black!15] (a) -- (b);
    \draw[thick] (a) -- (b);
    \draw[ultra thick, white] (x) -- (y);
    \draw[thick] (x) -- (y);
\end{tikzpicture}
\begin{tikzpicture}[draw=solarized-blue]
    \pts
    \draw[line width=3pt,black!15] (a) -- (b);
    \begin{scope}[even odd rule]
    \draw[thick] (x) -- (y);
    \clip[overlay]
            (-1cm,-1cm) rectangle (1cm,1cm)
            ($(x)!0.8pt!90:(y)$) -- ($(y)!0.8pt!-90:(x)$) --
            ($(y)!0.8pt!90:(x)$) -- ($(x)!0.8pt!-90:(y)$) --
            cycle;
    \draw[thick] (a) -- (b);  
    \end{scope}
\end{tikzpicture}
\begin{tikzpicture}[draw=solarized-blue]
    \pts
    \draw[thick,draw=solarized-blue!85!white] (a) -- (b);
    \draw[ultra thick, white] (x) -- (y);
    \draw[thick] (x) -- (y);
    \draw[line width=3pt,black,opacity=.15] (a) -- (b);
\end{tikzpicture}
\end{document}
  • As a practical matter: As it happens, reverting to Keynote ’09 solves the problem. I guess the newer versions are borked, somehow :-/ – Magnus Lie Hetland Aug 22 '16 at 10:14
  • Please provide a complete example we can copy-paste-compile. It is much easier than trying to reassemble code fragments, adding in the bits which might be needed though maybe not the same needed bits you used and so on. – cfr Aug 22 '16 at 23:08
  • You might look at how the packages for drawing knots do this. They have to do this lots of times. Compilation is not fast, but it would be impossible if not reasonably efficient and I'm assuming something similar for rendering. – cfr Aug 22 '16 at 23:36
  • @cfr All that was missing was the document environment, but I've added that. :) – Magnus Lie Hetland Aug 23 '16 at 8:25
  • Hm, yes. I think I've looked at the knots package before, but it does look like a better solution than doing it manually. It does seem to use clipping as well, though. But I guess maybe that can't be avoided. – Magnus Lie Hetland Aug 23 '16 at 8:26
2

This is an old question but it deserve an answer.

You can cut the double curve by filling small circles at each intersection.

\documentclass[tikz,border=3pt]{standalone}
\usetikzlibrary{intersections}
\colorlet{front}{blue!70}
\colorlet{back}{gray!30}
\begin{document}
  \begin{tikzpicture}[x=2cm,y=2cm]
    \clip (-2,-2) rectangle (2,2);
    \def\pathA{(-2,-1) .. controls (8,-1) and (-8,1) .. (2,1)}
    \def\pathB{(-1,-2) .. controls (-1,8) and (1,-8) .. (1,2)}
    % draw the curve A with two colors : back and front
    \draw[name path=curveA, line width=10pt, back] \pathA;
    \draw[line width=3pt,front] \pathA;
    \path[name path=curveB] \pathB;
    % draw a circle at each intersection with back color to "cut" the curve
    \fill [name intersections={of=curveA and curveB, name=i, total=\t}, back]
      foreach \s in {1,...,\t}{(i-\s) circle (5pt)};
    % draw the curve B through the cuted curve A
    \draw [line width=3pt, front] \pathB;
  \end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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