2

I have this code:

\documentclass{minimal}

\usepackage{tikz}
\usepackage{tkz-graph}

\usetikzlibrary{external,backgrounds}
%\expandafter\tikzexternalize[]


\begin{document}
    \begin{tikzpicture}[
        yscale=.75, 
        VertexStyle/.style={} % removes white fill from vertices
    ]
        \Vertex[L=$1$]{1}
        \NOEA[L=$2$](1){2}
        \NOEA[L=$3$](2){3}
        \NOEA[L=$4$](3){4}
        \NOEA[L=$5$](4){5}

        \SOEA[L=$2$](1){22}
        \NOEA[L=$3$](22){23}
        \NOEA[L=$4$](23){24}
        \NOEA[L=$5$](24){25}
        \NOEA[L=$6$](25){26}

        \SOEA[L=$3$](22){33}
        \NOEA[L=$4$](33){34}
        \NOEA[L=$5$](34){35}
        \NOEA[L=$6$](35){36}
        \NOEA[L=$7$](36){37}

        \Edges(1,2,3,4,5)
        \Edges(22,23,24,25,26)
        \Edges(33,34,35,36,37)
        \Edges(1,22,33)
        \Edges(2,23,34)
        \Edges(3,24,35)
        \Edges(4,25,36)
        \Edges(5,26,37)

        \begin{scope}[
            on background layer, % everything in the environment is drawn behind the vertices
            highlight/.style={rounded corners=1em,line width=1.5em,black,opacity=0.2,cap=round},
            whiten/.style={white,cap=round,line width=2em},
            highlightfill/.style={fill,line width=1.5em,rounded corners=1em,black,opacity=0.2,cap=round}
            ]
            % order is important !!!
            \draw [highlightfill] (2.center) -- (4.center) -- (25.center) -- (26.center) -- (37.center) -- (33.center) -- (1.center) -- (2.center);
        \end{scope}
    \end{tikzpicture}
\end{document}

producing the following:

enter image description here

Is it possible to have the outline and filling non-overlapping? Or extend the filling in a manner that its outer shape comes close to the depicted one?

PS: You could also have a look at TiKz and tikz-grph background of Graph.

  • If you don't actually need transparency you can just use black!20 in place of black,opacity=0.2. The fill and outline will still overlap, but you won't be able to see one through the other. – Emma Oct 18 '16 at 17:55
  • Ahh! Actualy no. Since I already put everything in background. Thank you. – milkpirate Oct 18 '16 at 19:33

2 Answers2

1

The solution below uses an idea from an answer to this question to clip the stroked path to only show the part outside the path itself. Unfortunately, depending on your viewer there may still be a slight overlap or gap.

\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{backgrounds}
\tikzset{reverseclip/.style={insert path={(-100cm,100cm) rectangle (100cm,-100cm)}}}
\newcommand{\drawoutside}[2][]{
    \begin{scope}[#1, even odd rule]
        \begin{pgfinterruptboundingbox}
            \clip[reverseclip] #2;
        \end{pgfinterruptboundingbox}
        \path[draw, fill=none] #2;
    \end{scope}
    \path [#1, draw=none] #2;
}

\begin{document}
    \begin{tikzpicture}[
      every node/.style={circle, fill=black, inner sep=1pt},
      highlightfill/.style={fill,rounded corners=1em, line width=1.5em,opacity=0.2,cap=round},
    ]
        \path (0,0) node[label=below:a] (a) {}
              (1,1) node[label=above:b] (b) {}
              (0,1) node[label=above:c] (c) {}
              (1,0) node[label=below:d] (d) {};

        \begin{scope}[on background layer]
            \drawoutside[highlightfill, blue]{(a.center) -- (c.center) -- (b.center) -- cycle}
            \drawoutside[highlightfill, red]{(a.center) -- (b.center) -- (d.center) -- cycle}
        \end{scope}
    \end{tikzpicture}
\end{document}

no overlap!

Emma
  • 3,453
  • 12
  • 20
1

I have just noticed that there is a much simpler solution to the transparency issue: transparency groups. Your original code can actually be used with only a small change.

\documentclass{minimal}

\usepackage{tikz}
\usepackage{tkz-graph}

\usetikzlibrary{external,backgrounds}
%\expandafter\tikzexternalize[]

\begin{document}
    \begin{tikzpicture}[
        yscale=.75,
        VertexStyle/.style={} % removes white fill from vertices
    ]
        \Vertex[L=$1$]{1}
        \NOEA[L=$2$](1){2}
        \NOEA[L=$3$](2){3}
        \NOEA[L=$4$](3){4}
        \NOEA[L=$5$](4){5}

        \SOEA[L=$2$](1){22}
        \NOEA[L=$3$](22){23}
        \NOEA[L=$4$](23){24}
        \NOEA[L=$5$](24){25}
        \NOEA[L=$6$](25){26}

        \SOEA[L=$3$](22){33}
        \NOEA[L=$4$](33){34}
        \NOEA[L=$5$](34){35}
        \NOEA[L=$6$](35){36}
        \NOEA[L=$7$](36){37}

        \Edges(1,2,3,4,5)
        \Edges(22,23,24,25,26)
        \Edges(33,34,35,36,37)
        \Edges(1,22,33)
        \Edges(2,23,34)
        \Edges(3,24,35)
        \Edges(4,25,36)
        \Edges(5,26,37)

        \begin{scope}[
            on background layer, % everything in the environment is drawn behind the vertices
            highlight/.style={rounded corners=1em,line width=1.5em,black,cap=round},
            whiten/.style={white,cap=round,line width=2em},
            highlightfill/.style={fill,line width=1.5em,rounded corners=1em,black,cap=round}
            ]
            % order is important !!!
            \begin{scope}[transparency group,opacity=0.2]
            \draw [highlightfill] (2.center) -- (4.center) -- (25.center) -- (26.center) -- (37.center) -- (33.center) -- (1.center) -- (2.center);
            \end{scope}
        \end{scope}
    \end{tikzpicture}
\end{document}

enter image description here

Emma
  • 3,453
  • 12
  • 20