# Apply command list to argument correctly

I have a clist filled with values I want to use as commands and apply them all successively e.g. \cmda{\cmdb{\cmdc{arg}}} to the argument of the \apply function. I added an example but it doesn't work. Any ideas how to solve that problem with a correct \apply function? I would be very thankful, because I spent a lot of time but still haven't found any working solution.

\documentclass{article}

\usepackage{xparse}
\usepackage{amsmath,amssymb}
\ExplSyntaxOn
\clist_new:N \g_pkg_cmd_clist {}
{
\clist_put_right:Nn \g_pkg_cmd_clist {#1}
}
\NewDocumentCommand{\apply}{ m }
{
\tl_clear:N \l_tmpa_tl
\tl_set:Nn \l_tmpa_tl {#1}
\clist_map_inline:Nn \g_pkg_cmd_clist
{
\tl_put_right:Nn \l_tmpa_tl {\csname ##1\endcsname{\l_tmpa_tl}}
}
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff

\begin{document}

$\apply{x}$ should be $\breve{\vec{\dot{x}}}$
%After appling all entries should be cleared

$\apply{y}$ should be $\vec{\dot{\breve{y}}}$

\end{document}


Maybe to explain the goal: I want to create a package which allows kind of object-oriented symbols definition commands like this:

\var<fcn1,fcn2,...>[subscripts or if known as superscripts][superscripts]
\current[battery,max][2] %=I_{bat,max}
\state<v>[i] %state vector \vec{x}_i (with \state=x)
\state<M> %=\mathbf{\uppercase{x}} (with \state=x)

-

To apply the various commands sequentially to the input, perhaps the easiest way is to build up in a token list, using the appropriate expansion control so that the result is exactly what is required:

\documentclass{article}
\usepackage{xparse}
\usepackage{amsmath,amssymb}
\ExplSyntaxOn
\clist_new:N \g_pkg_cmd_clist
\tl_new:N \l_pkg_tmp_tl
{ \clist_gput_right:Nn \g_pkg_cmd_clist {#1} }
\cs_new:Npn \pkg_cmd:n #1 { }
\NewDocumentCommand \apply { m }
{
\tl_set:Nn \l_pkg_tmp_tl {#1}
\clist_map_inline:Nn \g_pkg_cmd_clist
{
\tl_set:Nx \l_pkg_tmp_tl
{ \exp_not:c {##1} { \exp_not:V \l_pkg_tmp_tl } }
}
\clist_gclear:N \g_pkg_cmd_clist
\tl_use:N \l_pkg_tmp_tl
}
\ExplSyntaxOff

\begin{document}

$\apply{x}$

$\apply{y}$

\end{document}


Here, I have used \exp_not:c to expand the csnames into control sequences with no further expansion. (As noted in a comment, the OP wants the list of applied functions to be cleared after using \apply: I have added this in here.)

I am not clear on why the \addcmd command does not simply take a control sequence as an argument, which would read something like

\documentclass{article}
\usepackage{xparse}
\usepackage{amsmath,amssymb}
\ExplSyntaxOn
\clist_new:N \g_pkg_cmd_clist
\tl_new:N \l_pkg_tmp_tl
{ \clist_gput_right:Nn \g_pkg_cmd_clist {#1} }
\cs_new:Npn \pkg_cmd:n #1 { }
\NewDocumentCommand \apply { m }
{
\tl_set:Nn \l_pkg_tmp_tl {#1}
\clist_map_inline:Nn \g_pkg_cmd_clist
{
\tl_set:Nx \l_pkg_tmp_tl
{ \exp_not:N ##1 { \exp_not:V \l_pkg_tmp_tl } }
}
\tl_use:N \l_pkg_tmp_tl
}
\ExplSyntaxOff

\begin{document}

$\apply{x}$

\end{document}


The question originally seemed to require a simple mapping in the output, for which the following applies:

You are making life complicated for yourself by trying to build up the list of functions to apply, and in particular by having a loop where you add the literal \l_tmpa_tl to \l_tmpa_tl. Instead, try

\documentclass{article}
\usepackage{xparse}
\usepackage{amsmath,amssymb}
\ExplSyntaxOn
\clist_new:N \g_pkg_cmd_clist
{ \clist_gput_right:Nn \g_pkg_cmd_clist {#1} }
\NewDocumentCommand \apply { m }
{ \clist_map_inline:Nn \g_pkg_cmd_clist { \use:c {##1} {#1} } }
\ExplSyntaxOff

\begin{document}

$\apply{x}$

\end{document}


Notice that I have used \clist_gput_right:Nn here as you have declared your list globally. I have also used \use:c rather than \csname ... \endcsname, as the latter is not 'supported' LaTeX3 syntax and is also not needed here.

-
I just noticed that didn't make clear, that I want to apply all functions to the same argument x leading to $\breve{\vec{\dot{x}}}$. Therefore I though using a loop. That make it a bit more complicated. –  greenux Mar 20 '12 at 10:11
@greenux See the edit: I hope this is what you want. Perhaps you might edit the question to make it clearer? –  Joseph Wright Mar 20 '12 at 11:00
I edited the question and add another thing: After applying all commands should be cleared. I added \clist_clear:N \g_pkg_cmd_clist {} to your example at the end of \apply definition, but the commands for x still effect the \apply for y leading to six and not three applied command? –  greenux Mar 20 '12 at 12:13
@greenux just add \clist_gclear:N \g_pkg_cmd_clist at the end of the definition of \apply. –  clemens Mar 20 '12 at 12:27
@greenux I have edited the answer again to include this requirement (which again was not obvious from the question). You may have been caught out by the need to remember that the clist here is globally-assigned: it has therefore to be globally cleared. –  Joseph Wright Mar 20 '12 at 12:27