我有这个代码:
\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}
生成以下内容:
轮廓和填充是否可以不重叠?或者以某种方式延伸填充,使其外形接近所描绘的形状?
PS: 你也可以看看TiKz 和 tikz-grph 图表背景。
答案1
下面的解决方案采用了以下答案中的想法这个问题剪切描边路径以仅显示路径本身之外的部分。不幸的是,取决于您的查看器可能仍有轻微重叠或间隙。
\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}
答案2
我刚刚注意到,透明度问题有一个更简单的解决方案:透明度组。实际上,只需进行少量更改即可使用您的原始代码。
\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}