TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

pgffor's foreach can take an "array" of elements. I need to map those elements using another array(a lookup table) because I need both the elements and there mapped value.

e.g., suppose I an doing some cryptography and have letters mapped to numbers in some non-standard way

\foreach \i in {a,b,c,d,e}
 \i = \lookup{\i}{\data}

where data is, I guess some type of associative array that says stuff like

a = 12, b = 16, c = 3, d = 9, e = 19

which the foreach will then print

a = 12
b = 16
c = 3
d = 9
e = 19

I also can simply another array and I want to step through them simultanously, for of like

\foreach \i,\j in {a,b,c,d,e} , {12,16,3,9,19}
 \i = \j

(I do not want some type of nested loops, that is not what I'm trying to do)

The easiest solution the better. I'm not trying to do anything extremely complex

share|improve this question
Don't forget to indent your code by 4 spaces – Loop Space Aug 22 '12 at 12:01
up vote 3 down vote accepted

In the question, you have some options in mind and things you might not do but the question is not clear at all. I'm guessing something of the following.


% Got some array from Excel via =randbetween(1-26)

\foreach \x[count=\xi from 0] in {a,...,z}{
\x = \pgfmathresult, 

enter image description here

share|improve this answer
This looks like it will work. It's very simple, I want to map the index value into another value. It would be nice if I could simply define what you have done in my last example as it is cleaner but yours achieves the same thing as far as I can tell. – AbstractDissonance Aug 22 '12 at 13:03
@AbstractDissonance I'm sorry but I don't understand your comment. Why don't you state the problem clearly with actual data with a minimal example giving the exact output you wish to obtain so that we don't guess each others intention? – percusse Aug 22 '12 at 13:28
lol, you seem to be the only one confused ;/ – AbstractDissonance Aug 22 '12 at 13:32
@AbstractDissonance That might be true. – percusse Aug 22 '12 at 13:36

Your Answer


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.