How can I index into an array of "strings" (text to be typeset) in Latex? If I were to write something in Python, it might look like this:
dow = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"]
def getdow(daynum):
return dow[daynum]
But the best I can do in Latex is the following. The syntax gets rather unwieldy if there are more than about half a dozen entries. Is there a cleaner way? Does Latex have any sort of indexable arrays? A package is OK, but if going that route I'd prefer a lighter package.
(Note that I'm using days of the week just as an example; I'm aware that there are date packages.)
\documentclass{article}
\newcommand\getdow[1]{%
\ifnum #1=0Sunday\else
\ifnum #1=1Monday\else
\ifnum #1=2Tuesday\else
\ifnum #1=3Wednesday\else
\ifnum #1=4Thursday\else
\ifnum #1=5Friday\else
Saturday%
\fi\fi\fi\fi\fi\fi}
\begin{document}
Today is \getdow{5}.
\end{document}
Addendum: I benchmarked the solutions below. I used lualatex to compile the test documents, performed 100,000 accesses with paragraph breaks every 100, and chose the best of three trials:
- Expl syntax: 3.20s (1.42s with pdflatex)
- listofitems: 3.25s (1.53s with pdflatex)
- Lua: 2.10s
So the Lua solution is noticably faster, assuming lualatex. The other two solutions are faster still with pdflatex.




forloop for all three test documents when you ran them under LuaLaTeX?