In Matlab I can create a matrix by typing A = [1 2 3; 4 5 6; 7 8 9]
, for this the space is a delimiter between the entries of the matrix. I am wondering if there is a package which makes it possible to input a numerical array in a similar way directly into LaTeX?
For example, I would want to use the syntax:
$\SpaceDelimMat[c]{1 2 3; 4 5 6; 7 8 9}$,
instead of typing & (ampersand) 6 times like this:
$\left[\begin{array}[c]{rrr} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9\end{array}\right]$;
as to do the latter is very time consuming and makes LaTeX document less readable, especially when presenting a course about Matrices.
Best Answer
It's quite easy with LaTeX3 macros.
We first split the argument at the semicolons, then process each item, by splitting at spaces. A token list variable containing the body of the matrix is filled row by row and then delivered in the suitable
amsmath
environment. The optional argument (defaultb
, brackets) can bep
(parentheses),v
(single bar),V
(double bar),B
(braces) or even empty (no delimiter).Customizable version
One can also define a very customizable
\xmatlabmatrix
, taking as optional argument key-value options; the options areThe default for
colsep
is a space, but it can be customized locally with the key, or globally with\matlabmatrixsetup
in the preamble. In the example it is in the body; indeed it can be issued everywhere and obeys the scoping rules (remember to issuecolsep={,}
for a comma andcolsep={ }
for a space, with braces).Here's the code