1

Is it possible to "change" the color of a node after its declaration ? Here is an example using forest even if my question is about TikZ.

\documentclass{article}

\usepackage{forest}

\newcommand\colorafter[2]{ % ??? }

\begin{document} \begin{forest} [ [$A$, red % I don't want to use this. [$B$, name = nB] [$C$, name = nC] ] [$D$, blue % I don't want to use that. ] ] \colorafter{nB}{red} \colorafter{nD}{blue} \end{forest}

\end{document}

projetmbc
  • 13,315
  • 1
    I think this is not possible without major change of internal of forest. – muzimuzhi Z Aug 05 '20 at 11:16
  • What is the point of this? Why do you need to declare first, then modify afterwards? – AndréC Aug 05 '20 at 11:43
  • I would like to keep the logical definition of the tree and its decorations separated. I use more "advanced" formatting than the one in my example. – projetmbc Aug 05 '20 at 12:34
  • While not what you're asking for, is defining and using styles an option? Certainly if you have a lot of options, if will reduce "clutter" in the tree. (By the way, when replying to a specific comment, you should ping the user with an at-sign, @AndréC wouldn't have been notified of your reply.) – Torbjørn T. Aug 05 '20 at 14:44
  • @AndréC I have answered to your question. – projetmbc Aug 05 '20 at 14:55
  • I agree with @Torbjørn T that the use of style is most appropriate. – AndréC Aug 05 '20 at 15:23
  • @TorbjørnT. Can you explain how you would do with styles ? – projetmbc Aug 05 '20 at 23:16
  • You've probably seen it before, do e.g. \tikzset{foo/.style={draw=, blue, fill=cyan}, bar/.style={draw, dotted, yellow}} in the preamble and use just foo or bar in the tree. – Torbjørn T. Aug 06 '20 at 08:08
  • Yes I know. Thanks. – projetmbc Aug 06 '20 at 08:17

1 Answers1

3

enter image description here

In the example, all nodes are created as blue nodes. The color of A (top left) is changed to red at the end. The bottom node is there only to suggest the path I have chosen.

The answer is for TikZ. The function \aftercolorof}[2]{... creates a new node with color #1 over node #2 with name #2-2 (in case you need it afterward).

It is based on the function given by @Henri Menke in How to access the label text of a node?.

\documentclass[11pt, border=1cm]{standalone}
\usepackage{tikz}

\makeatletter \protected\def\tikz@fig@main#1{% \expandafter\gdef\csname labeltextof@\tikz@fig@name\endcsname{#1}% \iftikz@node@is@pic% \tikz@node@is@picfalse% \tikz@subpicture@handle{#1}% \else% \tikz@@fig@main#1\egroup% \fi} \makeatother

\newcommand\labeltextof[1]{\csname labeltextof@#1\endcsname} \newcommand{\aftercolorof}[2]{% #1 is the color, #2 us the node \path (#2.center) node[#1] (#2-2) {\labeltextof{#2}}; }

\begin{document}

\begin{tikzpicture}[ every node/.style={draw, circle, color=blue, text=blue}, spear/.style={->, very thin, shorten <=2pt, shorten >=2pt,} ] \path (0, 0) node (nA) {$A$}; \path (2, 0) node (nB) {$B$};

\path (1, -2) node[label={ [rectangle, text width=8em]-80:this is the node nC with nA's content }] (nC) {\labeltextof{nA}};

\path (nA) edge [spear, right] (nB) (nA) edge [spear, right] (nC);

% changing the color \aftercolorof{red}{nA} \end{tikzpicture}

\end{document}

Here is an example showing that the solution works with forest.

\documentclass{article}

\usepackage{tikz} \usepackage{forest}

\makeatletter \protected\def\tikz@fig@main#1{% \expandafter\gdef\csname labeltextof@\tikz@fig@name\endcsname{#1}% \iftikz@node@is@pic% \tikz@node@is@picfalse% \tikz@subpicture@handle{#1}% \else% \tikz@@fig@main#1\egroup% \fi} \makeatother

\newcommand\labeltextof[1]{\csname labeltextof@#1\endcsname} \newcommand{\aftercolorof}[2]{% #1 is the color, #2 us the node \path (#2.center) node[#1] (#2-2) {\labeltextof{#2}}; }

\begin{document}

\begin{forest} [ [$A$, name = nA [$B$] [$C$] ] [$D$, name = nD ] ] \aftercolorof{red}{nA} \aftercolorof{blue}{nD} \end{forest}

\end{document}

projetmbc
  • 13,315
Daniel N
  • 5,687