TikZ: How to draw a pattern at the border of a not closed tikz path

This is a generalization of your code that does that. It distinguishes between closed and non-closed paths.

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{decorations,backgrounds}

% BEGIN from https://tex.stackexchange.com/a/419558/11820
\newcounter{tmp}
\newif\ifpathisclosed

%\tikzstyle{D}=[draw=blue,line width=1pt,preaction={clip, postaction={draw=blue,opacity=0.5,line width=12pt}}]
%<- note that tikzstyle is deprecated

\tikzset{D/.style={
    preaction={draw=blue,line width=1pt},
    preaction={decoration={contour lineto closed, contour distance=6pt},
      decorate,
    },
    postaction={
      insert path={%
        \pgfextra{%
          \pgfinterruptpath
          \begin{scope}[opacity=0.5, transparency group]
            \path[fill=blue,even odd rule] 
            \mySecondList \myList 
            ;
          \end{scope}
        \endpgfinterruptpath}
    }},
}}



\tikzset{EDR/.style={
    preaction={draw=red,line width=1pt},
    preaction={decoration={contour lineto closed, contour distance=6pt},
      decorate,
    },
    postaction={
      insert path={%
        \pgfextra{%
          \pgfinterruptpath
          \path[pattern=north west lines, pattern color=red,even odd rule] 
          \mySecondList \myList 
          ;
        \endpgfinterruptpath}
    }},
}}

