在 tikz 中设置宏来为节点着色

在 tikz 中设置宏来为节点着色

我有以下代码

\documentclass[a0paper,landscape]{article}
\usepackage{tikz}
\usepackage{geometry}
\usetikzlibrary{arrows,patterns,shapes,positioning}
\geometry{margin = .5in}


\begin{document}
    \centering
    \begin{tikzpicture}[
        roundnode/.style = {circle,very thick,draw=blue!70,fill=blue!5,minimum size=7mm},
        cloudnode/.style = {cloud,cloud puffs=10, cloud puff arc=60,draw=cyan!70,fill=cyan!5,minimum width = 5mm,minimum height= 3mm}, 
        rectnode/.style = {rectangle,draw = green!60,fill=green!5,minimum size = 7mm},
    ]
    % Nodes
    \node[roundnode] (titulo) at (0,0) {Eletricidade};
    \node[rectnode,draw=yellow!70,fill=yellow!10] (carga) at (0, -3) {Cargas Elétricas};
    \node[rectnode,draw=red!70,fill=red!5,below left= of carga] (positivas) {Positivas};
    \node[rectnode,draw=blue!70,fill=blue!10,below right= of carga] (negativas) {Negativas};
    
    % Lines
    \begin{scope}[>=stealth,thick]
    \draw[->] (titulo) -- (carga) node [pos=.5,right] {Estuda os fenômenos relacionados a};
    \draw (carga) -- (0,-4)  node [below= 4mm]{Que podem ser} ;
    \draw[->](0,-4) -- (negativas);
    \draw[->](0,-4) -- (positivas);
    \end{scope}
    \end{tikzpicture}
\end{document}

我希望创建一个宏来替换其中的颜色draw=[colorname]!70,fill=[colorname]!10名称,颜色名称可以是 xcolor 包中的任何颜色,或者至少是 LaTeX 原生颜色。此宏应将颜色名称作为参数(参数,idk)。这样,我可以通过调用类似以下内容的方法来为节点着色:

\node[rectnode,nodecolor=blue,{other options}] at ({coordinate}) {Anything}

你们能帮助我吗?

答案1

你可以将参数传递给样式,也可以设置默认值。因此你可以这样做

 rectnode/.style = {rectangle,draw = #1!60,fill=#1!5,minimum size = 7mm},
 rectnode/.default = green

这里#1代表传递给样式的参数。要在节点中使用默认绿色,只需执行

\node[rectnode] ...

但如果你想要不同的颜色,例如

\node[rectnode=blue] ...

完整代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{geometry}
\usetikzlibrary{arrows,patterns,shapes,positioning}
\geometry{margin = .5in}


\begin{document}
    \centering
    \begin{tikzpicture}[
        roundnode/.style = {circle,very thick,draw=blue!70,fill=blue!5,minimum size=7mm},
        cloudnode/.style = {cloud,cloud puffs=10, cloud puff arc=60,draw=cyan!70,fill=cyan!5,minimum width = 5mm,minimum height= 3mm}, 
        rectnode/.style = {rectangle,draw = #1!60,fill=#1!5,minimum size = 7mm},
        rectnode/.default = green
    ]
    % Nodes
    \node[roundnode] (titulo) at (0,0) {Eletricidade};
    \node[rectnode=yellow] (carga) at (0, -3) {Cargas Elétricas};
    \node[rectnode=red,below left= of carga] (positivas) {Positivas};
    \node[rectnode=blue,below right= of carga] (negativas) {Negativas};
    
    % Lines
    \begin{scope}[>=stealth,thick]
    \draw[->] (titulo) -- (carga) node [pos=.5,right] {Estuda os fenômenos relacionados a};
    \draw (carga) -- (0,-4)  node [below= 4mm]{Que podem ser} ;
    \draw[->](0,-4) -- (negativas);
    \draw[->](0,-4) -- (positivas);
    \end{scope}
    \end{tikzpicture}
\end{document}

相关内容