8

I want to simplify codes for writing vectors. For example, if I write

\vector{1,2,3}

it would be equivalent to

\begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}

Similarly

\vector{1,2,3,4}

would be

\begin{bmatrix} 1 \\ 2 \\ 3 \\ 4 \end{bmatrix}

How can I create a macro for this type of work?

3 Answers 3

8

The stackengine package already supports this construction with \bracketVectorstack, which I bundle in the form asked for by the OP. Of course, it is not actually using bmatrix.

Note that if matrix construction is needed rather than vectors, look for the extensions provided by the tabstackengine package. Also, the inter-row baselineskip can be altered with, for example, \setstackgap{L}{1.2\baselineskip}.

\documentclass{article}
\usepackage{stackengine}
\newcommand\Vector[1]{\setstackEOL{,}\bracketVectorstack{#1}}
\begin{document}
\[
X = \Vector{1,2,3,4} \ne \Vector{1,x,3}
\]
\end{document}

enter image description here

6

The splitting is quite easy with expl3 syntax and a wrapper command, say \feqvector. This example does allow for setting the separator and the type of the matrix, say b or p or any other allowed type.

\documentclass{article}

\usepackage{mathtools}
\usepackage{xparse}

\ExplSyntaxOn
\clist_new:N \l_feq_vector_clist
\NewDocumentCommand{\feqvector}{O{\\}mO{b}}{
  \clist_set:Nn \l_feq_vector_clist {#2} % Set the list
  \begin{#3matrix}
  \clist_use:Nn \l_feq_vector_clist {#1} % show it with separator from #1 (\\)
  \end{#3matrix}
}
\ExplSyntaxOff

\begin{document}
A few: $\feqvector{1,2,3}$

More of them: $\feqvector{1,2,3,4,5}$

horizontal: $\feqvector[&]{1,2,3,4,5}$

Now with parenthesis and horizontal: $\feqvector[&]{1,2,3}[p]$
\end{document}

enter image description here

1
  • There should not be braces around N arguments (although in many cases they do no harm); there's no need to clear the clist, if you set it afterwards.
    – egreg
    Sep 29, 2015 at 21:05
4

If you can accept a bit different syntax (one character more in the first case), there is a simple solution:

\def\vector#1.{\begin{bmatrix} #1 \end{bmatrix}

with usage

\vector 1\\2\\3. or \vector 1\\2\\3\\4.

Certainly, dot as the end of argument(s) is only an example.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .