我想用特定颜色填充节点,但不是用通常的[fill=blue]
方式。原因是,我正在创建许多节点,并且我想用与其他节点不同的颜色填充一些随机节点,因此着色不能嵌入到创建过程中。有什么想法吗?
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}
\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=gray!20,minimum size=20}]
\foreach \x in {0,...,2}
\foreach \y in {0,...,2}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle] (\x\y) at (1.5*\x,1.5*\y) {};}
\foreach \x in {0,...,2}
\foreach \y [count=\yi] in {0,...,1}
\draw (\x\y)--(\x\yi) ;
\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);
\foreach \x in {0,...,2}
\foreach \y in {0,...,3}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle] (\x\y) at (10+1.5*\x,1.5*\y) {};}
\foreach \x in {0,...,2}
\foreach \y [count=\yi] in {0,...,2}
\draw (\x\y)--(\x\yi) ;
\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);
\foreach \x in {0,...,5}
\foreach \y in {0,...,4}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle] (\x\y) at (3+1.5*\x,-10+1.5*\y) {};}
\foreach \x in {0,...,5}
\foreach \y [count=\yi] in {0,...,3}
\draw (\x\y)--(\x\yi) ;
\end{tikzpicture}
\end{document}
假设我们想用蓝色填充最左上角的节点和最右下角的节点。
答案1
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}
\begin{document}
\begin{tikzpicture}[
darkstyle/.style={circle,draw,fill=gray!20,minimum size=20},
bluestyle/.style={circle,draw,fill=blue!20,minimum size=20},
]
\foreach \x in {0,...,2}
\foreach \y in {0,...,2}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\ifnum\x=\ifnum\y=2 0\else-1\fi
\node [bluestyle] (\x\y) at (1.5*\x,1.5*\y) {};
\else
\node [darkstyle] (\x\y) at (1.5*\x,1.5*\y) {};
\fi
}
\foreach \x in {0,...,2}
\foreach \y [count=\yi] in {0,...,1}
\draw (\x\y)--(\x\yi) ;
\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);
\foreach \x in {0,...,2}
\foreach \y in {0,...,3}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle] (\x\y) at (10+1.5*\x,1.5*\y) {};}
\foreach \x in {0,...,2}
\foreach \y [count=\yi] in {0,...,2}
\draw (\x\y)--(\x\yi) ;
\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);
\foreach \x in {0,...,5}
\foreach \y in {0,...,4}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\ifnum\x=\ifnum\y=0 5\else-1\fi
\node [bluestyle] (\x\y) at (3+1.5*\x,-10+1.5*\y) {};
\else
\node [darkstyle] (\x\y) at (3+1.5*\x,-10+1.5*\y) {};
\fi
}
\foreach \x in {0,...,5}
\foreach \y [count=\yi] in {0,...,3}
\draw (\x\y)--(\x\yi) ;
\end{tikzpicture}
\end{document}
答案2
在您的问题中,您说您想填充随机节点。如果您想要真正随机的选择,您可以执行以下随机darkstyle
样式:
\documentclass[tikz, border=7pt]{standalone}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}
\tikzset{
% ----- the static part of the style
darkstyle/.style={
circle,minimum size=20,draw
},
% ----- the random part of the style
darkstyle/.append code = {
\pgfmathsetmacro\randcolor{{"red!50","blue!35"}[int(2*random())]}
\pgfkeysalso{fill=\randcolor}
}
}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,2}
\foreach \y in {0,...,2}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle] (\x\y) at (1.5*\x,1.5*\y) {};}
\foreach \x in {0,...,2}
\foreach \y [count=\yi] in {0,...,1}
\draw (\x\y)--(\x\yi) ;
\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);
\foreach \x in {0,...,2}
\foreach \y in {0,...,3}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle] (\x\y) at (10+1.5*\x,1.5*\y) {};}
\foreach \x in {0,...,2}
\foreach \y [count=\yi] in {0,...,2}
\draw (\x\y)--(\x\yi) ;
\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);
\foreach \x in {0,...,5}
\foreach \y in {0,...,4}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle] (\x\y) at (3+1.5*\x,-10+1.5*\y) {};}
\foreach \x in {0,...,5}
\foreach \y [count=\yi] in {0,...,3}
\draw (\x\y)--(\x\yi) ;
\end{tikzpicture}
\end{document}
编辑:如果您真的不想要随机选择,您可以规定一些节点具有不同的颜色,如下所示:
\documentclass[tikz, border=7pt]{standalone}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols}
\tikzset{
% ----- the static part of the style
darkstyle/.style={
circle,minimum size=20,draw, fill = blue!35
},
% ----- the prescribed part pf the style
mycolor/.style = {fill=red!35},
mycolorA12/.style = {mycolor},
mycolorA20/.style = {mycolor},
mycolorB11/.style = {mycolor},
mycolorC54/.style = {mycolor},
}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,2}
\foreach \y in {0,...,2}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle,mycolorA\x\y/.try] (\x\y) at (1.5*\x,1.5*\y) {};}
\foreach \x in {0,...,2}
\foreach \y [count=\yi] in {0,...,1}
\draw (\x\y)--(\x\yi) ;
\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);
\foreach \x in {0,...,2}
\foreach \y in {0,...,3}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle,mycolorB\x\y/.try] (\x\y) at (10+1.5*\x,1.5*\y) {};}
\foreach \x in {0,...,2}
\foreach \y [count=\yi] in {0,...,2}
\draw (\x\y)--(\x\yi) ;
\draw [decorate,decoration={brace,amplitude=7pt,raise=2pt,aspect=0.5}] (20.south) -- (00.south);
\foreach \x in {0,...,5}
\foreach \y in {0,...,4}
{\pgfmathtruncatemacro{\label}{\x - 5 * \y +21}
\node [darkstyle,mycolorC\x\y/.try] (\x\y) at (3+1.5*\x,-10+1.5*\y) {};}
\foreach \x in {0,...,5}
\foreach \y [count=\yi] in {0,...,3}
\draw (\x\y)--(\x\yi) ;
\end{tikzpicture}
\end{document}
答案3
首先,考虑一个更简单的例子(直接适用于原始示例的例子),以便未来的人们实际上能够理解和适应答案:
(node1)
分别(node2)
绘制为圆形和矩形,- 我们试图将前者重新染成蓝色。
我不知道是否可以重新着色,但如果您还没有在 上绘制其他东西(node1)
,那么在 上面绘制一个蓝色节点(node1)
可能会对您有用:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=gray!20,minimum size=20}]
\node [darkstyle] (node1) at (2,1) {};
\node [darkstyle, rectangle] (node2) at (4,2) {};
% What to write here to change the color of (node1) to blue?
% A workaround:
\node [darkstyle, fill=blue!30] at (node1) {};
\end{tikzpicture}
\end{document}
输出:
如果您的旧节点中也有一些文本,那么它也需要是覆盖节点的标签。这些可以是硬编码的,也可以存储在宏中并调用。让我们用(node2)
这个时间来做:
\documentclass{standalone}
\usepackage{tikz}
% Thanks to: https://tex.stackexchange.com/q/175707/69346
\newcommand\storelabel[2]{\expandafter\xdef\csname label#1\endcsname{#2}}
\newcommand\getlabel[1]{\csname label#1\endcsname}
\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=gray!20,minimum size=20}]
\node [darkstyle] (node1) at (2,1) {\storelabel{node1}{5.7}\getlabel{node1}};
\node [darkstyle, rectangle] (node2) at (4,2) {\storelabel{node2}{9000.1}\getlabel{node2}};
% What to write here to change the color of (node2) to blue?
% A workaround (note the "rectangle" in the options):
\node [darkstyle, rectangle, fill=blue!30] at (node2) {\getlabel{node2}};
\end{tikzpicture}
\end{document}
输出:
答案迟了 5 年,因为其他人完全忽略了原始请求中的“创建后”部分。我提醒您,这个方法也不会重新着色已绘制的节点,因此存在缺点,并且不适用于所有人。但是,这里提供的解决方法至少在创建后会按照要求做一些事情。
我想说,这个问题即使在此之后仍然需要一个答案。