# How does this macro for augmented matrices work?

I've used this code I found somewhere to typeset literally hundreds of augmented matrices, and it's been amazing:

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage{arydshln}
\makeatletter
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{#1}}
\makeatother

\begin{document}
$$\begin{bmatrix}[ccc:c] 1&2&3&0 \\ 4&5&6&0 \\ 7&8&9&0 \\ \end{bmatrix}$$
\end{document}


My TeX and LaTeX skills are, um, fledgling. But I'm doing my best to learn.

However, I'm having trouble figuring out exactly how the redefined matrix command as above works. Here's my best guess. Can you correct me and/or fill in the gaps?

1. \makeatletter: The @ character by default entails some special significance and this command temporarily removes that special significance.
2. \renewcommand*: This is for redefining an existing macro. The starred variation here prevents the accidental inclusion of multiple paragraphs (\n\n or \par) when the macro is used.
3. \env@matrix: This is the command we're renewing, presumably. Is it defined somewhere in amsmath? Does it really have an @ symbol in its name?
4. [1]: This means the newly renewed command takes one optional argument.
5. [*\c@MaxMatrixCols c]: I don't know what the * at the beginning of this does. \c (afaik) is for doing cedillas, but that doesn't seem to fit here. MaxMatrixCols is a variable defined in amsmath that's 10 by default, are we doing some math with it here? Or re-setting it? Or what? The final c I also have no idea about. In fact, what is this entire construction? Is it some pattern matching for the optional parameter?
6. {%: The clever use of a comment here is presumably to ignore the newline. However, I don't see any difference in the spacing without it.
7. \hskip -\arraycolsep: This is a backspace by the amount of \arraycolsep
8. \let\@ifnextchar\new@ifnextchar: This is shadowing the command \@ifnextchar or something, but why? Neither \@ifnextchar or \new@ifnextchar seem to be used.
9. \array{#1}}: This inserts a raw array, and passes along the parameter. I'm unsure if #1 here refers to the optional [ccc:c] specification, or the actual contents of the matrix.
10. \makeatother: This presumably returns the @ symbol to its normal status.
-
I realize this question is not as general as maybe we'd really like here. But it does have lots of keywords that people searching for this type of information put into Google. (I should know, I'm among them.) An answer could be enlightening to many present and future La/TeX padawans. –  Harold Dec 1 '11 at 4:19
I think if you want to understand these package code, you could read the documented source code of LaTeX2e kernel: source2e.pdf first. –  Leo Liu Dec 1 '11 at 6:39

If you are still not familar with the usage of \renewcommand, you may need to learn more to understand the code.

1. \makeatletter: Makes @ to be a letter, thus it can be used in a macro name. See What do \makeatletter and \makeatother do?
2. As you know.
3. Read the documented source code of amsmath, amsmath.pdf (produced by compiling amsmath.dtx). It is the beginning of a set of matrix environments (matrix, bmatrix, pmatrix, etc.)
4. One argument. It's actually unknown at this time if the argument is optional or not.
5. [...] after [1] (or [2], [3]) means The first argument is optional, and specify the default value.

\c@MaxMatrixCols is the same of \value{MaxMatrixCols}, where MaxMatrixCols is a counter defined in amsmath (default to 10).

* \c@MaxMatrixCols c is used as the argument of \array, it is equivalent to

\begin{array}{*{10}{c}}

6. As you know.
7. As you know. The extra space of \arraycolsep is produced by \array i.e. array environment. I would use \begin{@{}*{10}{c}@{}} instead of this, if I write it.
8. \new@ifnextchar is defined in amsgen.sty loaded by amsmath. Read the documented source code.
9. \array is the beginning of array environment. Note that the individual matrix environments eventually call \endarray.
10. See above.
-
Thank you for taking the time to make this answer. I have learned a lot from it. –  Harold Dec 1 '11 at 15:18