
Así es como hago para trabajar con un número ilimitado de argumentos de una macro. ¿Existe una mejor manera de hacerlo?
\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}
Respuesta1
Te propongo una forma diferente de afrontar lo que pareces estar haciendo.
\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}
Es posible agregar formato a cada entrada, con un argumento final opcional (sin él la macro es la misma que en la versión anterior).
\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}
Tenga en cuenta que las macros en el argumento final deben ser completamente expandibles o estar protegidas (por eso lo definí \pboxed
; el estándar \boxed
no fallaría, sino sólo por casualidad).
Respuesta2
Usando expl3
(que se carga mediante xparse
) puede usar el tipo de secuencia incorporado. A continuación se define el \vcoord
uso exclusivo de funciones integradas. El resultado no es ampliable. También agrega dos macros, \readsequence
que configuran una variable de secuencia (no expandible) y \usesequence
que expande la secuencia con cada elemento de secuencia separado por su argumento (ampliable). puedes mirarinterface3
para la documentación de las funciones disponibles en 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}
Respuesta3
No estoy seguro de las mejores prácticas, pero puedes simplificar un poco la definición:
\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}