9

Consider the follwing command:

 \newcommand{\vektor}[3]{\left(\negthinspace\begin{smallmatrix}#1\\#2\\#3    \end{rsmallmatrix}
 \right)}

I can use it like

$\vektor{1}{2}{3}$

Now my question is whether it is possible to redefine the command such that the arguments are a comma separated list, such that I could write:

$\vektor{1,2,3}$
6

It's easy to do this sort of thing using \def, if you don't mind putting the argument inside delimiters that are not braces.

\documentclass{minimal}
\usepackage{amsmath}
\def\vektor(#1,#2,#3) {\left(\negthinspace\begin{smallmatrix}#1\\#2\\#3 \end{smallmatrix} \right)}
\begin{document}
\[ \vektor(a,b,c) \]
\end{document}

Obviously, the above would go horribly wrong if you replace the parentheses with braces.

EDIT

See Ryan Reich's answer (or Aditya's comment) for a neat modification that allows you to use braces.

  • 1
    Actually, it is easy to go from this solution to one that uses braces. \def\vektorwrapper#1{\vektor(#1)}. – Aditya Jun 2 '11 at 15:39
9

You can do it with \def and braces too, if you use two steps to hide Ian Thompson's code inside another macro.

\documentclass{minimal}
\usepackage{amsmath}
\def\vektor#1{\innervector(#1)}
\def\innervector(#1,#2,#3) {\left(\negthinspace\begin{smallmatrix}#1\\#2\\#3 \end{smallmatrix} \right)}
\begin{document}
\[ \vektor{a,b,c} \]
\end{document}
  • very nice! – Ian Thompson Jun 2 '11 at 14:41
  • That's cheating! – Seamus Jun 2 '11 at 15:09
8

As an after thought. You can make it a lot more general with a variable number of arguments and different row or column delimiters

\documentclass{article}
\usepackage{amsmath}
\makeatletter

\newcommand\myvector[2][\\]{%
    \global\def\my@delim{#1}%
    \left(\negthinspace\begin{smallmatrix}
        \my@vector #2,\relax\noexpand\@eolst%
    \end{smallmatrix}\right)}

\def\my@vector #1,#2\@eolst{%
   \ifx\relax#2\relax
      #1
   \else
      #1\my@delim
      \my@vector #2\@eolst%
   \fi}

\makeatother
\begin{document}
$x   = \myvector{1,2,3,:}$ and
$x^T = \myvector[,&]{1,2,3,...}$
\end{document}

This will give

enter image description here

  • ... incidentally, this solution helps me a lot. Thanks! – Reinhard Neuwirth Aug 5 '15 at 4:53
4

Here is a solution that uses the xparse package:

\usepackage{xparse}
\NewDocumentCommand{\InternalVector}{mmm}{%
  \left(\begin{smallmatrix}#1\\#2\\#3\end{smallmatrix}\right)%
}
\NewDocumentCommand{\vektor}{>{\SplitArgument{2}{,}}m}{\InternalVector#1}
1
\documentclass[a4paper]{article}
\usepackage{amsmath} % for smallmatrix

\begingroup\lccode`~=`,
  \lowercase{\endgroup\def\vekcomma{\mathcode`\,=\string"8000 \def~{\\}}}
\newcommand\vektor[1]{\left(\vekcomma
  \!\begin{smallmatrix}
  #1
  \end{smallmatrix}\!\right)}

\begin{document}
$a,b\vektor{a,b,c,d}\vektor{x,y,z}$
\end{document}

You are not limited to three components. The a,b part is to show that uses of the comma outside the argument of \vektor are allowed.

The fact that \left-\right make a group is fundamental, because this ensures that changes to the \mathcode of the comma are confined to that place. The special mathcode "8000 means that the comma behaves like an active character; the definition of the active comma is given in an indirect way, so as not to conflict with possible other meanings as an active character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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