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

Philip G. Ratcliffe philipratcliffe at tiscali.it
Tue Feb 3 11:45:24 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:
>
> \NeedsTeXFormat{LaTeX2e}
> \documentclass{article}
> \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
> counters
>                          \loop{% rows
>                                \loop{% columns
>                                      a_{\the\ir\the\ic}}% write element
>                                \advance\ic by 1 \ifnum\ic<4\TAB%
> alignement
> character `&'
>                                                            \repeat
>                                \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_{21}&a_{22}&a_{23}\\
>                    a_{31}&a_{32}&a_{33}\\\end{bmatrix} =
>     \begin{bmatrix}\MatrStrut\end{bmatrix} \]
>
> \end{document} %>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%
>
> % with `multido.sty'
> \multido{\ir=1+1}{3}{\multido{\ic=1+1}{3}{%
> a_{\ir\ic}\ifnum\ic=3\ENDROW\else\TAB\fi}}

The problem is probably that bmatrix reads the enclosed text looking for &
and \\ BEFORE anything gets expanded.  This makes it rather non-trivial to
interect with such a command.

I'm afraid I don't have any ready-made solution.

Cordialmente,  Philip G. Ratcliffe



More information about the texhax mailing list