\makeatletter
\def\pgfdecoratedcontourdistance{0pt}
\pgfset{
  decoration/contour distance/.code=%
\pgfmathsetlengthmacro\pgfdecoratedcontourdistance{#1}}
\pgfdeclaredecoration{contour lineto closed}{start}{%
  \state{start}[
  next state=draw,
  width=0pt,
  persistent precomputation=\let\pgf@decorate@firstsegmentangle\pgfdecoratedangle]{%
    %\xdef\myList{}\xdef\mySecondList{}%
    \setcounter{tmp}{0}%
    \global\pathisclosedfalse%
    \pgfpathmoveto{\pgfpointlineattime{.5}
      {\pgfqpoint{0pt}{\pgfdecoratedcontourdistance}}
    {\pgfqpoint{\pgfdecoratedinputsegmentlength}{\pgfdecoratedcontourdistance}}}%
  }%
  \state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{%
    \ifpgf@decorate@is@closepath@%
      \pgfmathsetmacro\pgfdecoratedangletonextinputsegment{%
      -\pgfdecoratedangle+\pgf@decorate@firstsegmentangle}%
    \fi
    \pgfmathsetlengthmacro\pgf@decoration@contour@shorten{%
    -\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
    \pgfpathlineto
    {\pgfpoint{\pgfdecoratedinputsegmentlength+\pgf@decoration@contour@shorten}
    {\pgfdecoratedcontourdistance}}%
    \stepcounter{tmp}%
    \pgfcoordinate{muemmel\number\value{tmp}}{\pgfpoint{\pgfdecoratedinputsegmentlength+\pgf@decoration@contour@shorten}
    {\pgfdecoratedcontourdistance}}%
    \pgfcoordinate{feep\number\value{tmp}}{\pgfpoint{\pgfdecoratedinputsegmentlength}{0pt}}%      
    \ifnum\value{tmp}=1\relax%
     \pgfcoordinate{muemmel0}{\pgfpoint{0pt}{\pgfdecoratedcontourdistance}}%
     \pgfcoordinate{feep0}{\pgfpoint{0pt}{0pt}}%      
     \xdef\myList{(muemmel\number\value{tmp})}%
     \xdef\mySecondList{(feep\number\value{tmp})}%
    \else
     \xdef\myList{\myList -- (muemmel\number\value{tmp})}%
     \xdef\mySecondList{(feep\number\value{tmp}) -- \mySecondList}%
    \fi 
    \ifpgf@decorate@is@closepath@%
      \pgfpathclose
      \global\pathisclosedtrue%
    \fi
  }%
  \state{final}{%\typeout{\myList,\mySecondList}%
    \ifpathisclosed%
      \xdef\myList{\myList -- cycle}% 
      \xdef\mySecondList{\mySecondList -- cycle}%
      %\typeout{closed \mySecondList \myList }
    \else
      %\typeout{\number\value{tmp}}%
      \xdef\myList{(muemmel0) -- \myList -- cycle}% 
      \xdef\mySecondList{\mySecondList -- (feep0) --}%
      %\typeout{not closed \mySecondList \myList }%   
    \fi
  }%
}
\makeatother
\tikzset{
  contour/.style={
    decoration={
      name=contour lineto closed,
      contour distance=#1
    },
decorate}}
% END from https://tex.stackexchange.com/a/419558/11820

\tikzset{EDO/.style={
    preaction={draw=orange,line width=1pt},
    preaction={decoration={contour lineto closed, contour distance=6pt},
      decorate,
    },
    postaction={
      insert path={%
        \pgfextra{%
          \pgfinterruptpath
          \path[pattern=north east lines, pattern color=orange,even odd rule] 
          \mySecondList \myList 
          ;
        \endpgfinterruptpath}
    }},
}}
\begin{document}

\begin{tikzpicture}[]
  \path [EDO] (2.875,3) -- ++(0,.75) -- ++(-2.875,0) -- ++(0,-.75) -- cycle;
  \path [EDR] (0,3) -- ++(0,-1) -- ++(6,0) -- ++(0,1) -- ++(-3.125,0) -- cycle ;
  \fill [white] (.21,3.3) rectangle (2.655,2.7);

  \begin{scope}[yshift=-2cm]
    \path [EDO] (2.875,3) -- ++(0,.75) -- ++(-2.875,0) -- ++(0,-.75) -- cycle;
    \path [EDR] (0,3) -- ++(0,-1) -- ++(6,0) -- ++(0,1) -- ++(-3.125,0) -- cycle ;
  \end{scope}

  \begin{scope}[yshift=-4cm]
    \path [EDO,draw, orange, line width=1pt] (2.875,3) -- ++(0,.75) -- ++(-2.875,0) -- ++(0,-.75);
    \path [EDR,draw, red, line width=1pt] (0,3) -- ++(0,-1) -- ++(6,0) -- ++(0,1) -- ++(-3.125,0);
  \end{scope}

  \begin{scope}[yshift=-6cm]
    \path [EDO,draw, orange, line width=1pt] (2.875,3) -- ++(0,.75) -- ++(-2.875,0) -- ++(0,-.75);
    \path [EDR,draw, red, line width=1pt,shorten >=6pt] (0,3) -- ++(0,-1) -- ++(6,0) -- ++(0,1) 
    -- ++(-3.125cm-6pt,0);
  \end{scope}
\end{tikzpicture}

\end{document}

enter image description here

Counting from the top, the third (or next-to-last) path is what you get by default. This is because the path does not know that the other path is what it is. To generate your desired output, one has to use shorten, see the fourth (or last) picture.

This is a more streamlined version thereof.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{patterns,decorations}

% BEGIN from https://tex.stackexchange.com/a/419558/11820


\tikzset{D/.style={
    preaction={draw=blue,line width=1pt},
    preaction={decoration={contour lineto closed, contour distance=6pt},
      decorate,
    },
    postaction={
      insert path={%
        \pgfextra{%
          \pgfinterruptpath
          \begin{scope}[opacity=0.5, transparency group]
            \path[fill=blue,even odd rule] 
            \mySecondList \myList 
            ;
          \end{scope}
        \endpgfinterruptpath}
    }},
}}



\tikzset{EDR/.style={
    preaction={draw=red,line width=1pt},
    preaction={decoration={contour lineto closed, contour distance=6pt},
      decorate,
    },
    postaction={
      insert path={%
        \pgfextra{%
          \pgfinterruptpath
          \path[pattern=north west lines, pattern color=red,even odd rule] 
          \mySecondList \myList 
          ;
        \endpgfinterruptpath}
    }},
}}

\makeatletter
\newif\ifdeco@path@is@closed
\newcounter{deco@count}

\def\pgfdecoratedcontourdistance{0pt}
\pgfset{
  decoration/contour distance/.code=%
\pgfmathsetlengthmacro\pgfdecoratedcontourdistance{#1}}
\pgfdeclaredecoration{contour lineto closed}{start}{%
  \state{start}[
  next state=draw,
  width=0pt,
  persistent precomputation=\let\pgf@decorate@firstsegmentangle\pgfdecoratedangle]{%
    %\xdef\myList{}\xdef\mySecondList{}%
    \setcounter{deco@count}{0}%
    \global\deco@path@is@closedfalse%
    \pgfpathmoveto{\pgfpointlineattime{.5}
      {\pgfqpoint{0pt}{\pgfdecoratedcontourdistance}}
    {\pgfqpoint{\pgfdecoratedinputsegmentlength}{\pgfdecoratedcontourdistance}}}%
  }%
  \state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{%
    \ifpgf@decorate@is@closepath@%
      \pgfmathsetmacro\pgfdecoratedangletonextinputsegment{%
      -\pgfdecoratedangle+\pgf@decorate@firstsegmentangle}%
    \fi
    \pgfmathsetlengthmacro\pgf@decoration@contour@shorten{%
    -\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
    \pgfpathlineto
    {\pgfpoint{\pgfdecoratedinputsegmentlength+\pgf@decoration@contour@shorten}
    {\pgfdecoratedcontourdistance}}%
    \stepcounter{deco@count}%
    \pgfcoordinate{muemmel\number\value{deco@count}}{\pgfpoint{\pgfdecoratedinputsegmentlength+\pgf@decoration@contour@shorten}
    {\pgfdecoratedcontourdistance}}%
    \pgfcoordinate{feep\number\value{deco@count}}{\pgfpoint{\pgfdecoratedinputsegmentlength}{0pt}}%      
    \ifnum\value{deco@count}=1\relax%
     \pgfcoordinate{muemmel0}{\pgfpoint{0pt}{\pgfdecoratedcontourdistance}}%
     \pgfcoordinate{feep0}{\pgfpoint{0pt}{0pt}}%      
     \xdef\myList{(muemmel\number\value{deco@count})}%
     \xdef\mySecondList{(feep\number\value{deco@count})}%
    \else
     \xdef\myList{\myList -- (muemmel\number\value{deco@count})}%
     \xdef\mySecondList{(feep\number\value{deco@count}) -- \mySecondList}%
    \fi 
    \ifpgf@decorate@is@closepath@%
      \pgfpathclose
      \global\deco@path@is@closedtrue%
    \fi
  }%
  \state{final}{%\typeout{\myList,\mySecondList}%
    \ifdeco@path@is@closed%
      \xdef\myList{\myList -- cycle}% 
      \xdef\mySecondList{\mySecondList -- cycle}%
      %\typeout{closed \mySecondList \myList }
    \else
      %\typeout{\number\value{deco@count}}%
      \xdef\myList{(muemmel0) -- \myList -- cycle}% 
      \xdef\mySecondList{\mySecondList -- (feep0) --}%
      %\typeout{not closed \mySecondList \myList }%   
    \fi
  }%
}
\makeatother
\tikzset{
  contour/.style={
    decoration={
      name=contour lineto closed,
      contour distance=#1
    },
decorate}}
% END from https://tex.stackexchange.com/a/419558/11820

\tikzset{EDO/.style={
    preaction={draw=orange,line width=1pt},
    preaction={decoration={contour lineto closed, contour distance=6pt},
      decorate,
    },
    postaction={
      insert path={%
        \pgfextra{%
          \pgfinterruptpath
          \path[pattern=north east lines, pattern color=orange,even odd rule] 
          \mySecondList \myList 
          ;
        \endpgfinterruptpath}
    }},
}}
\begin{document}

\begin{tikzpicture}[]
    \path [EDO,draw, orange, line width=1pt] (2.875,3) -- ++(0,.75) -- ++(-2.875,0) -- ++(0,-.75);
    \path [EDR,draw, red, line width=1pt,shorten >=6pt-\pgflinewidth/2] (0,3) -- ++(0,-1) -- ++(6,0) -- ++(0,1) 
    -- ++(-3.125cm-6pt,0);
\end{tikzpicture}

\end{document}

enter image description here

I cleaned up a bit. However, there is one thing that I did not do: stop using \pgfextra for paths. This is something that is to be avoided. In this very case it does seem OK. One could also write a cleaner version, but this would require you to add an extra command for the pattern. Please let me know if you want this to be spelled out.


One way to do it is the remove the draw part from the decoration, then draw the lines after the decoration are made, as your third example.

I modified your MWE:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{decorations,backgrounds}

% BEGIN from https://tex.stackexchange.com/a/419558/11820
\newcounter{tmp}

%\tikzstyle{D}=[draw=blue,line width=1pt,preaction={clip, postaction={draw=blue,opacity=0.5,line width=12pt}}]
%<- note that tikzstyle is deprecated

\tikzset{D/.style={
    preaction={draw=blue,line width=1pt},
    preaction={decoration={contour lineto closed, contour distance=6pt},
      decorate,
    },
    postaction={
      insert path={%
        \pgfextra{%
          \pgfinterruptpath
          \begin{scope}[opacity=0.5, transparency group]
            \path[fill=blue,even odd rule] 
            \mySecondList \myList 
            ;
          \end{scope}
        \endpgfinterruptpath}
    }},
}}



\tikzset{EDR/.style={
   % preaction={draw=red,line width=1pt},    %<- remove this line
    preaction={decoration={contour lineto closed, contour distance=6pt},
      decorate,
    },
    postaction={
      insert path={%
        \pgfextra{%
          \pgfinterruptpath
          \path[pattern=north west lines, pattern color=red,even odd rule] 
          \mySecondList \myList 
          ;
        \endpgfinterruptpath}
    }},
}}

\makeatletter
\def\pgfdecoratedcontourdistance{0pt}
\pgfset{
  decoration/contour distance/.code=%
\pgfmathsetlengthmacro\pgfdecoratedcontourdistance{#1}}
\pgfdeclaredecoration{contour lineto closed}{start}{%
  \state{start}[
  next state=draw,
  width=0pt,
  persistent precomputation=\let\pgf@decorate@firstsegmentangle\pgfdecoratedangle]{%
    \pgfextra{\xdef\myList{}\xdef\mySecondList{}}
    \pgfextra{\setcounter{tmp}{0}}
    \pgfpathmoveto{\pgfpointlineattime{.5}
      {\pgfqpoint{0pt}{\pgfdecoratedcontourdistance}}
    {\pgfqpoint{\pgfdecoratedinputsegmentlength}{\pgfdecoratedcontourdistance}}}%
  }%
  \state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{%
    \ifpgf@decorate@is@closepath@%
      \pgfmathsetmacro\pgfdecoratedangletonextinputsegment{%
      -\pgfdecoratedangle+\pgf@decorate@firstsegmentangle}%
    \fi
    \pgfmathsetlengthmacro\pgf@decoration@contour@shorten{%
    -\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
    \pgfpathlineto
    {\pgfpoint{\pgfdecoratedinputsegmentlength+\pgf@decoration@contour@shorten}
    {\pgfdecoratedcontourdistance}}%
    \stepcounter{tmp}
    \pgfcoordinate{muemmel\thetmp}{\pgfpoint{\pgfdecoratedinputsegmentlength+\pgf@decoration@contour@shorten}
    {\pgfdecoratedcontourdistance}}
    \pgfcoordinate{feep\thetmp}{\pgfpoint{\pgfdecoratedinputsegmentlength}{0pt}}      
    \pgfextra{\xdef\myList{\myList (muemmel\thetmp) -- }%
    \xdef\mySecondList{\mySecondList (feep\thetmp) -- }}
    \ifpgf@decorate@is@closepath@%
      \pgfpathclose
      \pgfextra{\xdef\myList{\myList cycle}% 
      \xdef\mySecondList{\mySecondList cycle}}
    \fi
  }%
  \state{final}{\pgfextra{%\typeout{\myList,\mySecondList}
  }}%
}
\makeatother
\tikzset{
  contour/.style={
    decoration={
      name=contour lineto closed,
      contour distance=#1
    },
decorate}}
% END from https://tex.stackexchange.com/a/419558/11820

\tikzset{EDO/.style={
  %  preaction={draw=orange,line width=1pt},    %<- remove this line
    preaction={decoration={contour lineto closed, contour distance=6pt},
      decorate,
    },
    postaction={
      insert path={%
        \pgfextra{%
          \pgfinterruptpath
          \path[pattern=north east lines, pattern color=orange,even odd rule] 
          \mySecondList \myList 
          ;
        \endpgfinterruptpath}
    }},
}}
\begin{document}

\begin{tikzpicture}[]
  \node at (-.5,0) {a)};
  \path [EDO] (2.875,0) -- ++(0,.75) -- ++(-2.875,0) -- ++(0,-.75) -- cycle;
  \path [EDR] (0,0) -- ++(0,-1) -- ++(6,0) -- ++(0,1) -- ++(-3.125,0) -- cycle ;
  \fill [white] (.21,.3) rectangle (2.655,-.3);
  \path [draw, orange, line width=1pt] (2.875,0) -- ++(0,.75) -- ++(-2.875,0) -- ++(0,-.75);
  \path [draw, red, line width=1pt] (0,0) -- ++(0,-1) -- ++(6,0) -- ++(0,1) -- ++(-3.125,0);
\end{tikzpicture}

\end{document}

This gives the following enter image description here


I note that the above code is long, so I use plain TikZ. Meaning of parameters is clear I hope! You can change color as you like.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
[line width=2pt,line join=round,line cap=round,thick]
\def\a{3} \def\b{1} \def\c{3.5} \def\d{1}
\draw[red,pattern=north west lines,pattern color=red!50] 
(0,\b)|-(\a+\c,0)|-(\a,\b); 
\draw[orange,pattern=north east lines,pattern color=orange!50]
(\a,\b)--++(90:\d)--++(180:\a)--++(-90:\d); 
\def\m{.2}
\path 
(0,0)+(\m,\m) coordinate (A)
(\a,\b+\d)+(-\m,-\m) coordinate (B)
(\a+\c,\b)+(-\m,-\m) coordinate (C);
\fill[white] (A) rectangle (B) (A) rectangle (C);
\end{tikzpicture}
\end{document}