Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 {}
\NewDocumentCommand{\addcmd}{ m }
   {
      \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}
\addcmd{dot}
\addcmd{vec}
\addcmd{breve}

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

$\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)  
share|improve this question

1 Answer 1

up vote 6 down vote accepted

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
\NewDocumentCommand \addcmd { m }
   { \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}
\addcmd{dot}
\addcmd{vec}
\addcmd{breve}

$\apply{x}$

\addcmd{dot}

$\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
\NewDocumentCommand \addcmd { m }
   { \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}
\addcmd{\dot}
\addcmd{\vec}
\addcmd{\breve}

$\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
\NewDocumentCommand \addcmd { m }
   { \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}
\addcmd{dot}
\addcmd{vec}
\addcmd{breve}

$\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.

share|improve this answer
    
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. –  cgnieder 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.