4

I have created a macro to create a size of 2 x 1, but I would like to add an optional argument to make it 2 x 1, or 3 x 1 depending if you choose to a third argument. The current implementation is,

\NewDocumentCommand{\Vector}{ m m}{%
    \ensuremath{%
        \begin{bmatrix}
                #1 \\ 
                #2
        \end{bmatrix}
    }
}

How could this be accomplished so that I can call \Vector{1}{2} or \Vector{1}{2}{3}?

Michael
  • 43

3 Answers3

4

Your macro can open an empty \tmp and then test, if the next character is { using \futurelet primitive. If this is true then the parameter is read and added to the \tmp. If it is false then the reading of all parameters is closed and the \left[\matrix{\tmp}\right] is printed.

\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\Vector{\def\tmp{}\futurelet\next\VectorA}
\def\VectorA{\ifx\next\bgroup \expandafter\VectorB \else \expandafter\VectorC\fi}
\def\VectorB#1{\addto\tmp{#1\cr}\futurelet\next\VectorA}
\def\VectorC{\left[\matrix{\tmp}\right]}

test: $$ \Vector{1}{2}, \Vector{2}{3}{4}, \Vector{4}{5}{6}{7} $$

wipet
  • 74,238
3

Here is a command \Vector that takes an arbitrary number of entries separated by commas. There is also an optional argument to change the spacing. Default is currently set to 3ex, which is the distance between the baselines of the entries (not the space between). Change the value of \vecspace to set a different default.

enter image description here

Usage is \Vector[<spacing>]{a_1, a_2, ..., a_n}. See the code below for the examples illustrated.

\documentclass{article}

\usepackage{tikz,amsmath}

\newcommand{\vecspace}{3ex} \newcommand{\Vector}[2][\vecspace]{\left[\tikz[baseline]{ \foreach \c[count=\n] in {#2}{\xdef\numb{\n}} \foreach \c[count=\n] in {#2}{ \node[inner sep=0, anchor=base] at (0,{(\numb/2-\n+.5)*#1}){\c}; }}\right] }

\begin{document}

[ \Vector{1,2},\Vector{1,2,3},\Vector{1,2,3,4},\Vector[4ex]{1,2,3,4} ]

\end{document}

Sandy G
  • 42,558
  • oh boy that is a bit more intense that I expected, is their a much simpler version for either size 2 or 3, not extremely flexible, but with a single optional argument for size 3. – Michael Nov 06 '22 at 03:22
  • I suppose you could just define two commands that take 2 and 3 arguments respectively: \Vector{1}{2} and \Vectorr{1}{2}{3}. – Sandy G Nov 06 '22 at 03:31
  • Could the specifier O or o be utilized? – Michael Nov 06 '22 at 03:38
1

You might want to do

\NewDocumentCommand{\Vector}{mmo}{%
  \begin{bmatrix} #1 \\ #2 \IfValueT{#3}{\\ #3} \end{bmatrix}%
}

but I'm not sure you really want

\Vector{1}{2}  \Vector{1}{2}[3]

which is difficult to see in the typescript.

\documentclass{article}
\usepackage{amsmath}

\NewDocumentCommand{\Vector}{mmo}{% \begin{bmatrix} #1 \ #2 \IfValueT{#3}{\ #3} \end{bmatrix}% }

\begin{document}

[ \Vector{1}{2} \quad \Vector{1}{2}[3] ]

\end{document}

enter image description here

I find that its much simpler to manage lists:

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn \NewDocumentCommand{\Vector}{m} { \begin{bmatrix} \clist_use:nn { #1 } { \ } \end{bmatrix} } \ExplSyntaxOff

\begin{document}

[ \Vector{1,2} \quad \Vector{1,2,3} \quad \Vector{1,2,3,4} ]

\end{document}

enter image description here

If you don't like commas as separators and prefer, say, a semicolon, you can do as follows.

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn \NewDocumentCommand{\Vector}{m} { \begin{bmatrix} \seq_set_split:Nnn \l_tmpa_seq { ; } { #1 } \seq_use:Nn \l_tmpa_seq { \ } \end{bmatrix} } \ExplSyntaxOff

\begin{document}

[ \Vector{1;2} \quad \Vector{1;2;3} \quad \Vector{1;2;3;4} ]

\end{document}

Note I removed \ensuremath that does nothing really useful in this case and in several others.
Part of my campaign DUUE (Don't Use Unnecessary Ensuremath).

egreg
  • 1,121,712
  • Your script works well and it is what I wanted. In Overleaf it compiles and works correctly, but I still get an error saying "Missing $ inserted" when my example use case would be \Vector{$u_1$}{$u_2$}[$u_3$]. Would you know why this is occurring? – Michael Nov 11 '22 at 01:43
  • @Michael Why the $ characters? The entries are already typeset in math mode. – egreg Nov 11 '22 at 07:12
  • Oh, I didn't realize. Is everything inside of bmatrix the same as if you were typing between $? – Michael Nov 11 '22 at 21:36