1

I need to pass an array of Strings to a command as a keyed value (or named value). my code is:

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\usepackage{keyval}
\usepackage{tikz-uml}
\usepackage{pgfmath}

\makeatletter
% ========= KEY DEFINITIONS =========
\define@key{myCommandKeys}{array}{\def\mm@array{#1}}
\DeclareDocumentCommand{\myCommand}{m}
{%
  \begingroup%
      % ========= KEY DEFAULTS + new ones =========
      \setkeys{myCommandKeys}{array={{nothing,to,see,here}},#1}%

      \def\array{\mm@array}

      \pgfmathparse{dim(\array)}
      \edef\arraylength{\pgfmathresult}

      \pgfmathparse{int(\arraylength-1)}
      \edef\lastIndex{\pgfmathresult}

      \node at (0, 5) {array passed: \array};
      \node at (0, 4) {number of items: \arraylength};
      \node at (0, 3) {last index: \lastIndex};

      \foreach \i in {0,...,\lastIndex} {
            \node at (0,1-\i) {item at index \i: \array[\i]};
      }       
  \endgroup%
}
\makeatother

\begin{document}
\begin{tikzpicture}
    \myCommand{array={why,you,no,working!?}}
\end{tikzpicture}
\end{document}

The output I am getting right now is:

enter image description here

and the error I get is

Package PGF Math Error: Unknown function `why' (in 'dim(why,you,no,working!?)').

Obviously, the array length should be 4, last index 3 and items displayed (one per line of output) should be:

why 
you
no
working!?
  • The problem here is working with strings. Moreover, indexing such an array doesn't work out-of-the-box via <array>[.]. – Werner Oct 20 '17 at 6:15
  • hi @Werner, can you please point out to any resource where i can read up on this? thanks! – gaurav gupta Oct 20 '17 at 6:19
3

You have to surround your array elements with " to tell pgf that those are strings and not function names. The following does work:

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\usepackage{keyval}
\usepackage{tikz-uml}
\usepackage{pgfmath}

\makeatletter
% ========= KEY DEFINITIONS =========
\define@key{myCommandKeys}{array}{\def\mm@array{#1}}
\DeclareDocumentCommand{\myCommand}{m}
{%
  \begingroup%
      % ========= KEY DEFAULTS + new ones =========
      \setkeys{myCommandKeys}{array={{nothing,to,see,here}},#1}%

      \def\array{{\mm@array}}

      \pgfmathparse{dim(\array)}
      \edef\arraylength{\pgfmathresult}

      \pgfmathparse{int(\arraylength-1)}
      \edef\lastIndex{\pgfmathresult}

      \node at (0, 5) {array passed: \array};
      \node at (0, 4) {number of items: \arraylength};
      \node at (0, 3) {last index: \lastIndex};

      \foreach \i in {0,...,\lastIndex} {
        \node at (0,1-\i) {item at index \i:
          \pgfmathparse{\array[\i]}\pgfmathresult};
      }       
  \endgroup%
}
\makeatother

\begin{document}
\begin{tikzpicture}
    \myCommand{array={"why","you","no","working!?"}}
\end{tikzpicture}
\end{document}
  • thanks so much, skillmon! I had quotes earlier and that didn't work either. i have now realized my main mistake - I was using \def\array{\mm@array} while it should be what you have done - \def\array{{\mm@array}}. thanks a tonne!! – gaurav gupta Oct 20 '17 at 7:16
  • when I pass an array with a single item, it doesn't work. example: \myCommand{array={"hello!!!"}} instead it displays each character separately (8 items). any idea why? – gaurav gupta Oct 20 '17 at 20:31
  • 1
    @gauravgupta change the definition \def\array{{\mm@array}} to \def\array{{\mm@array,}}. The trailing comma doesn't change the number of elements, but this way, pgf knows what should be an element (empty elements at the end of an array are ignored). – Skillmon Oct 21 '17 at 9:51
  • when i change the definition to \def\array{{\mm@array,}}, i get the correct output but there is an error (Undefined control sequence) for every time I invoke the command. any workaround for that? – gaurav gupta Oct 21 '17 at 23:12
  • 1
    @gauravgupta sadly I don't have a workaround for that :( You could use expl3 instead of pgf to parse that array, though. – Skillmon Oct 24 '17 at 21:17

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.