參數數量不受限制 - 良好實踐

參數數量不受限制 - 良好實踐

以下是我如何處理宏的無限數量的參數。有更好的方法嗎?

\documentclass[12pt,a4paper]{article}

\usepackage{ifmtarg}
\usepackage{nicematrix}

\makeatletter
    \def\backify#1|#2\@nil{%
        \@ifmtarg{#2}{%
            #1%
        }{%
            #1 \\ \backify#2\@nil%
        }%
    }

    \newcommand\vcoord[1]{%
        \begin{pmatrix}
            \backify#1|\@nil %
        \end{pmatrix}%
    }
\makeatother

\begin{document}

$\vcoord{3 | -4 | 0}$

\end{document}

答案1

我提出了一種不同的方法來應對你似乎正在做的事情。

\documentclass[12pt,a4paper]{article}

\usepackage{xparse}
\usepackage{amsmath}

\ExplSyntaxOn

% a general purpose macro for defining other macros
\NewDocumentCommand{\makemultiargument}{mmmmm}
 {
  \projetmbc_multiarg:nnnnn { #1 } { #2 } { #3 } { #4 } { #5 }
 }

% allocate a private variable
\seq_new:N \l__projetmbc_generic_seq

% the internal version of the general purpose macro
\cs_new_protected:Nn \projetmbc_multiarg:nnnnn
 {% #1 = separator
  % #2 = multiargument
  % #3 = code before
  % #4 = code between
  % #5 = code after

  % a group allows nesting
  \group_begin:
  % split the multiargument into parts
  \seq_set_split:Nnn \l__projetmbc_generic_seq { #1 } { #2 }
  % execute the <code before>
  #3
  % deliver the items, with the chosen material between them
  \seq_use:Nn \l__projetmbc_generic_seq { #4 }
  % execute the <code after>
  #5
  % end the group started at the beginning
  \group_end:
 }

\ExplSyntaxOff

% separator: |; before: \begin{pmatrix}; between: \\, after: \end{pmatrix}
\newcommand{\vcoord}[1]{%
  \makemultiargument{|}{#1}{\begin{pmatrix}}{\\}{\end{pmatrix}}%
 }

\begin{document}

$\vcoord{3 | \vcoord{-4 | -3 } | 0}$ 

\end{document}

在此輸入影像描述

可以使用尾隨可選參數為每個條目新增格式(如果沒有它,巨集與先前的版本中的相同)。

\documentclass[12pt,a4paper]{article}

\usepackage{xparse}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentCommand{\makemultiargument}{mmmmmo}
 {
  \projetmbc_multiarg:nnnnnn { #1 } { #2 } { #3 } { #4 } { #5 } { #6 }
 }

\seq_new:N \l__projetmbc_generic_seq

\cs_new_protected:Nn \projetmbc_multiarg:nnnnnn
 {% #1 = separator
  % #2 = multiargument
  % #3 = code before
  % #4 = code between
  % #5 = code after
  % #6 = ornament to items

  % allow nesting
  \group_begin:
  % split the multiargument
  \seq_set_split:Nnn \l__projetmbc_generic_seq { #1 } { #2 }
  \tl_if_novalue:nF { #6 }
   {
    \seq_set_eq:NN \l__projetmbc_temp_seq \l__projetmbc_generic_seq
    \seq_set_map:NNn \l__projetmbc_generic_seq \l__projetmbc_generic_seq { #6 }
   }
  #3
  \seq_use:Nn \l__projetmbc_generic_seq { #4 }
  #5
  \group_end:
 }

\ExplSyntaxOff

\NewDocumentCommand{\vcoord}{m}
 {
  \makemultiargument{|}{#1}{\begin{pmatrix}}{\\}{\end{pmatrix}}
 }

\NewDocumentCommand{\pboxed}{m}{\boxed{#1}}

\begin{document}

$\vcoord{3 | \vcoord{-4 | -3 } | 0}$

$\makemultiargument{|}{-4 | -3  | 0}{\begin{pmatrix}}{\\}{\end{pmatrix}}[\pboxed{#1}]$

\end{document}

請注意,尾隨參數中的巨集必須是完全可擴展的或受保護的(這就是我定義的原因\pboxed;標準\boxed不會失敗,但只是偶然)。

在此輸入影像描述

答案2

使用expl3(由 載入xparse)您可以使用內建序列類型。以下定義了您\vcoord僅使用內建函數。結果不可擴展。它還添加了兩個宏,\readsequence用於設定序列變數(不可擴展),並\usesequence擴展為序列,每個序列元素由其參數分隔(可擴展)。你可以看看interface3有關 中可用函數的文件expl3

\documentclass[]{article}

\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \l__projetmbc_seq
\NewDocumentCommand \readsequence { +m +m }
  {
    \seq_set_split:Nnn \l__projetmbc_seq { #1 } { #2 }
  }
\NewExpandableDocumentCommand \usesequence { +m }
  {
    \seq_use:Nnnn \l__projetmbc_seq { #1 } { #1 } { #1 }
  }
\NewDocumentCommand \vcoord { m }
  {
    \group_begin:
    \seq_set_split:Nnn \l__projetmbc_seq { | } { #1 }
    \begin { pmatrix }
      \seq_use:Nnnn \l__projetmbc_seq { \\ } { \\ } { \\ }
    \end { pmatrix }
    \group_end:
  }
\ExplSyntaxOff

\begin{document}
\readsequence{|}{a|b|c|d|e}
\[
  \begin{pmatrix}
    \usesequence{\\}
  \end{pmatrix}
\]

\[
  \vcoord{3|-4|0}
\]
\end{document}

答案3

不確定最佳實踐,但您可以稍微簡化定義:

\documentclass[12pt,a4paper]{article}

\usepackage{amsmath}


\long\def\backify#1|{#1\backify\\}
\def\endbackify#1#2{}

    \newcommand\vcoord[1]{%
        \begin{pmatrix}
            \backify#1|\endbackify|%
        \end{pmatrix}%
    }


\begin{document}

$\vcoord{3 | -4 | 0}$

\end{document}

相關內容