宏工廠和調用時傳遞的參數(第 2 部分)

宏工廠和調用時傳遞的參數(第 2 部分)

我正在製作一個工廠巨集 (A),它會產生另一個巨集 (B)。宏 B 在於影響它在呼叫時作為變數參數所獲取的內容。目前我可以在沒有 A 的情況下完成所有這些工作,但這不可擴展,因為它需要大量重寫。

宏工廠和調用時傳遞的參數 對於整個上下文。

繼上一個問題的答案之後(宏工廠和調用時傳遞的參數),我可以創建一個新的 MWE,其中一些點仍然打開:

  • 第 1 點:我在 中加入了逗號\clist_map_inline來人為地分隔每個參數
  • 第 2 點:我將參數清單放入定義中(3 m):\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點:我怎麼能讓這個m動態化?我所說的動態,是指:有時,函數可以接受 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}

在此輸入影像描述

相關內容