[Tex/LaTex] Define a new command with parameters inside newcommand

macrosnestingparameters

I have read the question and answers about:

What I want to do is similar, but one step further.
I would like to define a new command with a variable name, but the new command should have parameters (exactly one in my case).

I tried to use the hash symbol # twice, like this:

\newcommand{\deffun}[1]{\expandafter\newcommand[1]\csname #1\endcsname[1]{#1(##1)}}

But this does not seem to work and I am too confused or ignorant to produce a mwe with nesting of macro definitions with parameters.
For an answer, pointing me to the relevant place of the documentation would be enough.
It looks like nesting of macro definitions is not a commonly used nor explained feature.

PD: I was almost right, it is like this:

\newcommand{\deffun}[1]{\expandafter\newcommand\csname #1\endcsname[1]{#1(##1)}}

Best Answer

There are a number of ways to achieve this:

enter image description here

\documentclass{article}

\def\deffunA#1{\expandafter\def\csname #1\endcsname##1{#1(##1)}}

\newcommand{\deffunB}[1]{\expandafter\newcommand\csname #1\endcsname[1]{#1(##1)}}

\usepackage{etoolbox}
\newcommand{\deffunC}[1]{\csdef{#1}##1{#1(##1)}}

\begin{document}

\deffunA{abc}
$\abc{123}$

\deffunB{ghi}
$\ghi{123}$

\deffunC{jkl}
$\jkl{123}$

\end{document}

\deffunA uses a pure TeX way of defining a macro, while \deffunB is a LaTeX-related way of doing it. \deffunC uses etoolbox which provides a neat interface for defining macros. See section 3.1.1 Macro Definitions of the documentation. In fact, \csdef uses the LaTeX-defined \@namedef... yet another way of achieving what you're after.