I wish to define a command like \myrowvector{values={1,2,3}} and have spacing details set in the preamble. I have adapted code by user31729 to great effect, except... How should I deal with the last step in the iteration? I would like to prevent the insertion of a separator & (row vector) or \\ (column vector).
In order to increase spacing around the values, I have borrowed code by CarLaTex to create an alternative to the environment bmatrix (named bbmatrix). This generally works, but fails inside my \myrowvector command, with a "Missing $ inserted" message. I don't quite follow the part of the code with {+b} and I would appreciate a hint on how to use a matrix with greater spacing around values.
\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{array}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}
% https://tex.stackexchange.com/questions/570455/
% CarLaTex
\NewDocumentEnvironment{bbmatrix}{+b}{
\begin{+bmatrix}[columns={1.0em, c, colsep=2pt}]
#1
\end{+bmatrix}
}{}
% https://tex.stackexchange.com/questions/423115/
% user31729
\ExplSyntaxOn
\keys_define:nn { myrowvector }
{
values .clist_set:N = \l_myrowvector_values_clist,
}
\keys_define:nn { mycolvector }
{
values .clist_set:N = \l_mycolvector_values_clist,
}
\NewDocumentCommand\myrowvector{m}{
\keys_set:nn { myrowvector } { #1 }
\ensuremath{
\begin{bmatrix}
\clist_map_inline:Nn \l_myrowvector_values_clist {
##1 &
}
\end{bmatrix}
}
}
\NewDocumentCommand\mycolvector{m}{
\keys_set:nn { mycolvector } { #1 }
\ensuremath{
\begin{bmatrix}
\clist_map_inline:Nn \l_mycolvector_values_clist {
##1 \
}
\end{bmatrix}
}
}
\ExplSyntaxOff
\begin{document}
A row vector:
\begin{align}
\myrowvector{values={1,2,3}}
\end{align}
A column vector:
\begin{align}
\mycolvector{values={1,2,3}}
\end{align}
A less tightly spaced row vector:
\begin{align}
\begin{bbmatrix}
1 & 2 & 3
\end{bbmatrix}
\end{align}
A less tightly spaced column vector:
\begin{align}
\begin{bbmatrix}
1 \ 2 \ 3
\end{bbmatrix}
\end{align}
\end{document}
EDIT
I have accepted egreg's answer, but for posterity I'd like to copy skillmon's comment, which solved the spacing problem:
Instead of \clist_map_inline:Nn \l_mycolvector_values_clist { ##1 \\ } try to use \clist_use:Nn \l_mycolvector_values_clist { \\ } (or \clist_use:Nn \l_myrowvector_values_clist { & } respectively). See for instance tex.stackexchange.com/q/678399/117050.
Also, I have since found this related question: tex.stackexchange.com/questions/270223



\clist_map_inline:Nn \l_mycolvector_values_clist { ##1 \\ }try to use\clist_use:Nn \l_mycolvector_values_clist { \\ }(or\clist_use:Nn \l_myrowvector_values_clist { & }respectively). – Skillmon Mar 29 '23 at 19:33\clist_use:nninstead of temporary assignments to some other variable." Also do you have any idea why thebbmatrixwon't work? Thanks! – PatrickT Mar 29 '23 at 22:37