Metacommand for commands that dynamically select a parameter?

enter image description here

You just want to fully expand both arguments using \number to get their decimal expansion, while avoiding expanding everything else (for which a toks register is useful)

\documentclass{article}

\newcommand*{\newproj}[3]{%
  \toks0{\newcommand*#1}%
  \edef\tmp{\the\toks0[\number#2]{####\number#3}}%
\tmp
  }

\def\i{1}
\newproj{\fst}{2}{\i}
\newproj{\snd}{2}{2}

\begin{document}
\begin{tabular}{ll}
\verb^\fst{x}{y}^:& \fst{x}{y} \\
\verb^\snd{x}{y}^:& \snd{x}{y}
\end{tabular}
\end{document}

Here's a start at something

\documentclass{article}
\newcommand{\newproj}[3]{%
    \expandafter\newcommand\csname #1\expandafter\endcsname[#2]{###3}
}
\pagestyle{empty}
\begin{document}

Hello

\newproj{helloworldapplesauce}{4}{3}

\helloworldapplesauce{a}{b}{c}{d}
\end{document}

Alternatively you might try to do (following the advice of @HendrikVogt )

\newcommand{\newproj}[3]{\newcommand{#1}[#2]{###3}}

should also do the trick

Regardless of the approach, you'll probably want to make sure that the values passed in the parameters are numbers and that whatever is passed in the third argument defining the function is no larger than the second.


\documentclass[12pt]{article}
\usepackage{catoptions}
\makeatletter
% \generateparams is not any costlier than using \newcommand:
\new@def*\generateparams#1#2{%
  \ifnum#1<\numexpr#2+1####\number#1%
    \expandafter\generateparams
    \expandafter{\number\numexpr#1+1\expandafter}%
    \expandafter{\number#2\expandafter}%
  \fi
}
\robust@def*\newproj#1#2#3{%
  \@ifdefinable#1\relax
  \cptexpanded{\def\noexpand#1\generateparams1{#2}{#####3}}%
}

% Examples of \newproj:
\def\one{1}
\newproj{\fst}{2}{\one}
\newproj{\snd}{2}{2}

% Using a loop to avoid repeating \newproj for every new definition:
\robust@def*\NewProj#1{%
  \cptforeach \x/\y/\z \in#1\do{%
    \cptexpandsecond\newproj{\noexpandcsn{\x}{\y}{\z}}%
  }%
}

% Examples of \NewProj:
\NewProj{fstb/2/\one, sndb/2/2}

\makeatother

% Let us print the examples:
\begin{document}
\begin{tabular}{ll}
\verb^\fst{x}{y}^:& \fst{x}{y} \\
\verb^\snd{x}{y}^:& \snd{x}{y} \\
\verb^\fstb{x}{y}^:& \fstb{x}{y} \\
\verb^\sndb{x}{y}^:& \sndb{x}{y}
\end{tabular}
\end{document}

enter image description here