[Tex/LaTex] LaTeX newcommand with a variable number of arguments

macros

I'm trying to create a new command in LaTeX that will let me do

\begin{bmatrix}1\\2\\3\end{bmatrix}

Like this:

\m{1\\2\\3}

I can make two functions, one for the begin and one for the end bmatrix, and that works, but I'd like to make it work with one command. The issue with that is there can be any number of variables in the command. I've looked around and found some answers showing how to do it if you have, say, an optional second variable only. I'm trying to make this work with any number of arguments.

Best Answer

If you'd like a command where you can pass a single argument as a comma-separated list, rather than using \\ inside the argument (like Werner suggested), you could do the following:

\documentclass{article}

\usepackage{amsmath}
\usepackage{etoolbox} % needed for the \forcsvlist command

\newcommand*{\addrow}[1]{#1\\}
\newcommand{\m}[1]{%
      \begin{bmatrix}
        \forcsvlist\addrow{#1}%
      \end{bmatrix}}

\begin{document}

\[
\m{1,2,3}
\]

\end{document}

enter image description here