# How to make (multi part) node fill color depend on its content?

In the following MWE i like to change fill and text color depending on content of a cell similarly as I manage to change text color for this particular cell content:

\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}


which gives

In the top image i lake to have first row color in teal!60!black! and in the bottom as it is now. The text color in cells (except in the first) i like to have the same color as is color of the rows.

If I understand well your question, you can define a style ifstrequal with four arguments {str1}{str2}{style if equal}{style if not} and then use it inside your okvir like in the following code :

\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}


Note: inside the ifstrequal style you can use every one node part and every two node part styles to set separate styles for the two parts and avoid your \ifstrequal inside node contents`.

• Thank you very much! however, the same (similar) solution I test before I send my answer an receive error "missing endcsname". Meantime I upgraded my MikTeX, and your solution works. I need some time to see eventual difference between my tests and your solution ... (so far +1 :-) ) Mar 19, 2019 at 17:51
• i found the difference why my attempt didn't work! thank you very much again! Mar 21, 2019 at 23:27