Is there a way to make the solutions here
and here
work by including \ensuremath in their definitions?
Is there a way to make the solutions here
and here
work by including \ensuremath in their definitions?
For the cases where you are starting and ending in different macros just split the definition of \ensuremath in to two. So put
\relax\ifmmode\let\myend\relax\else$\def\myend{$}\fi
At the start of the first macro definition and
\myend
at the end of the second one.
Note however a format such as LaTeX works best if package authors follow the syntax guidelines so that the language as a whole remains more or less coherent and consistent. TeX makes a big distinction between math mode and text mode \. is not the same as \dot for example, and a user needs to be aware if they are in math mode or text. Sometimes for some small symbols where the user may not really need to know that math mode is being used internally \ensuremath is useful, but it is not usually a good idea to use it for something like a vector taking user-supplied arguments as the user in any case needs to know that the supplied arguments are processed as math. Compare the standard LaTeX array environment, this is identical to tabular except that each cell is in math mode, however rather than make this switch to math mode implicit the environment checks that it is not already in math mode and makes an error if not. Thus the user needs to switch to math before using array. This error check is purely for maintaining language consistency, the array environment would otherwise work in the same way in text or math. Similar considerations apply to argument structure. LaTeX comamnds never take variable number of {} arguments. {} arguments are intended to be mandatory. Optional arguments should be in [] and variable number of items should be in comma separated list, so \colvec{a,b} would be a syntax far more compatible with latex than \colvec{a}{b} if the vector may be of arbitrary length.
I took David Carlisle's advice and found a solution here using a comma separated list of arguments. Arguments of custom commands as comma separated list? really helped me.
\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcommand\rcvector[2][\\]{\ensuremath{%
\global\def\rc@delim{#1}%
\negthinspace\begin{pmatrix}
\rc@vector #2,\relax\noexpand\@eolst%
\end{pmatrix}}}
\def\rc@vector #1,#2\@eolst{%
\ifx\relax#2\relax
#1
\else
#1\rc@delim
\rc@vector #2\@eolst%
\fi}
\makeatother
\newcommand{\colvect}{\rcvector}
\newcommand{\rowvect}{\rcvector}
\begin{document}
\colvect{ct,x,y,z}\rowvect[,\;]{ct,x,y,z}
\end{document}
{}arguments. I would not use that syntax in LaTeX it breaks a very string latex syntax rule that commands always take the same argument form. Every documented latex command in the core distribution follows that. The way to get an arbitrary number of arguments is to use a single{}argument with a comma separated list. (That would also, not coincidentally, make it trivial to add\ensuremath) – David Carlisle Sep 22 '12 at 22:45