0

I need to dynamically create the ids of the nodes in TiKz with a counter, so that I can position the new nodes in terms of the previous ones. The counter is created as follows and gets increased for each new node:

\documentclass[10pt]{article}
\begin{document}
\thispagestyle{empty}

\newcounter{nojobs}\setcounter{nojobs}{0}

The definition of the command that creates the TiKz node dynamically is the following one (this command can get invoked as many times as required):

\newcommand{\job}[7]{
    \stepcounter{nojobs}

    \node [
        regular polygon,
        regular polygon sides=3,
        fill=headergray,
        anchor=north,
        shift={(+0.0cm,+0.0cm)},
        minimum width=\tipheight,
        minimum height=\tipheight,
        shape border rotate=90
    ]
        (__job_\value{nojobs})
        at
        (__job_\the\numexpr\value{nojobs}-1\relax.south)
    {};

}

The problem is that, whenever the command is invoked, LaTeX says:

! Undefined control sequence.
\job ...par \par \stepcounter {nojobs} \par \node 
  • Is this the correct approach for the problem? Any alternative solutions?
  • Can the counter variable be used in this way?
  • Can the counter variable be decreased and used in the calculation of the previous node ID?
  • 1
    Please make your code snippets compilable ... – Mensch Dec 3 '17 at 2:44
2

Your approach works -- in principle. Here are the first steps to make it work. The only real "improvement" is the use \pgfmathtruncatemacro.

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning,backgrounds,shapes}
\thispagestyle{empty}

\newcounter{nojobs}\setcounter{nojobs}{0}
\newcommand{\job}[7]{
    \stepcounter{nojobs}
    \pgfmathtruncatemacro{\ni}{\value{nojobs}}
    \pgfmathtruncatemacro{\nj}{\value{nojobs}-1}
    \node 
    [
        regular polygon,
        regular polygon sides=3,
%        fill=headergray,
        anchor=north,
        shift={(+0.0cm,+0.0cm)},
%         minimum width=\tipheight,
%         minimum height=\tipheight,
        shape border rotate=90
    ]
        (jobnode\ni)
        at
        (jobnode\nj.south)
    {#1};

}
\begin{document}
\tikz{\node (jobnode0) at (0,0) {};\job1234567\job2345678}
\end{document}

My crystal ball broke, so I had to comment out some of the options which one does not know without a compilable MWE.

EDIT: I cut of the .0 in the floating point numbers returned in an earlier version of my answer, using the comments to this question.

|improve this answer|||||
  • Why truncate \value{nojobs}? It's a counter: it must be an integer. You wouldn't need truncate at all if you saved the value before and after stepping the counter. – cfr Dec 3 '17 at 3:47
  • @cfr Because with my previous version, in which I used \pgfmathsetmacro, the nodes had names ending with 0.0, 1.0 etc. See the discussion following this question. – user121799 Dec 3 '17 at 3:56
  • Yes, but you don't need \pgfmathsetmacro here. PGFMATH is really, really expensive. You're using it completely unnecessarily. – cfr Dec 3 '17 at 3:58
  • 1
    @cfr OK, if you have a better answer, I'll be happy to withdraw mine. (I was under the impression that the original MWE does not work simply because \value{nojob} does not work when labeling tikz nodes.) – user121799 Dec 3 '17 at 4:08
  • This has always been my biggest struggle with LaTeX: not being 100% aware of when one 'control sequence' will work within a given environment or not. – Ricardo Dec 3 '17 at 8:43

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.