1

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.

enter image description here

\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

PatrickT
  • 2,923
  • 1
    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). – Skillmon Mar 29 '23 at 19:33
  • 1
    See for instance https://tex.stackexchange.com/q/678399/117050 – Skillmon Mar 29 '23 at 19:34
  • Thank you @Skillmon, this does the trick. I can't believe I didn't see your answer before. Now that the main problem is solved, it feels like a duplicate of that question. But the "api" is different and it's perhaps interesting to have an example of it on the site. I'd like to understand what you mean when you write "you can use \clist_use:nn instead of temporary assignments to some other variable." Also do you have any idea why the bbmatrix won't work? Thanks! – PatrickT Mar 29 '23 at 22:37

1 Answers1

3

As tabularray environments don't like that & is hidden in macros, you want to use +bmatrix as a command rather than an environment.

And you want to exploit \clist_use:Nn rather than mapping.

\documentclass[a4paper]{article}

\usepackage[english]{babel} %\usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} %\usepackage{expl3} %\usepackage{xparse} \usepackage{array} \usepackage{tabularray} \UseTblrLibrary{amsmath}

\ExplSyntaxOn

% adapted from % https://tex.stackexchange.com/questions/570455/ % CarLaTex

\cs_new_protected:Nn \patrickt_bbmatrix:n { \begin{+bmatrix}[columns={1.0em, c, colsep=2pt}] #1 \end{+bmatrix} } \cs_generate_variant:Nn \patrickt_bbmatrix:n { e }

% https://tex.stackexchange.com/questions/423115/ % user31729 \keys_define:nn { patrickt/vector } { values .clist_set:N = \l_patrickt_vector_values_clist, } \NewDocumentCommand\myrowvector{m} { \keys_set:nn { patrickt/vector } { #1 } \patrickt_bbmatrix:e { \clist_use:Nn \l_patrickt_vector_values_clist { & } } } \NewDocumentCommand\mycolvector{m}{ \keys_set:nn { patrickt/vector } { #1 } \patrickt_bbmatrix:e { \clist_use:Nn \l_patrickt_vector_values_clist { \ } } } \ExplSyntaxOff

\begin{document}

A row vector: \begin{equation} \myrowvector{values={1,2,3}} \end{equation}

A column vector: \begin{equation} \mycolvector{values={1,2,3}} \end{equation}

\end{document}

No need to define two different sets of keys. I also removed \ensuremath that has no real purpose and changed align* to equation* (never use the former for single equations).

enter image description here

On the other hand, I see no real reason to use a syntax like

\myrowvector{values={1,2,3}}

over

\myrowvector{1,2,3}

that you can accomplish with

\documentclass[a4paper]{article}

\usepackage[english]{babel} \usepackage[T1]{fontenc} \usepackage{array} \usepackage{tabularray} \UseTblrLibrary{amsmath}

\ExplSyntaxOn

% adapted from % https://tex.stackexchange.com/questions/570455/ % CarLaTex

\cs_new_protected:Nn \patrickt_bbmatrix:n { \begin{+bmatrix}[columns={1.0em, c, colsep=2pt}] #1 \end{+bmatrix} } \cs_generate_variant:Nn \patrickt_bbmatrix:n { e }

\NewDocumentCommand\myrowvector{m} { \patrickt_bbmatrix:e { \clist_use:nn { #1 } { & } } } \NewDocumentCommand\mycolvector{m}{ \patrickt_bbmatrix:e { \clist_use:nn { #1 } { \ } } } \ExplSyntaxOff

\begin{document}

A row vector: \begin{equation} \myrowvector{1,2,3} \end{equation}

A column vector: \begin{equation} \mycolvector{1,2,3} \end{equation}

\end{document}

A different and “unified” approach:

\documentclass[a4paper]{article}

\usepackage[english]{babel} \usepackage[T1]{fontenc} \usepackage{array} \usepackage{tabularray} \UseTblrLibrary{amsmath}

\ExplSyntaxOn

% adapted from % https://tex.stackexchange.com/questions/570455/ % CarLaTex

\cs_new_protected:Nn \patrickt_bbmatrix:n { \begin{+bmatrix}[columns={1.0em, c, colsep=2pt}] #1 \end{+bmatrix} } \cs_generate_variant:Nn \patrickt_bbmatrix:n { V }

\NewDocumentCommand\mymatrix{m} { __patrickt_bbmatrix_build:n { #1 } \patrickt_bbmatrix:V \l__patrickt_bbmatrix_body_tl }

\tl_new:N \l__patrickt_bbmatrix_body_tl \seq_new:N \l__patrickt_bbmatrix_rows_seq

\cs_new_protected:Nn __patrickt_bbmatrix_build:n { \seq_set_split:Nnn \l__patrickt_bbmatrix_rows_seq { ; } { #1 } \tl_clear:N \l__patrickt_bbmatrix_body_tl \seq_map_inline:Nn \l__patrickt_bbmatrix_rows_seq { \tl_put_right:Nx \l__patrickt_bbmatrix_body_tl { \clist_use:nn { ##1 } { & } \exp_not:N \ } } }

\ExplSyntaxOff

\begin{document}

\begin{equation} \mymatrix{1,2,3}\ne \mymatrix{1;2;3}\ne \mymatrix{1,2;3,4;5,6} \end{equation}

\end{document}

enter image description here

PatrickT
  • 2,923
egreg
  • 1,121,712
  • that's great, thanks egreg for another wonderful answer! I had used the syntax values= because it's what I'd come across first in the post I linked to in my question. Since then @skillmon has shown me closely related questions I had missed. But perhaps this more verbose syntax will come in handy if I define the data (the values) in an external file I read in (my original plan, not mentioned in my question)? I also had another plan to have a command to transpose. If I get there. For everyday usage the simpler syntax is definitely more convenient! – PatrickT Mar 30 '23 at 15:46
  • 1
    @PatrickT Just try it yourself by comparing the example I showed when equation* is replaced by align*. – egreg Mar 30 '23 at 16:48
  • 1
    @PatrickT Try when the line preceding the display is short. – egreg Mar 30 '23 at 17:11
  • just noticed that this only works well for single digits, e.g.\mymatrix{1.01,2.01,3.01} gets the digits cramped. – PatrickT Apr 10 '23 at 08:12
  • 1
    @PatrickT That's the setting you asked to tblr, isn't it? Namely columns={1.0em,...} – egreg Apr 10 '23 at 08:21
  • Oh, I see! I had copied that from the original code without paying attention. Thanks! – PatrickT Apr 10 '23 at 08:28