Tikz 节点 - 颜色命令

Tikz 节点 - 颜色命令

是否有命令允许我使用某个节点的颜色(例如名为“节点 1”的节点)?类似\nodecolor{Node 1}或类似的命令?我试图将某个节点的颜色用作变量。

答案1

这至少是 TikZ 和 LaTeX 文本颜色的概念证明(从那时起xcolor就允许使用\xglobal\colorlet

这让颜色命名pgf@sh@c@<node name>为当前活动的颜色(.)并且也可用于文本或通过定义的颜色text = <color>

\nodecolor当给定节点名称的颜色不存在时,宏会默默地恢复到当前颜色。


\tikz@strokecolor和 也可以做类似的事情\tikz@fillcolor,虽然我不知道节点的文本框是否真的是挂接这个的正确位置,但这是我发现的第一个简单的方法。

对于名称相同但图片不同的节点,还可以做更多的事情。


保存颜色名称也非常容易,例如在这种情况下.blue,但这不是一个好主意,因为这些名称可以被覆盖或非常动态(在这种情况下.)。

代码

\documentclass{article}
\usepackage{tikz}
\makeatletter
\newcommand*\nodecolor[1]{\@ifundefinedcolor{pgf@sh@c@#1}{.}{pgf@sh@c@#1}}
\tikzset{
  save text color/.style={
    execute at begin node=% latex only
      \ifx\tikz@textcolor\@empty
        \xglobal\colorlet{pgf@sh@c@\tikz@fig@name}{.}%
      \else
        \xglobal\colorlet{pgf@sh@c@\tikz@fig@name}{\tikz@textcolor}%
      \fi}}
\tikzset{anchor=base,baseline=0pt}
\begin{document}
\tikz[green]\node[ultra thick, draw=blue, fill=red, save text color] (Node 1) {Node 1};
The color of \textcolor{\nodecolor{Node 1}}{Node 1}.

\tikz       \node[ultra thick, draw=blue, fill=red, text=yellow, save text color] (Node 2) {Node 2};
The color of \textcolor{\nodecolor{Node 2}}{Node 2}.

\tikz\shade[left color=\nodecolor{Node 1}, right color=\nodecolor{Node 2}] circle[radius=2ex];
\end{document}

输出

如果您看不到这些颜色,那您很幸运。

答案2

如上所述,这是 Qrrbrbirlbel 代码的稍微修改和扩展版本。此版本使用用户可读的颜色名称(不带 @),可在 TikZ 选项中直接再次使用。

我还将该原理扩展到描边、填充和概念颜色(思维导图),它们都存储在 PGF/TikZ 内部的类似变量中。

如果这个问题/答案进入 tikz-ext,请随意使用此代码。

% !TEX TS-program = pdflatex
\documentclass[tikz]{standalone}
\usetikzlibrary{mindmap}

\colorlet{root-color}{blue}
\colorlet{root-textcolor}{red}
\colorlet{col1}{red}
\colorlet{col2}{green}

% 
%%% Saving node colors
%%% see <https://tex.stackexchange.com/questions/660876/tikz-nodes-command-for-its-color/662011#662011> 
% 
\makeatletter
\newcommand*\nodetextcolor[1]{\@ifundefinedcolor{text color of #1}{.}{text color of #1}}
\newcommand*\nodefillcolor[1]{\@ifundefinedcolor{fill color of #1}{.}{fill color of #1}}
\newcommand*\nodedrawcolor[1]{\@ifundefinedcolor{draw color of #1}{.}{draw color of #1}}
\newcommand*\nodeconceptcolor[1]{\@ifundefinedcolor{concept color of #1}{.}{concept color of #1}}
\tikzset{
  save node text color/.style={
    execute at begin node=% latex only
      \ifx\tikz@textcolor\@empty
        \xglobal\colorlet{text color of \tikz@fig@name}{.}%
      \else
        \xglobal\colorlet{text color of \tikz@fig@name}{\tikz@textcolor}%
      \fi
    },
  save node draw color/.style={
    execute at begin node=% latex only
      \ifx\tikz@strokecolor\@empty
        \xglobal\colorlet{draw color of \tikz@fig@name}{.}%
      \else
        \xglobal\colorlet{draw color of \tikz@fig@name}{\tikz@strokecolor}%
      \fi
    },
  save node fill color/.style={
    execute at begin node=% latex only
      \ifx\tikz@fillcolor\@empty
        \xglobal\colorlet{fill color of \tikz@fig@name}{.}%
      \else
        \xglobal\colorlet{fill color of \tikz@fig@name}{\tikz@fillcolor}%
      \fi
    },
  save node concept color/.style={
    execute at begin node=% latex only
      \ifx\tikz@concept@color\@empty
        \xglobal\colorlet{concept color of \tikz@fig@name}{.}%
      \else
        \xglobal\colorlet{concept color of \tikz@fig@name}{\tikz@concept@color}%
      \fi
    },
  save node colors/.style = {
    save node concept color,
    save node fill color,
    save node draw color,
    save node text color
  }
}
\makeatother

\DeclareDocumentCommand\info{m}{
    \node [
      annotation,
      right,
      draw = draw color of #1,
      text = text color of #1,
      fill = concept color of #1!10,
    ] 
    (#1-info) at (#1.east)
    {#1};
}



\begin{document}
  \begin{tikzpicture}[ 
    save node colors,
  ]
  %%% The Mindmap itsself
    \path[
      mindmap,
      concept color                 = root-color,
      every node/.style             = {
        concept,
      },
      root/.style                   = {
        text = root-textcolor,
      },
      level 1 concept/.append style = {
        sibling angle = 90,
      },
      level 2 concept/.append style = {
        sibling angle = 90,
      },
    ]
    node[root] (root) {Root}
    [clockwise from = 45]
    child[concept color = col1] {
      node (L1) {L1}
      child [concept color = violet] { node (L1-1) {L1-1} }
      child [concept color = orange] { node (L1-2) {L1-2} }
    }
    child[concept color = col2] {
      node (L2) {L2}
      child [concept color = purple] { node (L2-1) {L2-1} }
      child [concept color = teal] { node (L2-2) {L2-2} }
    }
  ;
%% Information nodes %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \info{root}
  \info{L1}
  \info{L1-1}
  \info{L1-2}
  \info{L2}
  \info{L2-1}
  \info{L2-2}
\end{tikzpicture}%
\end{document}

在此处输入图片描述

相关内容