12

I'm typesetting an article on graphical calculus in tensor categories in TikZ and I wanted to define a decoration that draws two parallel lines at the same time. Here is my first try:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{calc}
\usetikzlibrary{knots}
\usepgfmodule{decorations}

\begin{document}
    \pgfdeclaredecoration{twolines}{initial}
    {
        \state{initial}[width=2pt]
        {
            \pgfpathmoveto{\pgfpoint{0pt}{1pt}}
            \pgfpathlineto{\pgfpoint{2pt}{1pt}}
            \pgfpathmoveto{\pgfpoint{0pt}{-1pt}}
            \pgfpathlineto{\pgfpoint{2pt}{-1pt}}
            \pgfpathmoveto{\pgfpoint{0pt}{0pt}}
        }
    }
    \tikz[decoration=twolines]{
        \coordinate (T) at (0.6,0);
        \coordinate (N) at (0,0.2);
        \draw[decorate,knot=black] (T) to[out=-90,in=-90] (N) -- (0,1);
        \draw[decorate,knot=black] (0,-1) node[below] {$X \otimes Y$} -- ($(0,0)-(N)$) to[out=90,in=90] (T);
    }
\end{document}

enter image description here

As you can see, it kind of works, but it's not ideal. The individual line segments should be connected, and ideally, curved. I could just decrease "width" to a ridiculously short length (say 0.1pt), but that seems to slow big pictures down a lot. Is there a nicer way to fix the problems?

Edit: I tried percusse's excellent suggestion to just use the "double" option, which is probably very fast and doesn't even need decorations. However, it seems that it doesn't play well with the "knots" library. Combining "double" and "knot=black" seems to overwrite the "double" option.

Edit 2: My question is about tikz decorations and not about general ways to draw my diagram, especially none including pstricks.

Edit 3: Workarounds for my particular example aren't very interesting since it's easily done in TikZ or with other libraries. I'm interested in writing a good decoration.

  • 2
    I think you can get away with double in many cases. – percusse Jul 25 '14 at 17:36
  • @percusse, that's cool, I didn't know about it, since I only looked in the decorations documentation. However, my actual usecase involves Andrew Stacey's "knots" package and it seems not to be compatible with double. I'll update my question. – Turion Jul 25 '14 at 17:41
  • knots or braids? – percusse Jul 25 '14 at 17:47
  • knots, see my update. – Turion Jul 25 '14 at 17:48
  • 1
    double doesn't work with knot because knot invokes double internally. – Loop Space Jul 28 '14 at 21:18
12

knot and double don't work together because the double mechanism is how knot overwrites any underlying paths: it uses double with the sidelines set to the background colour and the central colour the main colour (this is opposite to how double is intended to be used). If you want the main path to itself be doubled then you need to draw the path three times: outermost with the background colour, middle with the main colour, and then innermost with the background colour again. This isn't natively provided for in TikZ (and nor in the knots package) but can be easily done with a preaction.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/193243/86}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\coordinate (T) at (.6,0);
\coordinate (N) at (0,0.2);

\draw[double] (T) to[out=-90,in=-90] (N) -- (0,1);
\draw[double,preaction={draw=white,double=none,line width=4pt}] (0,-1) node[below] {$X \otimes Y$} -- ($(0,0)-(N)$) to[out=90,in=90] (T);
\end{tikzpicture}
\end{document}

Knot path with double

If you want to specify your path as a single path with a self-intersection, you should use the knot environment (from the knots package) instead.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/193243/86}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{calc}
\usetikzlibrary{knots}

\begin{document}
\begin{tikzpicture}
\coordinate (T) at (.6,0);
\coordinate (N) at (0,0.2);

\begin{knot}[
  consider self intersections=true,
  ignore endpoint intersections=false,
  clip width=2,
]
\strand[double] (0,-1) node[below] {$X \otimes Y$} -- ($(0,0)-(N)$) to[out=90,in=90] (T) to[out=-90,in=-90] (N) -- (0,1);
\end{knot}
\end{tikzpicture}
\end{document}

