On Fri, 23 May 2014, Matthias Weber wrote:
Dear All,
I would like to define a command that expands
\vector{2,4} % or vector[2,4] if that’s easier
to
\startpmatrix
\NC 2 \NR
\NC 4 \NR
\stoppmatrix
and more generally
\vector{2,4,1,7}
to
\startpmatrix
\NC 2 \NR
\NC 4 \NR
\NC 1 \NR
\NC 7 \NR
\stoppmatrix
Any hints how to achieve this?
Surprisingly, this is not as simple as it may seem at first glance. Some
time ago, I had written a module to achieve this. See attached. The usage
is:
\usemodule[simplematrix]
\definesimplematrix[MATRIX][fence=bracket]
(any predefined math-fence will work) and then:
\MATRIX{1,2,3} for row vectors and \MATRIX{1;2;3} for column vectors, and
\MATRIX{1,2,3; 4,5,6} for matrices.
You can use \definesimplematrix[...][distance=..., align=...] to influence
the distance and align keys of mathmatrix.
Aditya
%D \module
%D [ file=t-simplematrix,
%D version=2014.02.18,
%D title=\CONTEXT\ User Module,
%D subtitle=Simple matrix,
%D author=Aditya Mahajan,
%D date=\currentdate,
%D copyright=Aditya Mahajan,
%D email=adityam <at> ieee <dot> org,
%D license=Simplified BSD License]
\writestatus{loading}{Simple matrix (ver: 2014.02.18)}
\startmodule[simplematrix]
\unprotect
\definenamespace
[simplematrix]
[ \c!type=module,
\c!name=simplematrix,
\c!command=\v!yes,
setup=\v!list,
\s!parent=simplematrix,
]
\setupsimplematrix
[
\c!distance=\emwidth,
\c!mathstyle=,
fence=bracket,
\c!align=
]
\appendtoks
\setevalue{\currentsimplematrix}{\usesimplematrix[\currentsimplematrix]}
\to \everydefinesimplematrix
\newtoks\simplematrixtoks
\define[1]\simplematrix_row
{\processcommalist[#1]\simplematrix_col
\appendtoks \NR \to \simplematrixtoks}
\define[1]\simplematrix_col
{\appendtoks \NC #1 \to \simplematrixtoks}
\unexpanded\def\usesimplematrix
{\dodoubleargument\usesimplematrix_indeed}
\def\simplematrix_left
{\edef\p_left{\namedmathfenceparameter{\simplematrixparameter{fence}}\c!left}%
\normalleft\ifx\p_left\empty.\else\Udelimiter\plusfour\fam\p_left\relax\fi
\,}
\def\simplematrix_right
{\edef\p_right{\namedmathfenceparameter{\simplematrixparameter{fence}}\c!right}%
\,
\normalright\ifx\p_right\empty.\else\Udelimiter\plusfive\fam\p_right\relax\fi}
\def\usesimplematrix_indeed[#name][#options]#matrix%
{\begingroup
\edef\currentsimplematrix{#name}%
\setupsimplematrix[#name][#options]%
\simplematrixtoks\emptytoks
\startusemathstyleparameter\simplematrixparameter
\appendtoks
\bgroup
\startmathmatrix
[
\c!distance=\simplematrixparameter\c!distance,
\c!left=\simplematrix_left,
\c!right=\simplematrix_right,
\c!align=\simplematrixparameter\c!align,
]
\to \simplematrixtoks
\processlist[];\simplematrix_row[#matrix]%
\appendtoks \stopmathmatrix \egroup \to \simplematrixtoks
\the\simplematrixtoks
\stopusemathstyleparameter
\endgroup}
\protect
\stopmodule
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the
Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________