[texhax] iterative construction of matrices - error at insertion of alignment characters or end line command

Matteo Gattanini matteo.gattanini at libero.it
Tue Feb 3 09:14:55 CET 2004

In writings dealing with linear algebra, in which it is continually 
necessary to visualize a lot of matrices of different order, is extremely 
comfortable provide a command that directly create the matrices after 
having declared the number of rows and columns and the name of the elements.
The following code tries to build the bases of such macro; the problem is 
the insertion of the alignment character `&' or end line command `\ 
\'  which causes an error. Anyone can help?
The code:

\usepackage{amsmath}% for `bmatrix' environment

\newcommand{\TAB}{\&} % should be `&'
\newcommand{\ENDROW}{\backslash\backslash}% should be `\\'

\newcommand{\MatrStrut}{\newcount\ir\ir=1\newcount\ic\ic=1 % rows & column 
                         \loop{% rows
                               \loop{% columns
                                     a_{\the\ir\the\ic}}% write element
                               \advance\ic by 1 \ifnum\ic<4\TAB% alignement 
character `&'
                               \ENDROW}%  line-end command `\\'
                         \advance\ir by 1 \ifnum\ir<4\repeat}
\begin{document} %>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%

The second matrix should be obtained with iterative code.
\[ \begin{bmatrix}a_{11}&a_{12}&a_{13}\\
                   a_{31}&a_{32}&a_{33}\\\end{bmatrix} =
    \begin{bmatrix}\MatrStrut\end{bmatrix} \]

\end{document} %>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%

% with `multido.sty'

More information about the texhax mailing list