TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

For one vector I can have something like:


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.

share|improve this question
I've added a general solution (with a different syntax) at Typesetting a row vector – egreg Sep 30 '12 at 16:37
up vote 35 down vote accepted

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


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


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

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.


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

share|improve this answer
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:




\Spvek{1;-2} \quad \Spvek[l]{1;-2;3}\quad \Spvek[c]{1;-2;-3}\quad\Spvek{1;2;-3;4}

Output will be:

enter image description here

share|improve this answer
@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.

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

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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.