我希望 tikz 中的特定節點類型具有特定的文字顏色。目前,我已經為文字顏色定義了一個巨集。但我需要在每個節點繼續使用該巨集。
難道沒有更好的解決方法嗎?那麼,就像定義節點的背景顏色一樣,我們也可以在\tikzstyle
語句中定義文字顏色嗎?
這是 MWE:
\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\newcommand{\whttxt}[1]{\textbf{\textcolor{LemonChiffon}{#1}}}
\begin{document}
\pagestyle{empty}
% Define block styles
\tikzstyle{block} = [rectangle, draw, fill=black, text width=5em, text centered, rounded corners, minimum height=4em]
\begin{tikzpicture}[node distance=1in]
\node [block] (nodea) {\whttxt{Node A}};
\node [block, below of=nodea] (nodeb) {\whttxt{Node B}};
\end{tikzpicture}
\end{document}
答案1
您可以使用text=LemonChiffon
選項來指定文字顏色以及font=
要套用的選項\bfseries
。下面,我\whttxt
用whttxt
應用這兩個選項的選項取代了巨集:
筆記:
- 你應該使用
\tikzset
而\tikzstyle
不是按照應該使用 \tikzset 或 \tikzstyle 來定義 TikZ 樣式嗎?。
代碼:
\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
%\newcommand{\whttxt}[1]{\textbf{\textcolor{LemonChiffon}{#1}}}
\begin{document}
\pagestyle{empty}
% Define block styles
%\tikzstyle{block} = [rectangle, draw, fill=black, text width=5em, text centered, rounded corners, minimum height=4em]
\tikzset{block/.style={rectangle, draw, fill=black, text width=5em, text centered, rounded corners, minimum height=4em}}
\tikzset{whttxt/.style={text=LemonChiffon, font=\bfseries}}
\begin{tikzpicture}[node distance=1in]
\node [block, whttxt] (nodea) {Node A};
\node [block, below of=nodea, whttxt] (nodeb) {Node B};
\end{tikzpicture}
\end{document}