So I'm trying to make a simple macro that creates a vector for me. I'm expecting that I can do
\myVec{1,...,5}
and the macro can expand to \begin{bmatrix}1 & 2 & 3 & 4 & 5\end{bmatrix}
. However, it seems that there's no way I can get an &
symbol to expand correctly. The following code stopped working if I put &
instead of ,
inside the if
branch. What am I missing here?
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgffor}
\makeatletter
\newcommand{\@length}[2]{ % Usage: \len{list}{counter}
\foreach\e in{#1}{
\stepcounter{#2}
}
}
\newcommand{\@genMatrix}[1]{
\newcounter{@len}
\@length{#1}{@len} % get the length of the list after expansion
\foreach \elem[count=\@i] in {#1}{
\elem
\ifnum\@i<\the@len
, % it stopped working if I change it to &
\fi
}
}
\newcommand{\myVec}[1]{
\begin{bmatrix}
\expandafter\@genMatrix{#1}
\end{bmatrix}
}
\makeatother
\begin{document}
\begin{equation*}
\myVec{1,...,5}
\end{equation*}
\end{document}