5

How can I add an enveloping border around the blue line in this MWE?

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[blue, line width=10pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 
\end{tikzpicture}
\end{document}

enter image description here

PS. I found out that something similar is possible with MetaPost in this previous answer https://tex.stackexchange.com/a/330016, but I'd like to know if this is also possible with Tikz.

  • 1
    Why not another line with thickness of 8pt inside,
    \draw[white, line width=8pt,shorten >=1pt,shorten <=1pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0);
    – Salim Bou Jul 17 '17 at 11:00
  • tikz offers the option [double] which does what @SalimBou proposes, it draws the path twice with two different thicknesses. However, it does not close the path ends. For that, you would have to construct a secondary path, along the lines of ($0+5pt$,0) -- (with calc library). If there is a way to automate that, I am unaware of it. – Huang_d Jul 17 '17 at 12:13
  • What's an enveloping border? Around what? The picture? The line? – cfr Jul 17 '17 at 15:29
  • @Huang_d You can shorten only the main path, though, which would have the same effect, I think. And do it in preaction (or postaction) rather than using double. – cfr Jul 17 '17 at 15:32
  • The suggestion of @salim-bou works fine for my case. – Arianna Angeletti Jul 17 '17 at 16:32
  • @cfr Around the line. – Arianna Angeletti Jul 17 '17 at 16:35
  • 1
    @SalimBou Would you like to write up an answer? – Torbjørn T. Aug 28 '17 at 21:36
  • @SalimBou I think shorten should be negative to enlarge the line at endings. – Ignasi Mar 28 '18 at 07:04

2 Answers2

2

This is Salim Bou solution with negative shorten values and declared as a style which adds a preaction.

Style border has three parameters:lower line width, border width (applied to shorten) and color.

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[border/.style n args={3}{
    preaction={draw=#3, line width=#1, shorten <=-#2, shorten >=-#2}}]
\draw[blue, line width=10pt, border={14pt}{2pt}{green}](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 

\draw[blue, line width=10pt, border={14pt}{2pt}{green}](3,0)rectangle++(2,3); 

\draw[blue, line width=10pt, border={14pt}{2pt}{green}] (7,1) circle(1cm);

\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
1

A solution based on this answer. This answer comes with an Outline style, which has one argument, the distance from the center to the line to the contour. The width is then twice this length.

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{decorations}

% based on https://tex.stackexchange.com/a/103088/121799
\def\pgfdecoratedcontourdistance{0pt}

\pgfkeys{/pgf/decoration/contour distance/.code={%
    \pgfmathparse{#1}%
    \let\pgfdecoratedcontourdistance=\pgfmathresult},%
    /pgf/decoration/contour name/.store in=\ContourName,
    /pgf/decoration/contour name=mycontour
}

\pgfdeclaredecoration{contour lineto}{start}
{
    \state{start}[next state=draw, width=0pt]{
    \pgfcoordinate{\ContourName-0}{\pgfpoint{0pt}{\pgfdecoratedcontourdistance}}
        \pgfpathlineto{\pgfpoint{0pt}{\pgfdecoratedcontourdistance}}%
    }
    \state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{       
        \pgfmathparse{-\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
        \let\shorten=\pgfmathresult%
        \pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength+\shorten}{\pgfdecoratedcontourdistance}}%  
    %\stepcounter{Outline}
    \pgfcoordinate{\ContourName-1}{\pgfpoint{\pgfdecoratedinputsegmentlength+\shorten}{\pgfdecoratedcontourdistance}}
    }
    \state{final}{
        \pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength}{\pgfdecoratedcontourdistance}}%
        \pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength}{0pt}}
    }   
}
\tikzset{Outline/.style={ postaction={
        decoration={contour lineto, contour distance=-#1,contour name=mycontourA},draw=blue,
         decorate},
        postaction={
        decoration={contour lineto, contour distance=#1,contour name=mycontourB},draw=blue,
         decorate},}}
\begin{document}

\begin{tikzpicture}
\draw[red,line width=10pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 
\path[blue,very thick,Outline=5pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 

\begin{scope}[xshift=4cm]
\path[blue,line width=2pt,Outline=5pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 
\end{scope}
\begin{scope}[xshift=8cm,scale=2]
\path[blue,line width=4pt,Outline=10pt](0,0)--(90:1.5)--(60:2.5)--(30:3.0); 
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

NOTE: This works only with polygons which are not closed.