I would like to define macros whose names contain numbers, not just letters:


They would be used to print their values as in the following example:



However, LaTeX does not allow multi-character macro names to contain numbers.

Following the answer of @jan-hlavacek at Defining commands/abbreviations that contain numbers I thought about a creating a \newcommand that conditionally defines a variable:

  \ifnum #1 = 1
  \ifnum #1 = 2

But, then there should be some way to access the variable \var nested in \definevar in the outer code. Any ideas?

  • 2
    it is not clear how you want to access the values, but why not \newcommand\varA{..} and \newcommand\varB{..} in the end that is likely to be far the simplest solution. – David Carlisle Dec 17 '17 at 21:44
  • @DavidCarlisle, imagine that I need to define 100 such variables. – Viesturs Dec 17 '17 at 21:46
  • 1
    if your 100 values are derived from the 100 input numbers just define \var to take one argument the number, and return the result. If you just have 100 values that you need to store, use \varAA, \varAB, ..\varZZ or perhas simpler use roman numerals \vari, \varii, ... \varc – David Carlisle Dec 17 '17 at 21:49
  • 2
    I've taken the liberty of editing your posting to use the term "macros" instead of "variables". TeX and LaTeX provide macros, not "variables". Sometimes macros almost behave like they're variables, but then one can easily find examples where that's not the case. – Mico Dec 17 '17 at 22:00
  • 1
    If you post a proper MWE, people might be able to suggest alternative approaches. This smells like an XY problem .... – cfr Dec 17 '17 at 22:35

If you have an array of variables, it's easier to access them with a “command with argument” syntax.

  \expandafter\newcommand\csname var#1var\endcsname{#2}%
\newcommand{\var}[1]{\csname var#1var\endcsname}


Then \var{1} will print “A”.

A not so efficient variant, but with more information about your real needs this could be refined.



 { % #1 = common name, #2 = list of values
    \clist_item:nn { #2 } { ##1 }




\var{1} and \var{3}

\var{2} and \var{4}


enter image description here


(too long for a comment, hence posted as an answer)

Are you aware of the existence of LuaLaTeX? One of the main differences to pdfLaTeX is that LuaLaTeX offer convenient "hooks" into Lua scripting. Lua, as a modern programming language, has no problem setting up and dealing with variables named var1 and var2. A minimal example (which outputs the string "AB"):

\directlua{var1 = "A"
           var2 = "B" 
           tex.sprint(var1..var2) }
  • A limitation of this method is that I would need to create an additional \directlua scope. – Viesturs Dec 17 '17 at 22:15
  • 1
    @Viesturs - I honestly don't see this as a limitation. Might "overhead" be a term you're OK with? Clearly, there's some extra overhead involved in interfacing between the tex and lua sides of the document. Just how much more overhead will be involved will depend mainly on what you intend to do with the variables. If string concatenation features prominently, you'll find Lua to be very natural environment for getting the job done. – Mico Dec 17 '17 at 22:43
  • 1
    Worth knowing, since I use LuaLaTeX exclusively (and do not know Lua programming). But I find it hard to believe that the customer base for LaTeX, which seems to consist mostly of folks writing math dissertations, would object to the inclusion of a little program code! – user139954 Dec 17 '17 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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