I was struggling to find a way to pass 1 or more arguments to a latex newcommand, without running into problems. In this case I was trying to build an index where the section title could be placed into one or more categories.
\newcommand allows you to specify a number of arguments, but this number is mandatory, and if you specify a higher number than you actually supply, then it will start to ‘eat’ into the next part of the document:
Results in:
If you supply less arguments than the number specified, you will receive an error.
My solution was to use one of the internal variables to generate a for loop, iterating over a comma-separated list of categories.
This results in and index of:
The tricky bit is that
is a command in its own right. We need to use the
and
to prevent the code being read as
followed by
.