.png)
Estou fazendo uma macro de fábrica (A) que produz outra macro (B). A macro B consiste em afetar o que obtém como parâmetros às variáveis quando chamada. Atualmente sou capaz de fazer tudo isso sem A, mas isso não é escalável, porque requer muita reescrita.
VerFábrica de macro e argumento passado na chamada para todo o contexto.
Seguindo as respostas da pergunta anterior (Fábrica de macro e argumento passado na chamada), eu poderia fazer um novo MWE com alguns pontos ainda em aberto:
- ponto 1: coloquei vírgulas no
\clist_map_inline
para delimitar artificialmente cada argumento - ponto 2: coloquei a lista do argumento na definição (os 3 m):
\NewDocumentCommand{\DefinitionVariables}{ m m m }%
- ponto 3: coloquei todos os argumentos na
\clist_map_inline
função:\clist_map_inline:nn { #1, #2, #3}%
Portanto, as questões que seguem essas mudanças são:
- ponto 1: Como posso não ter que colocar vírgulas? Acho que tem a ver com a
\clist_map_inline
função, sendo o coma um delimitador embutido. Isso levanta um problema quando há uma vírgula dentro de um argumento, pois a função irá cortá-lo, pois considera que é um delimitador. Eu estava pensando em ter uma espécie de\foreach argument in {all the arguments}
. Será que tal coisa existe ? - ponto 2: Como posso tornar isso dinâmico? Por dinâmico quero dizer: às vezes a função pode receber 3 argumentos, mas às vezes pode ter 7 ou 9 (entre 1 e 9). Algo como
numberOfArgument * m
ponto 3: Está relacionado ao ponto anterior, como posso também tornar isso dinâmico e não codificado? (isso
\foreach argument in {all the arguments}
resolveria).\documentclass[twoside]{article} % package pour utiliser une macro nested ac ses propres args \usepackage{xparse} \errorcontextlines32 \begin{document} %================================================================================== % Prerequisite : lines of code to define variableI to variableXVI %================================================================================== \iffalse \fi \newcommand{\DefinitVariable}[1]{% \expandafter\newcommand\csname variable\Roman{#1}\endcsname{}% }% % Loop for defining all the variable \newcounter{ctr} \loop \stepcounter{ctr} \expandafter\DefinitVariable{ctr}% \ifnum\thectr<16 \repeat %================================================================================== % Automation trial 5 : utilise la syntaxe expl3 \iftrue %\iffalse \ExplSyntaxOn \NewDocumentCommand{\DefinitionVariables}{ m m m }% <=== point 2 : there is as much 'm' as there is arguments { \int_zero:N \l_tmpa_int %\clist_map_inline:nn { #1 } \clist_map_inline:nn { #1, #2, #3}% <=== point 3 : allows not to put comas in the arguments, but rise the pb if there is comas inside the argument // { \int_incr:N \l_tmpa_int \tl_clear_new:c { variable \int_to_Roman:n { \l_tmpa_int } } \tl_set:cn { variable \int_to_Roman:n { \l_tmpa_int } } { ##1 } } } \ExplSyntaxOff \DefinitionVariables{Laetitia, 8 }{Pierre, 10}{Cedric}% <=== point 1 : coma inside the argument will be considered as a delimiter, so the mapping of variable will be wrong La variable 2 est : \variableII \\ FIN\\ La variable 1 est : \variableI \\ FIN\\ La variable 3 est : \variableIII \\ FIN\\ La variable 1 est : \variableI \\ FIN\\ \fi
Qualquer ajuda é muito apreciada!
Responder1
Uma versão de desenvolvimento do código anterior; o new \DefinitionVariables
aceita dois argumentos opcionais, o nome fixo e o delimitador; o último é (quase) completamente arbitrário, basta escolher um caractere (ou combinação deles) que não apareça nos valores que você deseja atribuir às variáveis.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\DefinitionVariables}{O{variable}mO{,}}
{
\aline_df:nnn { #1 } { #2 } { #3 }
}
\int_new:N \l_aline_df_int
\seq_new:N \l_aline_df_values_seq
\cs_new_protected:Nn \aline_df:nnn
{
\int_zero:N \l_aline_df_int
\seq_set_split:Nnn \l_aline_df_values_seq { #3 } { #2 }
\seq_map_inline:Nn \l_aline_df_values_seq
{
\int_incr:N \l_aline_df_int
\tl_clear_new:c { #1 \int_to_Roman:n { \l_aline_df_int } }
\tl_set:cn { #1 \int_to_Roman:n { \l_aline_df_int } } { ##1 }
}
}
\ExplSyntaxOff
\begin{document}
\DefinitionVariables{
Laetitia, 8; Patrick, 10; Cedric
}[;]
\noindent
La variable 1 est : \variableI\\
La variable 2 est : \variableII\\
La variable 3 est : \variableIII\\
FIN
\bigskip
\DefinitionVariables[var]{A,B,C}
\noindent
La var 1 est : \varI\\
La var 2 est : \varII\\
La var 3 est : \varIII\\
FIN
\end{document}
Uma rotina diferente baseada em recursão para permitir que os argumentos sejam delimitados por colchetes. Enquanto houver uma chave aberta, uma nova variável será definida.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\DefinitionVariables}{O{variable}}
{% pass control to an inner function
% #1 is the "name part", default "variable"
\aline_df:n { #1 }
}
% define an integer variable
\int_new:N \l_aline_df_int
\cs_new_protected:Nn \aline_df:n
{
% the integer variable assigns the trailing roman number
\int_zero:N \l_aline_df_int
% start the recursion
\__aline_df_peek:n { #1 }
}
\cs_new_protected:Nn \__aline_df_peek:n
{
% check whether the next token is { (ignoring spaces)
\peek_catcode_ignore_spaces:NT \c_group_begin_token
{
% if it is, increment the counter and call
% \__aline_df_next:nn { #1 } { #2 }, where
% { #2 } is the next braced group
\int_incr:N \l_aline_df_int
\__aline_df_next:nn { #1 }
}
}
\cs_new_protected:Nn \__aline_df_next:nn
{
% if the variable is already defined, clear it
% otherwise create it
\tl_clear_new:c { #1 \int_to_Roman:n { \l_aline_df_int } }
% set the variable
\tl_set:cn { #1 \int_to_Roman:n { \l_aline_df_int } } { #2 }
% restart the recursion
\__aline_df_peek:n { #1 }
}
\ExplSyntaxOff
\begin{document}
\DefinitionVariables{Laetitia, 8}{Patrick, 10}{Cedric}
\noindent
La variable 1 est : \variableI\\
La variable 2 est : \variableII\\
La variable 3 est : \variableIII\\
FIN
\bigskip
\DefinitionVariables[var]{A}{B}{C}{D}{E}{F}{G}{H}{I}{J}{K}
\noindent
La var 1 est : \varI\\
La var 2 est : \varII\\
La var 3 est : \varIII\\
La var 11 est : \varXI\\
FIN
\end{document}