I attempted to make a generic variable argument macro based off of what I saw here that can prepends text before each argument, append text after each argument, and adds text after all arguments have gone through.
It seems to work in general. Though, you can't put any kind of text after it because it will mess things up:
% Allows using an @ in a macro name so we can use \@ifnextchar
\makeatletter
% Generic macro for doing things to variable arguments
% #1: What to prepend to the current item
% #2: What to append to the current item
% #3: What to put after all is done
\newcommand{\checknextarg}[3]{\@ifnextchar\bgroup{\@gobblenextarg{#1}{#2}{#3}}{#3}}
% Helper function for \checknextarg that controls the printing/displaying
% #1: What to prepend to the current item
% #2: What to append to the current item
% #3: What to put after all is done
% #4: The current item
\newcommand{\@gobblenextarg}[4]{#1#4#2 \checknextarg{#1}{#2}{#3}}
% Restores the @ symbol back to normal
\makeatother
So, let us say we have some arbitrary macro called func that is displayed with some variable number of arguments. So, if I called \func{a_1}{a_2}{a_3}
, it would display something like
Let's say that I want func to require at least 2 parameters. So, I attempt to add the function like such:
\newcommand{\func}[2]{\text{func}(#1, #2\checknextarg{, }{}{)}}
The first argument to \checknextarg
is ", " since I wish for each parameter to be comma separated. The second is simply nothing because I don't desire to append anything to each parameter. The third is ")" to close off the function.
This works as intended for 2 parameters or greater. However, LaTeX will attempt to read the text after the call to the \func
macro if there are less than 2 commands put in because of the nature of \@ifnextchar
. So, I was wondering if there were any ideas on how to avoid taking in arguments in cases like this.