我有一个计划显示的 tikz 图像,然后显示一个新版本,其中突出显示一个节点,其他节点静音。我想将 SSS 和 SSI 内所有节点的填充量更改为fill=color!50
。(值得一提的是,它位于一个更大的数字内,我希望这样做)。
\documentclass{standalone}
\usepackage{tikz}
\definecolor{colorS}{RGB}{0,154,128}
\definecolor{colorI}{RGB}{255,32,0}
\tikzstyle{SusCirc}=[circle,draw=black!50,fill=colorS!90,thick, minimum size=2mm]%a circle
%representing a susceptible individual
\tikzstyle{InfCirc}=[circle,draw=black!50,fill=colorI!90,thick, minimum size=2mm]%a circle
%representing an infected individual
\tikzstyle{BigCirc}=[circle,draw=black!50,thick,inner sep=0pt,minimum size=0pt]
\begin{document}
\begin{tikzpicture}
\node[BigCirc] (SSS) at (0.000000, 0.000000) {\scalebox{0.25}{\begin{tikzpicture}
\node[SusCirc, minimum size = 8mm] (center) at (0.000000,0.000000) {};
\node[SusCirc, minimum size = 8mm] (left) at (1.000000,0.000000) {};
\node[SusCirc, minimum size = 8mm] (top) at (-1.000000,0.000000) {};
\draw [-] (center) -- (left);
\draw [-] (center) -- (top);
\end{tikzpicture}}};
\node[BigCirc] (SSI) at (2.954020, -1.762016) {\scalebox{0.25}{\begin{tikzpicture}
\node[SusCirc, minimum size = 8mm] (center) at (0.000000,0.000000) {};
\node[SusCirc, minimum size = 8mm] (left) at (1.000000,0.000000) {};
\node[InfCirc, minimum size = 8mm] (top) at (-1.000000,0.000000) {};
\draw [-] (center) -- (left);
\draw [-] (center) -- (top);
\end{tikzpicture}}};
\node[BigCirc] (SIS) at (4.237137, -0.360863) {\scalebox{0.25}{\begin{tikzpicture}
\node[SusCirc, minimum size = 8mm] (center) at (0.000000,0.000000) {};
\node[InfCirc, minimum size = 8mm] (left) at (1.000000,0.000000) {};
\node[SusCirc, minimum size = 8mm] (top) at (-1.000000,0.000000) {};
\draw [-] (center) -- (left);
\draw [-] (center) -- (top);
\end{tikzpicture}}};
\draw [-] (SSS) -- (SSI);
\draw [-] (SSS) -- (SIS);
\end{tikzpicture}
\end{document}
这是当前的外观。(抱歉——显然防火墙阻止了我的第一次上传???)
如上所述:我想将 SSS 和 SSI 内所有节点的填充量更改为fill=color!50
。一定有一种方法可以做到这一点,而不必编辑每个节点。
明确地说:我想编写一个命令来更改我的某个BigCirc
s 中所有节点的填充分数。我给出的是一个最小的工作示例,但我拥有的其中包含更多节点。
答案1
一般来说,我认为嵌套tikzpicture
s 是应该避免的。为此,您可以尝试定义pic
,这是 TikZ 3.0 中引入的。在这种情况下,您可以轻松修改fill opacity
(或只是opacity
,这也会影响外圆),如下例所示。
这里我定义了一个pic
接受三个参数的函数,它们是三个小圆圈的样式,第一个参数表示左边的圆圈,第二个参数表示中间的圆圈,第三个参数表示右边的圆圈。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}
\definecolor{colorS}{RGB}{0,154,128}
\definecolor{colorI}{RGB}{255,32,0}
\tikzset{
%a circle representing a susceptible individual
SusCirc/.style={circle,draw=black!50,fill=colorS!90, minimum size=2mm,inner sep=0pt},
%a circle representing an infected individual
InfCirc/.style={circle,draw=black!50,fill=colorI!90,minimum size=2mm,inner sep=0pt},
BigCirc/.style={circle,draw=black!50,thick,inner sep=1pt,minimum size=0pt},
%
pics/circles/.style args={#1#2#3}{
code={
\node [#1] (LeftDot) at (0,0) {};
\node [#2,right=2pt of LeftDot] (CenterDot) {};
\node [#3,right=2pt of CenterDot] (RightDot) {};
\draw (LeftDot) -- (CenterDot) -- (RightDot);
\node[BigCirc, fit = (LeftDot)(RightDot)] (circ) {};
}
}
}
\begin{document}
\begin{tikzpicture}
\pic (SSS) at (0.000000, 0.000000) {circles={SusCirc}{SusCirc}{SusCirc}};
\pic (SSI) at (2.954020, -1.762016) {circles={InfCirc}{SusCirc}{SusCirc}};
\pic (SIS) at (4.237137, -0.360863) {circles={SusCirc}{SusCirc}{InfCirc}};
\draw (SSScirc) -- (SSIcirc);
\draw (SSScirc) -- (SIScirc);
\end{tikzpicture}
\begin{tikzpicture}
\pic (SSS) at (0.000000, 0.000000) {circles={SusCirc}{SusCirc}{SusCirc}};
\pic (SSI) [fill opacity=0.2] at (2.954020, -1.762016) {circles={InfCirc}{SusCirc}{SusCirc}};
\pic (SIS) [opacity=0.2] at (4.237137, -0.360863) {circles={SusCirc}{SusCirc}{InfCirc}};
\draw (SSScirc) -- (SSIcirc);
\draw (SSScirc) -- (SIScirc);
\end{tikzpicture}
\end{document}
旧代码-三个不同pic
的
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\definecolor{colorS}{RGB}{0,154,128}
\definecolor{colorI}{RGB}{255,32,0}
\tikzset{
%a circle representing a susceptible individual
SusCirc/.style={circle,draw=black!50,fill=colorS!90, minimum size=2mm,inner sep=0pt},
%a circle representing an infected individual
InfCirc/.style={circle,draw=black!50,fill=colorI!90,minimum size=2mm,inner sep=0pt},
BigCirc/.style={circle,draw=black!50,thick,inner sep=0pt,minimum size=0pt},
%
allgrey/.pic={
\node [draw=black!50,thick,circle,minimum size=8mm,inner sep=0pt] (OuterCircle) {};
\node [SusCirc,right=0.4pt] (LeftDot) at (OuterCircle.west) {};
\node [SusCirc,left=0.4pt] (RightDot) at (OuterCircle.east) {};
\node [SusCirc] (CenterDot) at (OuterCircle.center) {};
\draw (LeftDot) -- (CenterDot) -- (RightDot);
},
%
leftred/.pic={
\node [draw=black!50,thick,circle,minimum size=8mm,inner sep=0pt] (OuterCircle) {};
\node [InfCirc,right=0.4pt] (LeftDot) at (OuterCircle.west) {};
\node [SusCirc,left=0.4pt] (RightDot) at (OuterCircle.east) {};
\node [SusCirc] (CenterDot) at (OuterCircle.center) {};
\draw (LeftDot) -- (CenterDot) -- (RightDot);
},
rightred/.pic={
\node [draw=black!50,thick,circle,minimum size=8mm,inner sep=0pt] (OuterCircle) {};
\node [SusCirc,right=0.4pt] (LeftDot) at (OuterCircle.west) {};
\node [InfCirc,left=0.4pt] (RightDot) at (OuterCircle.east) {};
\node [SusCirc] (CenterDot) at (OuterCircle.center) {};
\draw (LeftDot) -- (CenterDot) -- (RightDot);
}
}
\begin{document}
\begin{tikzpicture}
\pic (SSS) at (0.000000, 0.000000) {allgrey};
\pic (SSI) at (2.954020, -1.762016) {leftred};
\pic (SIS) at (4.237137, -0.360863) {rightred};
\draw (SSSOuterCircle) -- (SSIOuterCircle);
\draw (SSSOuterCircle) -- (SISOuterCircle);
\end{tikzpicture}
\begin{tikzpicture}
\pic (SSS) [fill opacity=0.5] at (0.000000, 0.000000) {allgrey};
\pic (SSI) [fill opacity=0.5] at (2.954020, -1.762016) {leftred};
\pic (SIS) at (4.237137, -0.360863) {rightred};
\draw (SSSOuterCircle) -- (SSIOuterCircle);
\draw (SSSOuterCircle) -- (SISOuterCircle);
\end{tikzpicture}
\end{document}
答案2
尝试使用节点选项来确定节点突出显示的不透明度:
\documentclass[border=3mm,
tikz]{standalone}
\usetikzlibrary{chains,fit,positioning}
\definecolor{colorS}{RGB}{0,154,128}
\definecolor{colorI}{RGB}{255,32,0}
\makeatletter
\tikzset{reset/.code={\def\tikz@after@path{}}}
\makeatother
\begin{document}
\begin{tikzpicture}[
node distance = 1mm,
start chain = going right,
every node/.style = {draw=black!50, circle,
inner sep=0pt, minimum size=2mm,
node contents={}},
SusCirc/.style = {fill=colorS!#1,% #1 determine color intensity
join=by -, on chain},
InfCirc/.style = {fill=colorI!#1, join=by -, on chain},
BigCirc/.style = {very thick}
]
\node (s11) [SusCirc=90];
\node (s12) [SusCirc=90];
\node (s13) [SusCirc=90];
\node (SSS) [BigCirc,fit=(s11) (s13)];
%
\node (s21) [SusCirc=90,reset,
below right=0mm and 55mm of SSS];
\node (s22) [SusCirc=90];
\node (s23) [InfCirc=90];
\node (SSI) [BigCirc,fit=(s21) (s23)];
%
\node (s31) [InfCirc=50,reset,
below right=21mm and 33mm of SSS];
\node (s32) [SusCirc=60];
\node (s33) [SusCirc=30];
\node (ISS) [BigCirc,fit=(s31) (s33)];
%
\draw (SSS) -- (SSI) (SSS) -- (ISS);
\end{tikzpicture}
\end{document}
为了使代码更简单、更紧凑,我使用了三个 tikz 库:(chain
用于定位小圆圈)、fit
(用于将大圆圈放在小圆圈周围,从而避免 tikz 图片嵌套)和positioning
(用于定位圆圈组)。颜色强度选项的使用应该是自我描述的。
编辑:通过使用函数可以进一步简化我的代码default
。上面的代码如下:
\documentclass[border=3mm,
tikz]{standalone}
\usetikzlibrary{chains,fit,positioning}
\definecolor{colorS}{RGB}{0,154,128}
\definecolor{colorI}{RGB}{255,32,0}
\makeatletter
\tikzset{reset/.code={\def\tikz@after@path{}}}
\makeatother
\begin{document}
\begin{tikzpicture}[
node distance = 1mm,
start chain = going right,
every node/.style = {draw=black!50, circle,
inner sep=0pt, minimum size=2mm,
node contents={}},
SusCirc/.style = {fill=colorS!#1,% #1 determine color intensity
join=by -, on chain},
SusCirc/.default = 90,
InfCirc/.style = {fill=colorI!#1, join=by -, on chain},
InfCirc/.default = 90,
BigCirc/.style = {very thick},
]
\node (s11) [SusCirc];
\node (s12) [SusCirc];
\node (s13) [SusCirc];
\node (SSS) [BigCirc,fit=(s11) (s13)];
%
\node (s21) [SusCirc,reset,
below right=0mm and 55mm of SSS];
\node (s22) [SusCirc];
\node (s23) [InfCirc];
\node (SSI) [BigCirc,fit=(s21) (s23)];
%
\node (s31) [InfCirc=50,reset,
below right=21mm and 33mm of SSS];
\node (s32) [SusCirc=60];
\node (s33) [SusCirc=30];
\node (ISS) [BigCirc,fit=(s31) (s33)];
%
\draw (SSS) -- (SSI) (SSS) -- (ISS);
\end{tikzpicture}
\end{document}
好吧,这仍然不是您想要获得的,但是,请尝试以下步骤:
\begin{scope}[fill opacity=0.5% or whatever value you like to have
]
\node (s31) [InfCirc,reset,
below right=21mm and 33mm of SSS];
\node (s32) [SusCirc];
\node (s33) [SusCirc];
\end{scope}
如果这令人满意,那么您可以省略 SusCirc 和 InCirc 的填充选项以及默认设置。