在下面的 MWE 中,我喜歡根據單元格的內容更改填充和文字顏色,就像我設法更改此特定單元格內容的文字顏色一樣:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{chains, positioning, shapes.multipart}
\usepackage{etoolbox}
\tikzset{HDLC/.style = {
start chain = 1 going left,
okvir/.style n args = {3}{rectangle split, rectangle split parts=2,
text depth=0.5ex, inner sep=1.2pt, outer sep=0mm,
font=\footnotesize\sffamily,
align=center, draw=gray!80,
text width=##1,
text=blue,
%%%%%%%%%%
%if ##3 is I than
rectangle split part fill={teal!60!black,white}, text=teal!60!black,
% else
% rectangle split part fill={cyan!60!black,white}, text=cyan!60!black,
node contents={\nodepart{one}\vphantom{/tip}\textcolor{white}{##2}
\nodepart{two}\vphantom{/tip}\ifstrequal{##2}{tip}
{\textcolor{black!60!red}{\textbf{##3}}}
{##3}
},% end of node contents
},
PP/.style n args = {3}{okvir={##1}{##2}{##3},
on chain=1},
}}
\begin{document}
\begin{tikzpicture}[HDLC, node distance=2mm and 0mm]
\node (a) [PP={7mm}{N\textsubscript{R}}{0}]% should use teal!60!black
node[PP={7mm}{P/F}{P}]
node[PP={7mm}{N\textsubscript{S}}{4}]
node[PP={5mm}{tip}{I}];
\node(b) [PP={7mm}{N\textsubscript{R}}{0}, below=of a]% should use cyan!60!black
node[PP={7mm}{P/F}{P}]
node[PP={7mm}{N\textsubscript{S}}{4}]
node[PP={5mm}{tip}{S}];
\end{tikzpicture}
\end{document}
這使
在頂部圖像中,我將第一行顏色設定為“青色!60!黑色!”和現在一樣在底部。單元格中的文字顏色(第一個單元格除外)我喜歡與行的顏色相同的顏色。
答案1
如果我很好地理解你的問題,你可以定義一個ifstrequal
帶有四個參數的樣式{str1}{str2}{style if equal}{style if not}
,然後在你的程式碼中使用它,okvir
就像下面的程式碼一樣:
\documentclass[tikz, margin=7pt]{standalone}
\usetikzlibrary{chains, positioning, shapes.multipart}
\usepackage{etoolbox}
\tikzset{
% ---------
ifstrequal/.code n args={4}{
\ifstrequal{#1}{#2}{\pgfkeysalso{#3}}{\pgfkeysalso{#4}}
},
% ---------
HDLC/.style = {
start chain = 1 going left,
okvir/.style n args = {3}{rectangle split, rectangle split parts=2,
text depth=0.5ex, inner sep=1.2pt, outer sep=0mm,
font=\footnotesize\sffamily,
align=center, draw=gray!80,
text width=##1,
text=blue,
% ---------
ifstrequal={##3}{I}{
rectangle split part fill={teal!60!black,white}, text=teal!60!black
}{
rectangle split part fill={cyan!60!black,white}, text=cyan!60!black
},
% ---------
node contents={
\nodepart{one}\vphantom{/tip}\textcolor{white}{##2}
\nodepart{two}\vphantom{/tip}\ifstrequal{##2}{tip}
{\textcolor{black!60!red}{\textbf{##3}}}
{##3}
},% end of node contents
},
PP/.style n args = {3}{okvir={##1}{##2}{##3},
on chain=1},
}}
\begin{document}
\begin{tikzpicture}[HDLC, node distance=2mm and 0mm]
\node (a) [PP={7mm}{N\textsubscript{R}}{0}]% should use teal!60!black
node[PP={7mm}{P/F}{P}]
node[PP={7mm}{N\textsubscript{S}}{4}]
node[PP={5mm}{tip}{I}];
\node(b) [PP={7mm}{N\textsubscript{R}}{0}, below=of a]% should use cyan!60!black
node[PP={7mm}{P/F}{P}]
node[PP={7mm}{N\textsubscript{S}}{4}]
node[PP={5mm}{tip}{S}];
\end{tikzpicture}
\end{document}
筆記:在 style內部,ifstrequal
您可以使用every one node part
和every two node part
styles 為兩個部分設定單獨的樣式,並避免使用\ifstrequal
inside node contents
。