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]!10colorname이 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}

관련 정보