A few of my macros currently require the user to pass the length of an array along with the array itself. It would be nice if the length could be calculated for them, as the user in question (myself) seems incapable of consistently counting beyond 3.

I've checked the pgfmanual's chapters 56 (for loops, where most of this is getting used) and 63 (mathematical functions where arrays are defined). In some sense, this is just counting commas (which I don't know how to do), but PGF has a few exceptions in arrays with () and {} allowing entries to have commas within them.

Can someone write a reasonable pgf math function that takes an array as its only argument and returns the length of that array as its result?

3 Answers 3


In the cvs version of pgf/tikz or in the version available for texlive at tlcontrib there is an experimental undocumented dim function in pgfmath defined as

% dim function: return dimension of an array
% dim({1,2,3}) return 3
% dim({{1,2,3},{4,5,6}}) return 2

      \advance\pgfmath@count by 1\relax


This can however be very slow for large arrays (any suggestions are welcome!).

You can use it this way

  The dimension of $\{1,2,3\}$ is

  The dimension of $\{1,2,\{3,4\},5\}$ is

dim of an array

  • Thanks. I had tried to write this myself and failed. I could never get the result to be returned. It would be nice for the manual to have more examples of functions, since returning the number was very difficult. Commented Nov 28, 2011 at 21:15
  • 1
    There is a hidden danger in this solution. If there is only 1 element in the array, for example number 10 used as in the provided examples. It will not return 1 as an answer, but it will count the elements of that number and return 2 as an answer. Some examples: The dimension of {10} is 2., The dimension of {"10"} is 2. and The dimension of {{10}} is 1.
    – karu
    Commented Aug 1, 2014 at 15:21
  • @karu You're right. Thanks for spotting this. It may be something to fix in the pgfmath parser itself. The internal form of the array {1,2,3} is {{1}{2}{3}} while for the array {1} it is {1} (and IMHO should be {{1}}). But I need some time to dig into it.
    – cjorssen
    Commented Aug 2, 2014 at 14:19

It is also relatively easy to solve using just TikZ:

  \foreach \x in #1{%
  The length of $\{1,2,3\}$ is \arrayLength{{1,2,3}}.\\
  And the length of $\{1,2,\{3,4\},5\}$ \arrayLength{{1,2,{3,4},5}}.

This just uses foreach to iterate over the list and increases a count on every element seen.

  • Is there a specific reason to embed the \foreach loop into a tikzpicture environment?
    – Daniel
    Commented Nov 23, 2011 at 20:47
  • @Daniel: Nope, just had some other stuff there first as well. Removed that but left the tikzpic environment. It can be removed without problems (as long as you add comments to prevent spurious spaces). Commented Nov 23, 2011 at 21:03
  • Then I would suggest to remove it from your answer as well; just to make it more concise and precise.
    – Daniel
    Commented Nov 23, 2011 at 21:10
  • @Daniel: Excellent point. I have done so. Commented Nov 24, 2011 at 7:55

Here is a (community wiki) answer written with great assistance from Bruno Le Floch, Joseph Wright, egreg, and probably a few others on the texsx chat.

\cs_new:Npn \Counter #1 \Stopper { \tl_length:n {#1} } 

It can be used as in:


Which sets \Length to 4.

  • This does not run at mine. Could you add a runable example please?
    – cis
    Commented Oct 28, 2020 at 15:22

You must log in to answer this question.

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