호출 시 전달되는 매크로 팩토리 및 인수(2부)

호출 시 전달되는 매크로 팩토리 및 인수(2부)

저는 다른 매크로(B)를 생성하는 공장 매크로(A)를 만들고 있습니다. 매크로 B는 호출될 때 변수에 대한 매개변수로 얻는 것에 영향을 미치는 것으로 구성됩니다. 현재 A 없이 이 모든 작업을 수행할 수 있지만 다시 작성해야 하기 때문에 확장성이 없습니다.

보다호출 시 전달된 매크로 팩토리 및 인수 전체 맥락에 대해.

질문에 대한 답변에 이어 이전 질문(호출 시 전달된 매크로 팩토리 및 인수), 일부 포인트가 아직 열려 있는 상태에서 새로운 MWE를 만들 수 있습니다.

  • \clist_map_inline포인트 1 : 각 인수를 인위적으로 구분하기 위해 쉼표를 넣었습니다.
  • 포인트 2: 정의에 인수 목록을 넣었습니다(3m).\NewDocumentCommand{\DefinitionVariables}{ m m m }%
  • 포인트 3: 모든 인수를 \clist_map_inline함수에 넣었습니다.\clist_map_inline:nn { #1, #2, #3}%

따라서 이러한 변경 사항에 따른 질문은 다음과 같습니다.

  • 포인트 1: 혼수상태에 빠지지 않으려면 어떻게 해야 합니까? 나는 그것이 \clist_map_inline내장된 구분 기호인 쉼표를 사용하여 함수 와 관련이 있다고 생각합니다 . 함수가 구분 기호로 간주하여 인수를 잘라내기 때문에 인수 내부에 혼수상태가 있으면 문제가 발생합니다. 나는 일종의 \foreach argument in {all the arguments}. 그런 것이 존재합니까?
  • 포인트 2: 이것을 어떻게 동적으로 만들 수 있나요? 동적이란 의미는 다음과 같습니다. 때로는 함수가 3개의 인수를 사용할 수 있지만 때로는 7개 또는 9개(1과 9 사이)가 될 수도 있습니다. 다음과 같은 것numberOfArgument * m
  • 포인트 3: 이전 포인트와 관련이 있는데, 이것을 하드 코딩하지 않고 동적으로 만들려면 어떻게 해야 합니까? (그것이 \foreach argument in {all the arguments}해결될 것이다).

    \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
    

도움을 주시면 매우 감사하겠습니다!

답변1

이전 코드의 개발 버전입니다. new는 \DefinitionVariables고정된 이름과 구분 기호라는 두 개의 선택적 인수를 허용합니다. 후자는 (거의) 완전히 임의적입니다. 변수에 제공하려는 값에 나타나지 않는 문자(또는 그 조합)를 선택하면 됩니다.

\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}

여기에 이미지 설명을 입력하세요

인수를 중괄호로 구분할 수 있도록 하는 재귀를 기반으로 하는 다른 루틴입니다. 여는 중괄호가 뒤에 오는 한 새 변수가 정의됩니다.

\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}

여기에 이미지 설명을 입력하세요

관련 정보