I'm trying to remove the dependencies of the packages pgffor and etoolbox from a code I own which uses expl3, but I don't know which is the correct equivalent of \csxdef in expl3. At the moment I have the following code, which works, but I don't know if it's the right way to replace it.
\documentclass{article}
\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{pgffor}
\ExplSyntaxOn
\NewDocumentCommand{\mycmdone}{ m m }
{
\_csxdef_pgfetoolbox:nn { #1 } { #2 }
}
\cs_new_protected_nopar:Npn \_csxdef_pgfetoolbox:nn #1 #2
{
\foreach \x [count=\n] in { #2 } { \csxdef{#1\n}{\x} } % save in \#1<n>
}
\NewDocumentCommand{\mycmdtwo}{ m m }
{
\_csxdef_expl:nn { #1 } { #2 }
}
\cs_new_protected_nopar:Npn \_csxdef_expl:nn #1 #2
{
\clist_set:Nn \l_tmpa_clist {#2}
\int_step_inline:nn { \clist_count:N \l_tmpa_clist }
{
\cs_set:cpx {l_#1##1:} { \clist_item:Nn \l_tmpa_clist { ##1 } }
}
}
\ExplSyntaxOff
\begin{document}
Test mycmdone
\mycmdone{A}{X,Y,Z,W}
\csuse{A1} \csuse{A2} \csuse{A3} \csuse{A4}
% repeat
\mycmdone{A}{X,Y,Z,W}
\csuse{A1} \csuse{A2} \csuse{A3} \csuse{A4}
\par
Test mycmdtwo
\mycmdtwo{B}{P,Q,R}
\ExplSyntaxOn
\use:c{l_B1:} ~ \use:c{l_B2:} ~ \use:c{l_B3:}
\ExplSyntaxOff
% repeat
Test mycmdtwo
\mycmdtwo{B}{P,Q,R}
\ExplSyntaxOn
\use:c{l_B1:} ~ \use:c{l_B2:} ~ \use:c{l_B3:}
\ExplSyntaxOff
\end{document}
Commands take two arguments, where the second is separated by commas and is not used directly in the document, \csuse is passed as an argument to another code I have defined. I don't know if what I should use is \cs_new: or \l_#1#2_tl or if expl3 has a function that already does this (I don't want to reinvent the wheel).
Greetings.
cs_newvs\tl_new, putting\tl_new:cis almost obligatory in my case, otherwise it doesn't happen when usingdebug_on:n {...}(which I think is good practice :). Query ... Is there a difference betweenuse:cand\tl_use:c? – Pablo González L Jun 17 '19 at 03:33\use:c{name}is exactly\csname name\endcsname. If\nameexists it will be used and if it is not TeX will implicitly\let\name\relaxand use it with no error message.\tl_use:cuses\tl_use:N, which checks if thetl varexists before using it and prints and error message if it does not. So again, in the realm of best practices,\tl_use:cis a better option because it will not silently do something you don't want it to. (Sorry for the delay, I posted the answer right before going to bed and just woke up :-) – Phelype Oleinik Jun 17 '19 at 08:56\mycmdtwo(repeat) should I occupy\use:cor should I change\tl_new:cto\tl_clear_new:c?...Saludos – Pablo González L Jun 17 '19 at 11:26\mycmdtwo{B}{P,Q,R}again to overwrite the token lists, then\tl_clear_new:cis the way to go. I'll add to the answer. Only in very specific situations\use:cwill be the correct choice, mainly due to its implicit behaviour. – Phelype Oleinik Jun 17 '19 at 11:33