ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Aditya Mahajan <adityam@umich.edu>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Simple command with variable number of arguments
Date: Fri, 23 May 2014 06:56:15 -0400 (EDT)	[thread overview]
Message-ID: <alpine.DEB.2.02.1405230650470.30428@nqv-guvaxcnq> (raw)
In-Reply-To: <6EAF5FDE-7BE4-4766-B683-FA9994227A04@indiana.edu>

[-- Attachment #1: Type: TEXT/PLAIN, Size: 946 bytes --]

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

[-- Attachment #2: Type: TEXT/PLAIN, Size: 2365 bytes --]

%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

[-- Attachment #3: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

  parent reply	other threads:[~2014-05-23 10:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-23  9:54 Matthias Weber
2014-05-23 10:08 ` Otared Kavian
2014-05-23 10:55   ` Matthias Weber
2014-05-23 10:56 ` Aditya Mahajan [this message]
2014-05-23 11:14   ` Matthias Weber
2014-05-23 11:37     ` Aditya Mahajan
2014-05-23 12:08       ` Matthias Weber
2014-05-23 11:44 ` luigi scarso
2014-05-23 12:12   ` Matthias Weber
     [not found] <mailman.56.1400848757.2240.ntg-context@ntg.nl>
2014-05-23 13:48 ` Jeong Dal
2014-05-23 18:00   ` Hans Hagen
     [not found]   ` <537FA66D.3000608@wxs.nl>
2014-05-24  0:36     ` Jeong Dal
2014-05-24  6:55     ` Jeong Dal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.02.1405230650470.30428@nqv-guvaxcnq \
    --to=adityam@umich.edu \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).