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?
\makeatletter
: The@
character by default entails some special significance and this command temporarily removes that special significance.\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.\env@matrix
: This is the command we're renewing, presumably. Is it defined somewhere inamsmath
? Does it really have an@
symbol in its name?[1]
: This means the newly renewed command takes one optional argument.[*\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 inamsmath
that's 10 by default, are we doing some math with it here? Or re-setting it? Or what? The finalc
I also have no idea about. In fact, what is this entire construction? Is it some pattern matching for the optional parameter?{%
: 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.\hskip -\arraycolsep
: This is a backspace by the amount of\arraycolsep
\let\@ifnextchar\new@ifnextchar
: This is shadowing the command\@ifnextchar
or something, but why? Neither\@ifnextchar
or\new@ifnextchar
seem to be used.\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.\makeatother
: This presumably returns the@
symbol to its normal status.
Best Answer
If you are still not familar with the usage of
\renewcommand
, you may need to learn more to understand the code.\makeatletter
: Makes@
to be a letter, thus it can be used in a macro name. See What do \makeatletter and \makeatother do?amsmath
,amsmath.pdf
(produced by compilingamsmath.dtx
). It is the beginning of a set of matrix environments (matrix
,bmatrix
,pmatrix
, etc.)[...]
after[1]
(or[2]
,[3]
) means The first argument is optional, and specify the default value.\c@MaxMatrixCols
is the same of\value{MaxMatrixCols}
, whereMaxMatrixCols
is a counter defined inamsmath
(default to 10).* \c@MaxMatrixCols c
is used as the argument of\array
, it is equivalent to\arraycolsep
is produced by\array
i.e.array
environment. I would use\begin{@{}*{10}{c}@{}}
instead of this, if I write it.\new@ifnextchar
is defined inamsgen.sty
loaded byamsmath
. Read the documented source code.\array
is the beginning ofarray
environment. Note that the individual matrix environments eventually call\endarray
.