I'm trying to generate a lot of name badges. I'm wondering, is it possible to store the names as strings in one array, according to the PGF manual:


I tried this:


Without success, because it seems to store the whole string in \names variable, and if I write


The output will be




is what I want to write out.

So why is this not working? How could I use arrays? Does it require some special library?

  • Is this for using in a foreach loop?
    – percusse
    Commented Feb 19, 2012 at 17:11
  • 4
    Here is an example that does not require quotes: \begin{tikzpicture} \def\mylist{Laura,Katie,Frank,Joe} \foreach[count=\xi] \x in \mylist \node[minimum width=1cm] (n\xi) at (\xi,\xi) {\x}; \end{tikzpicture}. The quotes are needed when you plugin your list into a macro and to protect the text from a math parser!
    – percusse
    Commented Feb 19, 2012 at 17:19
  • No, i use it not in a foreach loop. Without foreach loop somehow it doesn't work for me, it gives back the whole string. Thanks!
    – deeenes
    Commented Feb 19, 2012 at 21:04

4 Answers 4


This answer may be more generic than specifically relating to TikZ/PGF.

(La)TeX is a macro-based language, so it does not work as expected compared to other languages when dealing with "arrays". For example, while \names[2] should yield Laura where

\def\names{Katie, Frank, Laura, Joe}

(indexing from 0), (La)TeX considers [2] to have no connection to \names. As such, you're more likely to obtain the output Katie, Frank, Laura, Joe[2] - a concatenation of \names (as it is defined) and [2].

In order to allow for indexing like one typically would using arrays, you would need some other functionality. Here's an example of a list parser that works like you would expect arrays do:

enter image description here

\usepackage{xparse}% http://ctan.org/pkg/xparse
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
    {\namesarray}% \names
    {% \names[<index>]
\newcommand{\namesarray}{Katie, Frank, Laura, Joe}%
\verb|\names:|\ \names \par
\verb|\names[2]:|\ \names[2] \par
\verb|\names[0]:|\ \names[0] \par
\verb|\names[5]:|\ \names[5]

The idea here is to store the names in an array \namesarray and then define a macro (or "function") that takes an optional argument. If no argument is supplied (i.e., you just use \names), then you print the entire \namesarray. If an argument is supplied (of the form \names[<index>]), parse the list sequentially to find that item that matches <index> and print it.

The list parser relies on etoolbox's \docsvlist and enumerator \do.

  • 1
    Wow, you implemented it very professionally! It works fine. I'm just adding one comment: i can get the values of the array with a counter, like this: \newcounter{i} \setcounter{i}{0} \names[\value{i}] \stepcounter{i} ...So it's perfect!
    – deeenes
    Commented Feb 19, 2012 at 21:08
  • Is it possible to get the size of the array? (I would like to use it in a \forloop)
    – Zach
    Commented Sep 8, 2016 at 18:28
  • 1
    @Zach: You can use \sbox9=\hbox{\names} (processes \names but doesn't print it) which then allows you to use the listtotal counter that holds the number of items in the list.
    – Werner
    Commented Sep 8, 2016 at 18:46

To answer the specific question you are asking, here is a complete minimal example.


So, you need to use the pgfmath parser to do the job through \pgfmathparse and then use the result of the parsing with \pgfmathresult: in other words, you cannot call \names[2] directly.

Here is a more complete example with a \foreach loop:

\foreach \i in {0,...,3} {%
  Name \i: \pgfmathparse{\names[\i]}\pgfmathresult, }
  • So now it's clear for me, what i need to make it work like in the PGF manual. Thanks!
    – deeenes
    Commented Feb 19, 2012 at 21:19
  • 1
    Your second snippet doesn't work for me. Gives an error: Undefined control sequence. \pgfkeys. After replacing \usepackage{pgfmath,pgffor} to \usepackage{tikz} it worked.
    – user4035
    Commented Apr 23, 2013 at 22:54
  • Where is this syntax documented? I searched the documentation and found nothing...
    – Dror
    Commented Mar 31, 2014 at 13:22

The arrayjob package has some more macros for using the data array.



\verb|\names(3):|\ \names(3) \par
\verb|\names(1):|\ \names(1) \par
\verb|\names(5):|\ \names(5)

enter image description here

The arrays are 1 based (index starts at 1)


You don't need arrays (but arrays are described in the pgf manual). Also you don't need numbers to "index" the arrays.

   \pgfkeys{/English to German/Peter/.code=Peter,
            /English to German/Mary/.code=Maria,
            /German to English/Peter/.code=Peter,
            /German to English/Maria/.code=Mary}

\pgfkeys{/English to German/Peter}, % -> Peter
\pgfkeys{/English to German/Mary}, % -> Maria
\pgfkeys{/German to English/Peter}, and % -> Peter, and
\pgfkeys{/German to English/Maria}. % -> Mary.
  • It's easy, and works good. If i understand good, with this solution, i can define only one list?
    – deeenes
    Commented Feb 19, 2012 at 21:13
  • No, you can have several lists. I've changed my example to show this.
    – user10274
    Commented Feb 20, 2012 at 1:07
  • @user10247: please insert \begin{document} into your code.
    – Black Mild
    Commented Sep 5, 2018 at 8:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .