如何使(多部分)节点填充颜色取决于其内容?

如何使(多部分)节点填充颜色取决于其内容?

在以下 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}

在此处输入图片描述

笔记:ifstrequal样式中您可以使用every one node partevery two node part样式为两个部分设置单独的样式,并避免\ifstrequal在里面node contents

相关内容