Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I've been using LaTeX for a while, but only recently I made myself a mission to create a class. I needed to learn the details of TeX, and I couldn't find answers to some of my questions. One of them is:

How can I implement a dynamically allocated array in TeX? One of the environments I defined takes a single parameter such as {1234}. I need to split this into characters, which can be done this way. Then I need to list the numbers such that array=[1,2,3,4].

Then I want to loop the elements of this array and place a single-row tabular environment with i columns for each i in the array.

To make things clearer, here's is my purpose. One will define a choices environment such that:

\begin{choices}{32}
\choice ch1
\choice ch2
\choice ch3
\choice ch4
\choice ch5
\end{choices}

is going to create multiple choices in two rows, 3 for the first and 2 for the second (3+2=5):

A) ch1  B) ch2  C) ch3
    D) ch4  E) ch5

This notation is going to help create more flexible choices, with a maximum number of 9 choices per line.

share|improve this question

1 Answer

up vote 7 down vote accepted

For small-scale arrays, the usual solution is to use a \csname-based approach

\expandafter\def\csname my@array@1\endcsname{content-for-key-1}

which can then be read back just using

\csname my@array@1\endcsname

Depending on the use case, you may of course want to have the index as a LaTeX counter or TeX count

\csname my@array@\the\my@array@index\endcsname

The problem with this is that TeX's hash algorithm is not great, so with large arrays performance is poor. This can be solved by 'packing' the array into a single macro, as is done for example by the prop module of expl3.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.