인수 개수 제한 없음 - 모범 사례

인수 개수 제한 없음 - 모범 사례

매크로의 무제한 개수의 인수를 사용하여 작업하는 방법은 다음과 같습니다. 그렇게 하는 더 좋은 방법이 있나요?

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

관련 정보