# Problem with \expandafter and csname

Above there is a code that I would like to factorize more. I would to define the two macros \exercice and \topic in a similar way that I do with the special \@init@new@exo but I fail for the moment. I'm missing something with \expandafter and csname...

\documentclass[12pt]{article}
\usepackage{pgfkeys}

\makeatletter
\newcommand\@init@new@exo[1]{
\pgfkeys{
/#1@keys/.is family,
/#1@keys,
pts/.initial  = {}, % Render the subject or not
src/.initial  = {}  % Number of the exam

}

\expandafter\newcommand\csname #1@number\endcsname{Nb for #1}
}

\@init@new@exo{topic}
\@init@new@exo{exercice}

\newcommand\exercice[1][]{%
\begingroup
\pgfkeys{/exercice@keys, #1}
\pgfkeysgetvalue{/exercice@keys/pts}{\exercice@points}
\pgfkeysgetvalue{/exercice@keys/src}{\exercice@source}
exercice \exercice@number{}
(\exercice@points{} pts)
[Source :\exercice@source{}]
\endgroup
}

\newcommand\topic[1][]{%
\begingroup
\pgfkeys{/topic@keys, #1}
\pgfkeysgetvalue{/topic@keys/pts}{\topic@points}
\pgfkeysgetvalue{/topic@keys/src}{\topic@source}
topic \topic@number{}
(\topic@points{} pts)
[Source :\topic@source{}]
\endgroup
}
\makeatother

\begin{document}

\exercice[pts = 1, src = Imaginaire]

\exercice

\topic[pts = 1, src = Imaginaire]

\topic[pts = 2]

\end{document}

• It is not clear to me what you're after... Can you elaborate? – Werner Oct 26 '17 at 4:36
• @Werner Yes that's it ! In the command \@init@new@exo, I do twice the same thing (and in reality I would like to do that 5 times). But I can't do the same thing for the two commands \exercice and \topic using \expandafter and \csname. If you look at \exercice and \topic, you will see that their codes are very similar. – projetmbc Oct 26 '17 at 7:52
• @ShreevatsaR “Exercice” is French. – egreg Oct 26 '17 at 8:29
• @projetmbc The codes are completely identical, except for the keyword (exercice and topic). I'm not sure what's the reason for defining different sets of keys when they are the same for each kind. – egreg Oct 26 '17 at 9:08
• @egreg Oops. That was bad of me; thanks for pointing it out. I've deleted the comment. – ShreevatsaR Oct 26 '17 at 14:16

Something like this arranges all the csname construction to happen first so it is not intermingled with the pgf parsing.

\documentclass[12pt]{article}
\usepackage{pgfkeys}

\makeatletter
\newcommand\@init@new@exo[1]{%%
\pgfkeys{
/#1@keys/.is family,
/#1@keys,
pts/.initial  = {}, % Render the subject or not
src/.initial  = {}  % Number of the exam

}%%
%%
\expandafter\newcommand\csname #1@number\endcsname{Nb for #1}%%
}

\@init@new@exo{topic}
\@init@new@exo{exercice}

\newcommand\zz[1]{\expandafter\@zz
\csname#1\expandafter\endcsname
\csname#1@points\expandafter\endcsname
\csname#1@source\expandafter\endcsname
\csname#1@number\endcsname
{#1}}

\newcommand\@zz[5]{%
\newcommand#1[1][]{%
\begingroup
\pgfkeys{/#5@keys, ##1}%%
\pgfkeysgetvalue{/#5@keys/pts}{#2}%%
\pgfkeysgetvalue{/#5@keys/src}{#3}%%
#5 #4{} %%
(#2{} pts)
[Source :#3{}] %%
\endgroup
}%%
}

\zz{exercice}
\zz{topic}

\makeatother

\begin{document}

\exercice[pts = 1, src = Imaginaire]

\exercice

\topic[pts = 1, src = Imaginaire]

\topic[pts = 2]

\end{document}

• Thanks for this technical solution that will take me a long time to find ! :-) – projetmbc Oct 26 '17 at 14:45
• Is there an easy way to allow the use of more than 9 arguments with @zz ? I need to use more keys in my API. – projetmbc Oct 28 '17 at 16:01

You can include the definition of the command in the “init” phase:

\documentclass[12pt]{article}
\usepackage{xparse,chngcntr}

\ExplSyntaxOn

\NewDocumentCommand{\initkeys}{mo}
{
\newcounter{#1}
\IfValueT{#2} { \counterwithin{#1}{#2} }
\projetmbc_init_keys:n { #1 }
}

\cs_new_protected:Nn \projetmbc_init_keys:n
{
\keys_define:nn { projetmbc/#1 }
{
pts .tl_set:c = l_projetmbc_#1_pts_tl,
src .tl_set:c = l_projetmbc_#1_src_tl,
}
\exp_args:Nc \NewDocumentCommand {#1} { O{} }
{
\refstepcounter{#1}
\group_begin:
\keys_set:nn { projetmbc/#1 } { ##1 }
#1 ~ \use:c { the#1 } ~
\tl_if_empty:cF { l_projetmbc_#1_pts_tl }
{
\c_space_tl
( \tl_use:c { l_projetmbc_#1_pts_tl } ~ pts)
}
\tl_if_empty:cF { l_projetmbc_#1_src_tl }
{
\c_space_tl
[Source: ~ \tl_use:c { l_projetmbc_#1_src_tl } ]
}
\group_end:
}
}

\ExplSyntaxOff

\initkeys{exercice}
\initkeys{topic}[exercice]

\begin{document}

\exercice[pts = 1, src = Imaginaire]

\exercice

\topic[pts = 1, src = Imaginaire]

\topic[pts = 2]

\end{document}


• Do you know a good source for learning $\LaTeX{}$3 expl3 language ? I really like the way things are implemented. – projetmbc Oct 26 '17 at 17:39
• @projetmbc I learned it by perusing interface3.pdf and looking at expl3 code . – egreg Oct 26 '17 at 21:46