2

considering the generic table \mytable, to get an element from there I am using \pgfplotstablegetelem{\row}{[index]\column}\of\mytable.

but if I don't have a table but an array?

the case is this: \def\array{alba, prova, 13, CO\ped{2}}

I would like to create \singleElement that takes the third element of the array.

How can I do?

2
  • 1
    Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Commented Sep 6, 2015 at 10:33
  • For a one dimensional array (presumably one element per line) one could do the whole thing using plain tex commands line \newread, \openin, \read, \closein, and \csname. Commented Sep 6, 2015 at 16:02

1 Answer 1

0

One way mey be to define it this way:

\def\singleElement#1,#2,#3,#4\close{#3}

And then invoke it using:

\expandafter\singleElement\array,,,\close

This way \singleElement has four arguments, separated by commas, where #4 is ended by \close (well you can choose any \-sequence you prefer for that, just replace \close with, say, \foo everywhere in the above). The way I called it is: your \array contains a list of comma-separated elements, so I make it expand (\expandafter makes TeX read \array before \singleElement, so \array is replaced with the comma-separated list before \singleElement tries to do its job), then \singleElement looks for its arguments, and the commas make sure it finds them, for if \array doesn't have enough elements you would get an error as the commas for \singleElement's arguments would not be found, whereas this way if \array has less than 4 elements (i.e. less than 3 commas) it finds all necessary commas afterwards and the \close to finish argument 4.

Naturally this forces \array to separate its elements by commas, but then you can substitute commas with whatever token you want. If you want dots as separators, you can do:

\def\singleElement#1.#2.#3.#4\close{#3}

and call it by:

\expandafter\singleElement\array...\close

And you can use numbers, punctuation signs, even letter sequences:

\def\singleElement#1abc#2abc#3abc#4\close{#3}
\expandafter\singleElement\array abc abc abc\close

would have abc as the element separator, but that is definitely not advisable :).

Of course, this is a solution that doesn't use pgfplots, so maybe it is not what you expected, but it will work even without loading pgfplots. In fact, it doesn't even require LaTeX: Plain TeX is enough for it.

2
  • 1
    What if the array has more than nine arguments? ;-) Commented Sep 6, 2015 at 11:21
  • Well he asked for the third :). Anyway one can always extract a super-long number 9 and call the macro again on that one. In other words, a little recursion. Commented Sep 6, 2015 at 11:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.