# column and row vectors with builtin math mode

Is there a way to make the solutions here

typesetting column vector

and here

Typesetting a Row Vector

work by including \ensuremath in their definitions?

• Yes just add \ensuremath{...} around the existing definitions. What did you try? Using \ensuremath for such constructs is probably a bad idea though really, the TeX model is to have different commands for TeX and math and trying to over-use \ensuremath to use the same commands for both leads to pain in the end. – David Carlisle Sep 22 '12 at 22:04
• Of course I tried that, but no matter where I put the \ensuremath{} I always got an error telling me math delimiters were missing. Where's the best place to put them? Not having to specify math delimiters is a feature of the physics/math related package I'm developing. So far there have been no problems. – user19024 Sep 22 '12 at 22:27
• Oh sorry I just saw the first answer in the row case not the second that took arbitrary number of {} 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
• – Peter Grill Sep 23 '12 at 16:13

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}

• Clearly I give you +1 for taking my advice:-) – David Carlisle Sep 25 '12 at 8:43
• @David I took your advice so seriously that I'm now rewriting parts of my package to use this comma separated list strategy. I think it better suits the package's intended audience. Many thanks! – user19024 Sep 25 '12 at 17:42