[texhax] pgf & dim & 1-element array-like structures

Neal H. Walfield neal at walfield.org
Tue Aug 2 14:13:11 CEST 2016


Hi,

I'm trying to get the length a pgf array-like structure in a macro.
I'm using the "dim" function.  I want the caller to pass a list as
follows:

  \foo{"something", "another thing"}

Sometimes, only one string needs to be passed, in this case, the
following should be enough:

  \foo{"one thing"}

Here's a simplified version of some code that I've tried:

  \documentclass{article}
  
  \usepackage{pgf}
  \def\foo#1{
    \def\array{{#1}}
    \pgfmathparse{dim(\array)}
    dim(\array) = \pgfmathresult
  }
  
  \def\bar#1{
    \def\array{{#1, ""}}
    \pgfmathparse{dim(\array)}
    dim(\array) = \pgfmathresult
  }
  
  \begin{document}
  
  \foo{"xyz"}
  
  \foo{{"xyz"}}
  
  \foo{"xyz", "abc"}
  
  \bar{"xyz"}
  
  \bar{"xyz", "abc"}
  
  \end{document}

The calls to \foo output:

  dim("xyz") = 3
  dim("xyz") = 1
  dim("xyz", "abc") = 2

The "bug" with single element arrays is apparently known.  To work
around it, I thought I'd just add an empty element to the array-like
structure, as shown in \bar above.  But, this results in the following
error:

  ! Undefined control sequence.
  \pgfmath at token@stop ->\pgfmath at stop 
                                      
  l.25 \bar{"xyz"}
                  
  ! Undefined control sequence.
  \pgfmath at token@stop ->\pgfmath at stop 
                                      
  l.27 \bar{"xyz", "abc"}

Does anyone have an idea what the problem could be?

Thanks!

:) Neal


More information about the texhax mailing list