Are there arrays in LaTeX? I don't mean the way to typeset arrays. I mean arrays as the data structure in LaTeX/TeX as a "programming language". I need to store a number of vbox-es or hbox-es in an array. It may be something like "an array of macros".
More details: I have an environment that should typeset songs. I need to store some songs' paragraphs given as arguments to my macro \songparagraph (so I will not typeset them, just store those paragraphs). As I don't know how many paragraphs can be in one particular song I need an array for this. When the environment is closed, all the paragraphs will be typeset - but they will be first measured and the best placement for each paragraph will be computed (for example, some paragraphs can be put one aside the other in two columns to make the song look more compact and save some space).
Any ideas would be welcome. Please, if you know about arrays in LaTeX, post a link to some basic documentation, tutorial or just state basic commands.
This is an array how it could be implemented in LaTeX:
\documentclass{article}
\begin{document}
\newcounter{mycounter}
\setcounter{mycounter}{1}
% ary is any prefix you want, it should not exist as a command.
\expandafter\newcommand\csname ary\the\value{mycounter} \endcsname{myfirstelement}
\stepcounter{mycounter}
\expandafter\newcommand\csname ary\the\value{mycounter} \endcsname{mysecondelement}
\csname ary1 \endcsname
or
\newcounter{index}
\setcounter{index}{2}
\csname ary\the\value{index} \endcsname
\end{document}
Run this through LaTeX (latex mydoc.tex or pdflatex mydoc.tex) and you see the output.
A short explanation: this creates two commands (with newcommand): ary1 and ary2. The \expandafter
is needed because newcommand
should not define \csname
but the command created by \csname
... \endcsname
. \expandafter
jumps over the next token, in this case the control sequence \newcommand
and executes the next command before TeX sees the \newcommand
. That means, the first thing in the newcommand-lines TeX sees is the \csname
...\endcsname
construct, TeX executes it and then executes \newcommand
with the result of the \csname
...\endcsname
construct. \csname foo\endcsname
is the same as \foo
, but you can use any character or even spaces in the command created by \csname
...\endcsname
.
This is not trivial. See the great book "TeX by topic" from Victor Eijkhout: http://eijkhout.net/texbytopic/texbytopic.html