7

I have to place several nodes in my TikZ picture, so I create them with a \foreach that also gives them a name (an ID variable is defined in the \foreach). Now my problem is that after the creation I would like to edit the label of a node, simply by using the name I have given to it. Something like

\node (thatParticularNode) {myLabel};

Is it possible or my only chance is to draw each node separately?

6
  • 1
    I guess just using \def\myLabel{thatParticularNode} would run into problems with expansion... But maybe an xdef or something could work? – Seamus Jan 27 '16 at 13:03
  • 1
    If you ones set the node, you can not change it content anymore, you can only overwrite it. Is this what you like to do? Better is to define its content when you generate the. Otherwise is better generate only coordinates for these nodes and than use command you show in question. Anyway, small, complete document, which shows, what you doing, will be of big helps to understand your question better. – Zarko Jan 27 '16 at 13:09
  • @Zarko In some sense, yes I would like to overwrite the label, but referencing the node by its name. You can imagine a situation in which you have to draw 100 circles with a foreach (giving them names from ID1 up to ID100) and then write something inside the 10th circle. Is there a way to do so? – minomic Jan 27 '16 at 14:01
  • In pgfkeys-system there is a /.try handler. It is like "so let's try if someone set the content for this node before." – Symbol 1 Jan 27 '16 at 14:06
  • Yes, there is more possibilities, hover to be more particular, you need to provide a small document (as I already said in previous document), on which basis I can show, how I would solve your problem. We now are still on level of guessing. Anyway, maybe this lead to solution: foreach \i/\j in {<node id or coordinate>/<node content>, .... }. – Zarko Jan 27 '16 at 14:07
3

In pgfkeys system, the handler /.try tries to execute the predefined code. If it fails, i.e. the key is not yet defined, it would act as it does not exists.

On the other hand, if one calls /.retry right after a /.try, pgfkeys would try the former only if the later fails. That is to say, one has a general framework

\pgfkeys{
    Did I set this special key?/ ID:289668/ let us/.try,
    In case it fails, we still have a fallback/.retry
}

And here is an example

\documentclass[border=9,tikz]{standalone}
\begin{document}

\tikzset{
    node content/normal/.code={(\i,\j)},
    node content/3/5/.code= \LARGE\color{red} I like this node
}

\tikz[nodes={inner sep=0},scale=3]{
    \draw foreach\i in{1,...,10}{
        foreach\j in{1,...,10}{
            (\i,\j)node[circle,draw,minimum size=rnd*100]{
                \tikzset{node content/\i/\j/.try}
                \tikzset{node content/normal/.retry}
            }
        }
    };
}

\end{document}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.