[texhax] dynamically generating tikz node names

Paul Isambert zappathustra at free.fr
Fri Aug 17 08:12:31 CEST 2012


"Neal H. Walfield" <neal at walfield.org> a écrit:
> 
> I'm trying to programmatically name a bunch of tikz nodes (actually
> pgfplots).  Here is a minimal example:
> 
>   \documentclass{article}
>   \usepackage{tikz}
>   
>   \begin{document}
>   
>   \def\alltraces{aaa, bbb, ccc, ddd}
>   
>   \def\plotall{
>     \begin{tikzpicture}
>     \gdef\i{0}
>     \node (t\i) at (0, 0) {};
>     \foreach \trace in \alltraces {%
>       \edef\prev{\i}
>       \pgfmathadd{\i}{1}
>       \xdef\i{\pgfmathresult}
>       \node[anchor=north] (t\i) at (t\prev.south) {\i \trace};
>     }
>     \end{tikzpicture}
>   }
>   
>   \plotall
>   
>   \end{document}
> 
> pgfmathresult returns numbers like 2.0 rather than 2 (\show\i returns,
> e.g., \i=macro: ->1.0.), which I think confuses tikz.

You can use a simple macro to remove the decimal part:

    \def\nozero#1.0{#1}

and then replace the line

    \xdef\i{\pgfmathresult}

with

    \xdef\i{\expandafter\nozero\pgfmathresult}

I don't know whether there are better ways to plot PGF nodes, but at
least this solves your problem.

Best,
Paul



More information about the texhax mailing list