![¿Cómo hacer que el color de relleno del nodo (varias partes) dependa de su contenido?](https://rvso.com/image/392332/%C2%BFC%C3%B3mo%20hacer%20que%20el%20color%20de%20relleno%20del%20nodo%20(varias%20partes)%20dependa%20de%20su%20contenido%3F.png)
En el siguiente MWE, me gusta cambiar el color de relleno y del texto según el contenido de una celda, de manera similar a cómo logro cambiar el color del texto para el contenido de esta celda en particular:
\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}
lo que da
En la imagen superior, quiero que el color de la primera fila sea `verde azulado!60!negro! y en el fondo como está ahora. El color del texto en las celdas (excepto en la primera) me gusta que tenga el mismo color que el color de las filas.
Respuesta1
Si entiendo bien tu pregunta, puedes definir un estilo ifstrequal
con cuatro argumentos {str1}{str2}{style if equal}{style if not}
y luego usarlo dentro de tu okvir
me gusta en el siguiente código:
\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}
Nota:Dentro del ifstrequal
estilo puedes usar every one node part
y every two node part
estilos para establecer estilos separados para las dos partes y evitar el \ifstrequal
interior node contents
.