I am trying to do some automatic drawing in tikz. The point is I want to be able to provide the minimal quantity of information to my macro, and still have it work.
More precisely (but without entering useless details), I want to take an array of coordinates, perform some computations on them, and draw the picture based on the result of these computations.
But whatever route I try to take, I face the same problem : I need to work with arrays of coordinates. Thanks to \foreach
and to the \myarray[index]
syntax, some things are (more or less) easy to do with such arrays. One good example is this question.
However, as soon as you try to do more complicated stuff, difficulties arise. For example, how to sort an array? How to map a function on each element of an array?
It seems it would all be easy to do, if we were able to easily create and/or modify an array. However, with the commands I know, it is only possible to read the array's content.
So I have tried concentrating on the most easy task I can imagine yet not do : appending an element to the end of an array.
Since I couldn't find any pure pgf way of doing it, I have tried some low-level hacks (which you can find below for the most part), but pgf
's syntax giving importance to braces makes it a hell to track the expansion process, let alone fix it.
\documentclass{article}
\usepackage{tikz}
\newcommand{\printarray}[1]{\noindent
\foreach \i in #1 {\i\\}
}
\begin{document}
\def\myarraywithonebrace{1,2,3,4}
\def\myarraywithtwobraces{{1,2,3,4}}
\printarray{\myarraywithonebrace}
\printarray{\myarraywithtwobraces}
% Ok... So according to the manual, it's supposed to work better with two braces,
% but then what am I doing wrong.
% Whatever, let's append an element
\def\mynewarraywithonebrace{\myarraywithonebrace,5}
\def\mynewarraywithtwobraces{\myarraywithtwobraces,5}
\printarray{\mynewarraywithonebrace}
\printarray{\mynewarraywithtwobraces}
% Ok, so it fails in both cases because it takes the array as a whole.
% Let's try appending sequencially each of its elements, then?
\def\mynewarraywithonebrace{\foreach \i in \myarraywithonebrace {\i, }5}
\def\mynewarraywithtwobraces{\foreach \i in \myarraywithtwobraces {\i, }5}
\printarray{\mynewarraywithonebrace}
\printarray{\mynewarraywithtwobraces}
% Now what... in each case I get a string instead of an array.
% Maybe it means it's time to add that extra pair of braces?
\def\mynewarraywithonebrace{{\foreach \i in \myarraywithonebrace {\i, }5}}
\def\mynewarraywithtwobraces{{\foreach \i in \myarraywithtwobraces {\i, }5}}
\printarray{\mynewarraywithonebrace}
\printarray{\mynewarraywithtwobraces}
% ... Nope
\end{document}
Is my quest hopeless?
xparse
– percusse Nov 23 '12 at 20:29