# How to improve on this "twolines" decoration?

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}


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.

• I think you can get away with double in many cases. Jul 25, 2014 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. Jul 25, 2014 at 17:41
• knots or braids? Jul 25, 2014 at 17:47
• knots, see my update. Jul 25, 2014 at 17:48
• double doesn't work with knot because knot invokes double internally. Jul 28, 2014 at 21:18

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}


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}


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

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}


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}


• This doesn't really answer how to improve the decoration, right? The way how the curve looks exactly is irrelevant. Jul 28, 2014 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. Jul 28, 2014 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. Jul 28, 2014 at 17:15
• Sorry, your solution does look very good, thank you for the effort anyways! Jul 28, 2014 at 17:17
• I have another idea, that I'll fine-tune and propose in a moment. Jul 28, 2014 at 17:21

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}