# Generate list of macros in the output

I have several macros defined in my document.

\newcommand{\pa}[1]{\left(#1\right)} % enclose with parentheses
\newcommand{\bra}[1]{\left[#1\right]} % enclose with square brackets
\newcommand{\pfrac}[2]{\pa{\frac{#1}{#2}}} % enclosed in parentheses
\newcommand{\bfrac}[2]{\bra{\frac{#1}{#2}}} % enclosed in square brackets


Since there are lot many macros, I would like to create a list of the macros and their renderings in the output pdf.

Currently, I use the following macros for generating the list.

\newcommand{\printpa}{\backslash pa\{arg1\}=\pa{arg1}}
\newcommand{\printbra}{\backslash bra\{arg1\}=\bra{arg1}}
\newcommand{\printpfrac}{\backslash pfrac\{arg1\}\{arg2\}=\pfrac{arg1}{arg2}}
\newcommand{\printbfrac}{\backslash bfrac\{arg1\}\{arg2\}=\bfrac{arg1}{arg2}}


Here, I have to implement a print macro for each macro.

Please suggest me an easier way to create such a list. For example, a print macro that takes a macro name and number of arguments would be nice.

\newcommand{\printmacro}[2] = ??? %takes "macro name" and "number of arguments" as arguments.
\printmacro{pa}{1}
\printmacro{bra}{1}
\printmacro{pfrac}{2}
\printmacro{bfrac}{2}


Here's a solution. Ideally just \printmacro{foo} would understand the arguments and use the necessary arguments, but I can't think of a clever way of doing this (just bruteforcing by seeing the \meaning and then substituting #n by {argn}, but if the macro is robust then it doesn't do well, so…). May be someone comes and shows a clever definition.

\documentclass{scrartcl}
\usepackage{mathtools}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand \printmacro { m m } { \dips_printmacro:cn { #1 } { #2 } }
\cs_new_protected:Npn \dips_printmacro:Nn #1 #2
{
\ensuremath
{
\texttt { \token_to_str:N #1 \dips_printmacro_args:n { #2 } }
=
\dips_printmacro_meaning:Nn #1 #2
}
}
\cs_new_protected:Npn \dips_printmacro_args:n #1
{
\int_step_inline:nnnn { 1 } { 1 } { #1 }
{ \c_left_brace_str \textit{arg##1} \c_right_brace_str }
}
\cs_new_protected:Npn \dips_printmacro_meaning:Nn #1 #2
{
\tl_clear:N \l_tmpa_tl
\int_step_inline:nnnn { 1 } { 1 } { #2 }
{ \tl_put_right:Nx \l_tmpa_tl { {\noexpand\textit{arg##1}} } }
\exp_last_unbraced:NV #1 \l_tmpa_tl
}
\cs_generate_variant:Nn \dips_printmacro:Nn { c }
\ExplSyntaxOff

\newcommand{\pa}[1]{\left(#1\right)} % enclose with parentheses
\newcommand{\bra}[1]{\left[#1\right]} % enclose with square brackets
\newcommand{\pfrac}[2]{\pa{\frac{#1}{#2}}} % enclosed in parentheses
\newcommand{\bfrac}[2]{\bra{\frac{#1}{#2}}} % enclosed in square brackets

\begin{document}
\printmacro{pa}{1}\par
\printmacro{bra}{1}\par
\printmacro{pfrac}{2}\par
\printmacro{bfrac}{2}\par
\end{document}


Note that this might only work for simple commands defined by you. If robust commands, or xparse defined commands, etc. are used this might not work as you expect.