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.
The \begin{array} command indicates the beginning of an array environment. The curly braces after this command hold the parameters for the array's columns. These parameters are any combination of three letters: l (left-align), c (centre), and r (right-align). An array can have as few as one column.
What Are Arrays in Data Structures? An array is a linear data structure that collects elements of the same data type and stores them in contiguous and adjacent memory locations.
C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With