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.

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

I'd like to have a macro where I can insert a key as argument and get a value which I have defined before. This should be similar to Create an array of variables but with strings/text instead of numbers.

The usage should look like:


and later


which should return http://xmlns.com/foaf/0.1/. When I try to use the code from the before mentioned question I get ! Illegal parameter number in definition of.

share|improve this question
This is more or less exactly what LaTeX's \@namedef and \@nameuse do. Or etoolbox' \csdef and \csuse. – clemens May 16 '13 at 17:47
What will your definitions contain? URLs only? – Werner May 16 '13 at 18:26
@Werner yes only URIs. I just realized, after checking all these answers, that it didn't work for me because I had a hash # in one URI which resulted in the ! Illegal parameter number in definition of. – white_gecko May 16 '13 at 20:31
@white_gecko: Then you can include the url package and change #2 in David's answer to \url{#2}. – Werner May 16 '13 at 20:44
Thank you David Carlisle, egreg and Steve B. Segletes all your answer work for me :-) My final result is here: gist.github.com/white-gecko/5595112 – white_gecko May 16 '13 at 21:10
up vote 6 down vote accepted
\def\setprefix#1#2{\expandafter\def\csname MY@#1\endcsname{#2}}

\def\getprefix#1{\csname MY@#1\endcsname}

Should be all you need. The MY@ internal prefix is arbitrary and just needs to be distinct from other prefixes (so don't use LT@ unless you want to break longtable). By using different strings there you can have different arrays of macros.

share|improve this answer

You can use \csname...\endcsname:


    {\@latex@warning{No prefix `\detokenize{#1}' defined}UNKNOWN}%







In the first argument of \setprefix you're basically limited in using only balanced braces.

share|improve this answer

Note also that you can recall the prefix with \key, in the example \foaf (only as long as the key is alphabetic).

ROLLBACK to original answer, removing caveat.

share|improve this answer
The keys do not have to be alphabetic but can be anything: \setprefix{?.3$a}{foo}\useprefix{?.3$a} will work just fine – clemens May 16 '13 at 17:53
Why the edit? \expandafter\def\csname ?.3$a\endcsname{foo} and then \csname ?.3$a\endcsname does work without problems! Your initial solution did suffice just fine! (I'd had upvoted but I'm out of votes for today...) – clemens May 16 '13 at 18:17
@cgnieder I did not know that I could put strange characters inside a csname. Now if I only knew how to undo an edit... – Steven B. Segletes May 16 '13 at 18:19
There should be a rollback button if you look at the edit history – clemens May 16 '13 at 18:20

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.