New command like an arbitrary number of nested applications of some command
I am not sure if this is what you want:
\documentclass{scrartcl}
\long\def\grabfirst#1#2{#1} \long\def\grabsecond#1#2{#2}
\def\newnestedcommand#1[#2]#3#4{\edef#1{\donest{#2}{#3}{#4}}}
\def\donest#1#2#3{\ifnum\numexpr#1\relax=1 \expandafter\grabfirst\else\expandafter\grabsecond\fi
{\noexpand#2{\unexpanded{#3}}}{\noexpand#2{\donest{#1-1}{#2}{#3}}}}
\begin{document}
\newnestedcommand\foo[3]\nestthis{three times}
\texttt{\meaning\foo}
\end{document}
This lets you define a macro that will contain the nested commands. If you want to do this on the fly, we can do it. Apart from being more flexible, this is more robust.
\long\def\grabfirst#1#2{#1} \long\def\grabsecond#1#2{#2}
\def\commandnested[#1]#2#3{\docommandnest{#1-1}{#2}{#2{#3}}}
\def\docommandnest#1#2#3{\ifnum\numexpr#1\relax=0 \expandafter\grabfirst\else\expandafter\grabsecond\fi
{#3}{\docommandnest{#1-1}{#2}{#2{#3}}}}
\def\addparens#1{(#1)}
\commandnested[6]\addparens{this is inside 6 levels of parentheses}
Here is an implementation with expl3
: the key is in defining token lists that expand to left and right braces (in a full expansion context):
\documentclass{article}
\usepackage{xparse}
% define the command to be nested
\newommand{\SomeCommand}[1]{?#1?}
\ExplSyntaxOn
\NewDocumentCommand{\CommandNested}{O{1}m}
{
\use:x
{
\prg_replicate:nn { #1 }
{ \exp_not:N \SomeCommand \c_argento_left_brace_tl }
\exp_not:n { #2 }
\prg_replicate:nn { #1 }
{ \c_argento_right_brace_tl }
}
}
% when expanded, these give a left brace or a right brace
\tl_const:Nn \c_argento_left_brace_tl { \if_true: { \else: } \fi: }
\tl_const:Nn \c_argento_right_brace_tl { \if_false: { \else: } \fi: }
\ExplSyntaxOff
\begin{document}
\CommandNested{x}
\CommandNested[3]{x}
\end{document}
(The question about how to achieve an arbitrary amount of nested applications of some command is of more general kind.
It is not clear to me what purpose in practise could be pursued by nesting the same command/the same set of control sequence tokens.
Of course having commands/control sequence tokens nested automatically is feasible in (La)TeX. But I don't know whether my following answer will suit those specific needs that brought into being that more general question.
Perhaps you which to reveal further details about the specific objective you wish to achieve by means of information related to your more general question?)
Seems your syntax-specification requires revision:
Seems the syntax \CommandNested[n]{...}
denotes the processing of two arguments (1. The amount of nesting levels / 2. The content of the innermost nesting-level) while it seems that the result \SomeCommand{\SomeCommand{\SomeCommand{\SomeCommand{\SomeCommand{...}}}}}
comes into being due to processing three arguments (1. The amount of nesting levels / 2. The control-sequence-token that is to be nested / 3. The content of the innermost nesting-level).
Below is an approach for LaTeX2e (no e-TeX- or pdfTeX-extensions needed) with syntax
\CommandNested{<amount of nesting levels>}%
{<control sequence token that is to be nested>}%
{<content of innermost nesting level>}
Unlike required in your specification, the argument denoting the
<amount of nesting levels>
is not an optional one but a mandatory one.
Due to \romannumeral
-expansion, the result will be obtained after two
expansion-steps/after two chains of \expandafter
. (This is shown in Test 2.)
Sincerely
Ulrich
\documentclass{article}
\makeatletter
\newcommand\CommandNested[3]{%
\romannumeral0\expandafter\InnerCommandNested
\romannumeral\number\number#1 000A\relax{#3}{#2}%
}%
\@ifdefinable\InnerCommandNested{%
\long\def\InnerCommandNested#1#2\relax#3#4{%
\ifx A#1\expandafter\@secondoftwo\else\expandafter\@firstoftwo\fi
{\InnerCommandNested#2\relax{#4{#3}}{#4}}{ #3}%
}%
}%
\makeatother
\newcommand\SomeCommand[1]{%
\string\SomeCommand\string{#1\string}%
}%
\begin{document}
\ttfamily\selectfont
\parskip=\bigskipamount
Test 1:
\CommandNested{5}{\SomeCommand}{...}
Test 2:
\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\temp
\expandafter\expandafter\expandafter{%
\CommandNested{5}{\UndefinedOtherCommand}{...}}
\meaning\temp
\end{document}