
我有以下程式碼
\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
colorname 是 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}