1

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

\def\var1{A}
\def\var2{B}

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

\documentclass{article}
\begin{document}
\var1

\var2
\end{document}

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:

\newcommand\definevar[1]{
  \ifnum #1 = 1
  \def\var{A}
  \fi
  \ifnum #1 = 2
  \def\var{B}
  \fi
}

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
2

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

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

\definevar{1}{A}
\definevar{2}{B}

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

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

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

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

\ExplSyntaxOff

\definevars{\var}{A,B,C,x}

\begin{document}

\var{1} and \var{3}

\var{2} and \var{4}

\end{document}

enter image description here

3

(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"):

\documentclass{article}
\begin{document}
\directlua{var1 = "A"
           var2 = "B" 
           tex.sprint(var1..var2) }
\end{document}
  • 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.