# typesetting column vector

I would like to define a command which typesets a column vector.

For one vector I can have something like:

\left(
\begin{array}{c}
a\\
b\\
\end{array}
\right)


I would like the command to produce such a vector, for either 2 or 3 arguments. \colvec{a}{b}{c} should produce the same vector as above only with one more entry where \colvec{a}{b} will produce the above vector. How should I do it? I tried to overload a command name but that's impossible.

-
I've added a general solution (with a different syntax) at Typesetting a row vector –  egreg Sep 30 '12 at 16:37

Note that you have extra space around your vector. You should probably using something like (pmatrix is part of the amsmath package)

\begin{pmatrix}a\\b\end{pmatrix}


The standard LaTeX \newcommand provides a way to have a single optional argument.

\newcommand*\colvec[3][]{
\begin{pmatrix}\ifx\relax#1\relax\else#1\\\fi#2\\#3\end{pmatrix}
}


Note that you have to use \colvec[a]{b}{c} if you want three elements or \colvec{a}{b} if you want two.

Update
As per your request in the comments, here's one that takes any number of elements based on the number passed in the first argument.

\newcount\colveccount
\newcommand*\colvec[1]{
\global\colveccount#1
\begin{pmatrix}
\colvecnext
}
\def\colvecnext#1{
#1
\ifnum\colveccount>0
\\
\expandafter\colvecnext
\else
\end{pmatrix}
\fi
}


You use it exactly as you wanted, \colvec{5}{a}{b}{c}{d}{e}.

-
Can it be extended to a vector of arbitrary length? say something like: \colvec{5}{a}{b}{c}{d}{e} will produce a column vector with 5 entries? –  Dror Sep 4 '10 at 9:18
Yeah, you could do that. Seems like more hassle than it's worth though. How about \newcommand*\colvec[1]{\begin{pmatrix}#1\end{pmatrix} and you write \colvec{a\\b\\c\\d\\e}? –  TH. Sep 4 '10 at 9:31
Yes!!! This is what I was looking for! Simple and easy to use. Thanks! –  Dror Sep 4 '10 at 10:03
the smallpmatrix from mathtools may also be handy –  daleif Apr 21 '12 at 16:36
It's psmallmatrix not smallpmatrix. Just to clear up any mistakes (run into that problem just now). –  Florian Pilz Mar 9 '13 at 10:14

This is a more "TeX" approach. The number of rows is arbitrary. The columns are aligned right by default, but can be c or l as well:

\documentclass{article}

\makeatletter
\newcommand{\Spvek}[2][r]{%
\gdef\@VORNE{1}
\left(\hskip-\arraycolsep%
\begin{array}{#1}\vekSp@lten{#2}\end{array}%
\hskip-\arraycolsep\right)}

\def\vekSp@lten#1{\xvekSp@lten#1;vekL@stLine;}
\def\vekL@stLine{vekL@stLine}
\def\xvekSp@lten#1;{\def\temp{#1}%
\ifx\temp\vekL@stLine
\else
\ifnum\@VORNE=1\gdef\@VORNE{0}
\else\@arraycr\fi%
#1%
\expandafter\xvekSp@lten
\fi}
\makeatother

\begin{document}
$\Spvek{1;-2} \quad \Spvek[l]{1;-2;3}\quad \Spvek[c]{1;-2;-3}\quad\Spvek{1;2;-3;4}$
\end{document}


Output will be:

-
@percusse How did you crop the picture? I tried, but couldn't find a way. Thank you for cropping. –  Peter Breitfeld Apr 21 '12 at 20:39
I did it manually using Paint :) –  percusse Apr 21 '12 at 20:44
@PeterBreitfeld: See How do you crop an attached PDF? –  Werner Apr 21 '12 at 22:26
+1 Just what I was looking for. But what does Spvek mean? –  Oriol Mar 16 '13 at 20:17
Turns out to be abb. for spaltenvektor, in german pbreitfeld.de/schule2e.sty –  hos Nov 6 '13 at 20:13

You can use the xparse package to define commands with optional braced arguments. Something like (untested)

\DeclareDocumentCommand \colvec {mmg} {%
\IfNoValueTF #3 {%
\twocolvec {#1}{#2}
}{%
\threecolvec {#1}{#2}{#3}
}%
}


Where the two intermediate functions typeset the array as appropriate.

-
Does this means that I have to define separately the function \twocolvec and \threecolvec? –  Dror Sep 4 '10 at 9:17

I would to supplement the solution above about \Spvek by Peter B. Yes, it is a more "TeX" approach but pure "TeX" approach is in two lines only:

\def\spvec#1{\left(\vcenter{\halign{\hfil$##$\hfil\cr \spvecA#1;;}}\right)}
\def\spvecA#1;{\if;#1;\else #1\cr \expandafter \spvecA \fi}

$\spvec{1;2;3} + \spvec{1;2;-3;4} + \spvec{1;2}$


This solution will be work in LaTeX too because only TeX primitives are used here.

-

For vectors with only two elements, or any doublet you want to express in column form, there is a standard LaTeX command in math mode $\binom{a}{b}$ or alternatively ${n \choose k}$. These look nice with tight vertically lengthened parentheses.

-