0

Is there a command that lets me use the color of a certain node (e.g. the node with the name "Node 1")? Something like \nodecolor{Node 1} or similar? I am trying to use the color of a certain node as a variable.

Bernard
  • 271,350
manuel459
  • 319

2 Answers2

3

Here's at least a proof of concept for the text color for TikZ and LaTeX (since then xcolor is used which allows to \xglobal a \colorlet).

This lets a color named pgf@sh@c@<node name> to the color that is either currently active (.) and would be used for the text as well or the one that is defined via text = <color>.

The \nodecolor macro silently reverts to the current color when the color for the given node name doesn't exist.


Similar things can be done to \tikz@strokecolor and \tikz@fillcolor, though I wonder if the node's text box is really the right place to hook this in but it is the first easy one I found.

There's also more do be done for nodes of the same name but in different pictures.


It would be very easy to also save the color name, e.g. . or blue in this case, but this is not really a good idea since these names can be overwritten or are very dynamic (in the case of .).

Code

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

Output

You're lucky if you can't see these colors.

Qrrbrbirlbel
  • 119,821
  • Big thanks for that! You solved a problem I was thinking and tinkering on for days now. I modified and extended your code a bit and will present the result below. – Moss_the_TeXie May 26 '23 at 15:13
0

As announced above, here is a slightly modified and expanded version of Qrrbrbirlbel's code. This version uses user-readable colour names (without @) that can be used directly again in TikZ options.

I have also extended the principle to stroke, fill and concept colours (of mindmaps), which are all stored in similar variables in the PGF/TikZ innards.

If this question/answer makes it into tikz-ext, feel free to use this code.

% !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}

enter image description here