with the knot environment

  • Your second example doesn't work on my computer, though. – Turion Jul 29 '14 at 17:19
  • @Turion What's the error? It might be a version issue. – Loop Space Jul 29 '14 at 22:39
  • TikZ 3 and knots 1.0. It looks like this: !this – Turion Oct 13 '14 at 15:08
8

I can propose a solution (more or less accurate) in pstricks but it should not be difficult to adapt it to tikz-pdf, as the code is simple. It's based on the fact that the curve you want is close to a strophoid and that one can plot it using its polar equation.

        \documentclass[11pt, a4paper,svgnames,pdf]{article}
        \usepackage[utf8]{inputenc}
        \usepackage[T1]{fontenc}
        \usepackage{lmodern}

        \usepackage{pst-plot}
        \usepackage{pst-node} 

        \begin{document}

        \psset{yunit=8mm, xunit=8mm, labelsep=0pt , linewidth=1pt,linecolor=IndianRed, doubleline=true,  plotpoints=200,plotstyle=curve, polarplot, algebraic}%
        \Rnode{S}{\psset{}
        \psplot{-1.35}{0}{0.5*cos(2*x)/cos(x)} \psplot[border =1pt]{0}{1.35}{0.5*cos(2*x)/cos(x)}}
        \uput{0.35cm}[dl](S){$X\bigotimes Y $}

        \end{document}

enter image description here

Added: Hopefully a better fitting solution. Note that global change in the unit will require an adjustement of the elliptic arc x-diameter in the code:

        \documentclass[11pt, a4paper,svgnames,pdf]{article}
        \usepackage[utf8]{inputenc}
        \usepackage[T1]{fontenc}
        \usepackage{lmodern}

        \usepackage{pst-plot}
        \usepackage{pst-node}

        \begin{document}
        \pspicture%
        \psset{labelsep=0pt , linewidth=1pt,linecolor=IndianRed, doubleline=true}%
        \Rnode{S}{%
          \psline(0,1.6)(0,0.6)\psarc(0.80,0.6){0.8}{179}{271}
           \psellipticarcn(0.8,0)(0.30, 0.257){90}{-90}
          \psarc[border = 1pt, bordercolor = white](0.80,-0.6){0.8}{89}{181}\psline(0,-0.6)(0,-1.6)
          }
        \uput{1.8}[d](S){$X\bigotimes Y $}
        \endpspicture

        \end{document}

enter image description here

  • This doesn't really answer how to improve the decoration, right? The way how the curve looks exactly is irrelevant. – Turion Jul 28 '14 at 16:54
  • No, I proposed a curve that more or less had the look of your picture, and was a smooth curve. But it was not clear to me what you exactly wanted. The soultuion might come from a piecewise defined function. – Bernard Jul 28 '14 at 17:10
  • My question is "How to improve on this twolines decoration", so the question is how to improve the decoration, not the curve. – Turion Jul 28 '14 at 17:15
  • Sorry, your solution does look very good, thank you for the effort anyways! – Turion Jul 28 '14 at 17:17
  • I have another idea, that I'll fine-tune and propose in a moment. – Bernard Jul 28 '14 at 17:21
4

run with xelatex

\documentclass{article}
\usepackage[svgnames]{pstricks}
\begin{document}

\begin{pspicture}[showgrid=false](-0.11,0)(1,5)
\psset{linecolor=IndianRed,doubleline}
\psecurve(0,0)(0,1.99)(0.2,2.4)(0.5,2.7)(0.8,2.7)(0.9,2.49)(0.9,2)
\psecurve[border=1pt](0.9,3)(0.9,2.51)(0.8,2.3)(0.5,2.3)(0.2,2.6)(0,3.01)(0,5)
\psline(0,2.01)  \psline(0,3)(0,5)
\uput{0.35cm}[-90](0,0){$X\bigotimes Y $}
\end{pspicture}
\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.