Replacing ampersands in argument
Transforming my comment into an answer: you need to hide the ampersand from align
's scanning mechanism.
The easiest way to do this is placing the command in braces:
\begin{align}
x = {\colvec{3 & 3}}
\end{align}
To have a more comfortable syntax one can add the braces to the definition of \colvec
:
\documentclass{article}
\usepackage{xstring}
\usepackage{amsmath}
\DeclareRobustCommand\colvec[1]{%
{\saveexpandmode\expandarg
\StrSubstitute{\noexpand#1}&\ [\vectorentries]%
\restoreexpandmode[\vectorentries]^T}}
\begin{document}
\begin{align}
x &= \colvec{3 & 3} \\
E &= mc^2 % to show that alignment still works
\end{align}
\end{document}
Why not using a simpler strategy?
\newcommand{\colvec}[1]{
{
\setlength{\arraycolsep}{.16667em}
[\begin{matrix}#1\end{matrix}]^T
}
}
A different way of replacing the &
with a space is with xparse
and expl3
:
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\colvec}{m}
{
\tl_set:Nn \l_tmpa_tl {#1}
\tl_replace_all:Nnn \l_tmpa_tl { & } { \ }
[\tl_use:N \l_tmpa_tl]^T
}
\ExplSyntaxOff
This doesn't even require the extra braces.