Đặt macro cho các nút màu trong tikz
Sep 21 2020
Tôi có mã sau
\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}
và tôi muốn tạo một macro để thay thế tên draw=[colorname]!70,fill=[colorname]!10màu là bất kỳ màu nào từ gói xcolor hoặc ít nhất là các màu gốc của LaTeX. Macro này phải lấy tên màu làm tham số (đối số, idk). Bằng cách đó, tôi có thể tô màu cho nút bằng cách gọi một cái gì đó như:
\node[rectnode,nodecolor=blue,{other options}] at ({coordinate}) {Anything}
Các bạn có thể giúp mình được không?
Trả lời
1 TorbjørnT. Sep 21 2020 at 04:05
Bạn có thể chuyển các đối số cho các kiểu và cũng có thể đặt các giá trị mặc định. Vì vậy, bạn có thể làm
rectnode/.style = {rectangle,draw = #1!60,fill=#1!5,minimum size = 7mm},
rectnode/.default = green
Đây #1là viết tắt của đối số được truyền cho kiểu. Để sử dụng màu xanh lá cây mặc định trong một nút, chỉ cần làm
\node[rectnode] ...
nhưng nếu bạn muốn một màu khác, hãy làm ví dụ
\node[rectnode=blue] ...
thay thế. Hoàn thành mã:
\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}